Convert Figma logo to code with AI

phonegap logophonegap-plugin-barcodescanner

cross-platform BarcodeScanner for Cordova / PhoneGap

1,271
1,407
1,271
79

Top Related Projects

32,747

ZXing ("Zebra Crossing") barcode scanning library for Java, Android

Barcode scanner library for Android, based on the ZXing decoder

Barcode Scanner Libraries for Android

A cross platform HTML5 QR code reader. See end to end implementation at: https://scanapp.org

Quick Overview

The phonegap-plugin-barcodescanner is a Cordova/PhoneGap plugin that enables barcode scanning functionality in mobile applications. It supports various barcode formats and provides a simple API for integrating barcode scanning capabilities into cross-platform mobile apps.

Pros

  • Cross-platform compatibility (iOS, Android, Windows)
  • Supports multiple barcode formats (QR Code, Data Matrix, UPC, etc.)
  • Easy integration with Cordova/PhoneGap projects
  • Active community and regular updates

Cons

  • Dependency on Cordova/PhoneGap ecosystem
  • May have performance limitations compared to native implementations
  • Limited customization options for the scanning interface
  • Potential issues with newer mobile OS versions due to plugin update delays

Code Examples

  1. Basic barcode scanning:
cordova.plugins.barcodeScanner.scan(
  function (result) {
    alert("Barcode scanned: " + result.text);
  },
  function (error) {
    alert("Scanning failed: " + error);
  }
);
  1. Scanning with additional options:
cordova.plugins.barcodeScanner.scan(
  function (result) {
    console.log("Format: " + result.format);
    console.log("Cancelled: " + result.cancelled);
  },
  function (error) {
    console.error("Scanning failed: " + error);
  },
  {
    preferFrontCamera: true,
    showFlipCameraButton: true,
    showTorchButton: true,
    torchOn: false,
    prompt: "Place a barcode inside the scan area",
    resultDisplayDuration: 500,
    formats: "QR_CODE,PDF_417",
    orientation: "landscape",
    disableAnimations: true,
    disableSuccessBeep: false
  }
);
  1. Encoding text as a barcode:
cordova.plugins.barcodeScanner.encode(
  cordova.plugins.barcodeScanner.Encode.TEXT_TYPE,
  "https://example.com",
  function(success) {
    alert("Encoding succeeded: " + success);
  },
  function(fail) {
    alert("Encoding failed: " + fail);
  }
);

Getting Started

  1. Install the plugin in your Cordova/PhoneGap project:

    cordova plugin add phonegap-plugin-barcodescanner
    
  2. Add the following permissions to your config.xml file:

    <config-file target="AndroidManifest.xml" parent="/*" mode="merge">
        <uses-permission android:name="android.permission.CAMERA" />
    </config-file>
    
  3. Use the plugin in your JavaScript code as shown in the examples above.

  4. Build and run your project:

    cordova build
    cordova run android (or ios)
    

Competitor Comparisons

32,747

ZXing ("Zebra Crossing") barcode scanning library for Java, Android

Pros of ZXing

  • More comprehensive barcode support, including 1D and 2D formats
  • Actively maintained with regular updates and improvements
  • Supports multiple programming languages and platforms

Cons of ZXing

  • Steeper learning curve due to its broader scope
  • May require more configuration and setup for specific use cases
  • Larger library size, which could impact app performance

Code Comparison

ZXing (Java):

MultiFormatReader reader = new MultiFormatReader();
Result result = reader.decode(binaryBitmap);
String contents = result.getText();

PhoneGap-Plugin-BarcodeScanner (JavaScript):

cordova.plugins.barcodeScanner.scan(
  function (result) { alert("Barcode: " + result.text); },
  function (error) { alert("Scanning failed: " + error); }
);

ZXing offers more low-level control and flexibility, while PhoneGap-Plugin-BarcodeScanner provides a simpler, plugin-based approach for PhoneGap/Cordova applications. ZXing is better suited for complex barcode scanning needs across various platforms, while PhoneGap-Plugin-BarcodeScanner is ideal for quick integration in mobile hybrid apps.

Barcode scanner library for Android, based on the ZXing decoder

Pros of zxing-android-embedded

  • Native Android implementation, offering better performance and integration with Android devices
  • More frequent updates and active maintenance
  • Supports a wider range of barcode formats

Cons of zxing-android-embedded

  • Limited to Android platform, not cross-platform like phonegap-plugin-barcodescanner
  • Requires more setup and configuration for integration into non-native Android projects

Code Comparison

zxing-android-embedded:

IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0);
integrator.initiateScan();

phonegap-plugin-barcodescanner:

cordova.plugins.barcodeScanner.scan(
  function (result) {
    alert("We got a barcode\n" + "Result: " + result.text);
  },
  function (error) {
    alert("Scanning failed: " + error);
  }
);

The zxing-android-embedded code shows native Android implementation, while phonegap-plugin-barcodescanner uses JavaScript and Cordova plugins for cross-platform functionality. The native implementation offers more control over the scanning process, but requires more setup. The Cordova plugin provides a simpler API for quick integration across multiple platforms.

Barcode Scanner Libraries for Android

Pros of barcodescanner

  • Native Android implementation, potentially offering better performance
  • Supports a wider range of barcode formats
  • More customization options for the scanning UI

Cons of barcodescanner

  • Limited to Android platform only
  • May require more setup and configuration compared to the PhoneGap plugin
  • Less integration with cross-platform development frameworks

Code Comparison

phonegap-plugin-barcodescanner:

cordova.plugins.barcodeScanner.scan(
  function (result) {
    alert("Barcode: " + result.text + "\nFormat: " + result.format);
  },
  function (error) {
    alert("Scanning failed: " + error);
  }
);

barcodescanner:

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0);
integrator.initiateScan();

The phonegap-plugin-barcodescanner uses a JavaScript API for easy integration with PhoneGap/Cordova applications, while barcodescanner requires Java code and is more tightly integrated with Android's native environment. The PhoneGap plugin offers a simpler API for cross-platform development, whereas barcodescanner provides more control and customization options for Android-specific implementations.

A cross platform HTML5 QR code reader. See end to end implementation at: https://scanapp.org

Pros of html5-qrcode

  • Pure JavaScript implementation, no native dependencies required
  • Works directly in web browsers without additional plugins
  • Supports multiple cameras and file-based QR code scanning

Cons of html5-qrcode

  • Limited to QR codes only, doesn't support other barcode formats
  • May have lower performance on older devices compared to native solutions
  • Requires HTTPS for camera access in modern browsers

Code Comparison

html5-qrcode:

const html5QrCode = new Html5Qrcode("reader");
html5QrCode.start(
  { facingMode: "environment" },
  { fps: 10, qrbox: 250 },
  qrCodeSuccessCallback,
  qrCodeErrorCallback
);

phonegap-plugin-barcodescanner:

cordova.plugins.barcodeScanner.scan(
  function (result) { alert("Scanned: " + result.text); },
  function (error) { alert("Scanning failed: " + error); },
  { preferFrontCamera: false, showFlipCameraButton: true }
);

The html5-qrcode library offers a more web-centric approach, ideal for projects that don't require native integration. It's easier to implement in pure web applications but may lack some advanced features and performance optimizations of the PhoneGap plugin. The PhoneGap plugin, being a native solution, provides broader barcode format support and potentially better performance on mobile devices, but requires the PhoneGap/Cordova framework.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PhoneGap Plugin BarcodeScanner

================================

Build Status

Cross-platform BarcodeScanner for Cordova / PhoneGap.

Follows the Cordova Plugin spec, so that it works with Plugman.

Installation

This requires phonegap 7.1.0+ ( current stable v8.0.0 )

phonegap plugin add phonegap-plugin-barcodescanner

It is also possible to install via repo url directly ( unstable )

phonegap plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git

Optional variables: This plugin requires the Android support library v4. The minimum version is 24.1.0. Default value is 27.+. Check out the latest version here.

phonegap plugin add phonegap-plugin-barcodescanner --variable ANDROID_SUPPORT_V4_VERSION="27.1.1"

Supported Platforms

  • Android
  • iOS
  • Windows (Windows/Windows Phone 8.1 and Windows 10)
  • Browser

Note: the Android source for this project includes an Android Library Project. plugman currently doesn't support Library Project refs, so its been prebuilt as a jar library. Any updates to the Library Project should be committed with an updated jar.

Note: Windows 10 applications can not be build for AnyCPU architecture, which is default for Windows platform. If you want to build/run Windows 10 app, you should specify target architecture explicitly, for example (Cordova CLI):

cordova run windows -- --archs=x86

PhoneGap Build Usage

Add the following to your config.xml:

<!-- add a version here, otherwise PGB will use whatever the latest version of the package on npm is -->
<plugin name="phonegap-plugin-barcodescanner" />

On PhoneGap Build if you're using a version of cordova-android of 4 or less, ensure you're building with gradle:

<preference name="android-build-tool" value="gradle" />

Using the plugin

The plugin creates the object cordova.plugins.barcodeScanner with the method scan(success, fail).

The following barcode types are currently supported:

Barcode TypeAndroidiOSWindows
QR_CODE✔✔✔
DATA_MATRIX✔✔✔
UPC_A✔✔✔
UPC_E✔✔✔
EAN_8✔✔✔
EAN_13✔✔✔
CODE_39✔✔✔
CODE_93✔✔✔
CODE_128✔✔✔
CODABAR✔✖✔
ITF✔✔✔
RSS14✔✖✔
PDF_417✔✔✔
RSS_EXPANDED✔✖✖
MSI✖✖✔
AZTEC✔✔✔

success and fail are callback functions. Success is passed an object with data, type and cancelled properties. Data is the text representation of the barcode data, type is the type of barcode detected and cancelled is whether or not the user cancelled the scan.

A full example could be:

   cordova.plugins.barcodeScanner.scan(
      function (result) {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
      },
      function (error) {
          alert("Scanning failed: " + error);
      },
      {
          preferFrontCamera : true, // iOS and Android
          showFlipCameraButton : true, // iOS and Android
          showTorchButton : true, // iOS and Android
          torchOn: true, // Android, launch with the torch switched on (if available)
          saveHistory: true, // Android, save scan history (default false)
          prompt : "Place a barcode inside the scan area", // Android
          resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
          formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
          orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
          disableAnimations : true, // iOS
          disableSuccessBeep: false // iOS and Android
      }
   );

Encoding a Barcode

The plugin creates the object cordova.plugins.barcodeScanner with the method encode(type, data, success, fail).

Supported encoding types:

  • TEXT_TYPE
  • EMAIL_TYPE
  • PHONE_TYPE
  • SMS_TYPE
A full example could be:

   cordova.plugins.barcodeScanner.encode(cordova.plugins.barcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
            alert("encode success: " + success);
          }, function(fail) {
            alert("encoding failed: " + fail);
          }
        );

iOS quirks

Since iOS 10 it's mandatory to add a NSCameraUsageDescription in the Info.plist.

NSCameraUsageDescription describes the reason that the app accesses the user's camera. When the system prompts the user to allow access, this string is displayed as part of the dialog box. If you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.

To add this entry you can use the edit-config tag in the config.xml like this:

<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
    <string>To scan barcodes</string>
</edit-config>

Windows quirks

  • Windows implementation currently doesn't support encode functionality.

  • On Windows 10 desktop ensure that you have Windows Media Player and Media Feature pack installed.

Thanks on Github

So many -- check out the original iOS, Android and BlackBerry 10 repos.

Licence

The MIT License

Copyright (c) 2010 Matt Kane

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

NPM DownloadsLast 30 Days