Monaca Docs
Search…
Barcode Scanner Plugin
Tested Version: 1.0.0
This plugin provides a scanning barcode feature. Detect barcode or QR Code[^1] by device's camera and returns extracted strings.

Plugin ID

1
@monaca/monaca-plugin-barcode-scanner
Copied!

Adding the Plugin in Monaca

In order to use this plugin, please enable Barcode Scanner plugin in Monaca Cloud IDE.

Supported Platforms

Build Environments

  • Cordova 11.0.0 or later
  • Android Platform 10.1.2 or later
  • iOS Platform 6.2.0 or later

Operating Environments

  • Android 5.1 or later (9 or later recommended)
  • iOS 11 or later (13 or later recommended)

Supported Barcode Types

  • QR_CODE
  • EAN_8
  • EAN_13

API Reference

monaca.BarcodeScanner.scan()

1
monaca.BarcodeScanner.scan(successCallback, failCallback)
Copied!
  • Calling scan () will transition to the scanner screen.
  • When the barcode is detected, the extracted character string is displayed below the frame.
  • Tap the string to return to the original screen and the string and barcode type will be returned to successCallback.
  • When returned to the original screen without selecting the string, the detection will be cancelled. In order to return to the original screen, click the "Close" (X on the screen) button for iOS and the "Back" button for Android.

successCallback

successCallback(result)

result: following data
1
{
2
data: {
3
"text": "xxxxxxxx" // detected string
4
"format": "QR_CODE" // barcode type
5
},
6
cancelled: false // detection cancelled(true) or not(false)
7
}
Copied!

failCallback

failCallback(error)

error: error message(string)
message
description
"permission denied"
camera permission is not granted.

Example

1
monaca.BarcodeScanner.scan((result) => {
2
if (result.cancelled) {
3
// scan cancelled
4
} else {
5
// scan
6
const detected_text = result.data.text;
7
const detected_format = result.data.format;
8
}
9
}, (error) => {
10
// permission error
11
const error_message = error;
12
});
Copied!

iOS Quirks

Since iOS 10, it's mandatory to provide a usage description in the info.plist. The description string is displayed in the permission dialog box.
This plugin requires the following usage descriptions:
  • NSCameraUsageDescription specifies the reason for your app to access the device's camera.
To add these entries to the info.plist, you can use the <edit-config> tag in the config.xml file like this:
1
<platform name="ios">
2
<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
3
<string>need camera access to scan barcode</string>
4
</edit-config>
5
</platform>
Copied!

Android Quirks

The library androidx.camera:camera-view used internally requires compileSDKVersion>=31. So Target SDK Version of Android Application setting in Monaca Cloud IDE should be set 31 or above.
[^1]: QR Code is a registered trademark of DENSO WAVE INCORPORATED in Japan and in other countries.