Barcode Scanner Libraries for Android

Overview

Project Archived

July 1 2020

This project is no longer maintained. When I first started this project in late 2013 there were very few libraries to help with barcode scanning on Android. But the situation today is much different. We have lots of great libraries based on ZXing and there is also barcode scanning API in Google's MLKit (https://github.com/googlesamples/mlkit). So given the options I have decided to stop working on this project.

Introduction

Android library projects that provides easy to use and extensible Barcode Scanner views based on ZXing and ZBar.

Screenshots

Minor BREAKING CHANGE in 1.8.4

Version 1.8.4 introduces a couple of new changes:

  • Open Camera and handle preview frames in a separate HandlerThread (#1, #99): Though this has worked fine in my testing on 3 devices, I would advise you to test on your own devices before blindly releasing apps with this version. If you run into any issues please file a bug report.
  • Do not automatically stopCamera after a result is found #115: This means that upon a successful scan only the cameraPreview is stopped but the camera is not released. So previously if your code was calling mScannerView.startCamera() in the handleResult() method, please replace that with a call to mScannerView.resumeCameraPreview(this);

ZXing

Installation

Add the following dependency to your build.gradle file.

repositories {
   jcenter()
}
implementation 'me.dm7.barcodescanner:zxing:1.9.13'

Simple Usage

1.) Add camera permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" />

2.) A very basic activity would look like this:

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);                // Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v(TAG, rawResult.getText()); // Prints scan results
        Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Please take a look at the zxing-sample project for a full working example.

Advanced Usage

Take a look at the FullScannerActivity.java or FullScannerFragment.java classes to get an idea on advanced usage.

Interesting methods on the ZXingScannerView include:

// Toggle flash:
void setFlash(boolean);

// Toogle autofocus:
void setAutoFocus(boolean);

// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);

// Specify the cameraId to start with:
void startCamera(int cameraId);

Specify front-facing or rear-facing cameras by using the void startCamera(int cameraId); method.

For HUAWEI mobile phone like P9, P10, when scanning using the default settings, it won't work due to the "preview size", please adjust the parameter as below:

mScannerView = (ZXingScannerView) findViewById(R.id.zx_view);

// this paramter will make your HUAWEI phone works great!
mScannerView.setAspectTolerance(0.5f);

Supported Formats:

BarcodeFormat.UPC_A
BarcodeFormat.UPC_E
BarcodeFormat.EAN_13
BarcodeFormat.EAN_8
BarcodeFormat.RSS_14
BarcodeFormat.CODE_39
BarcodeFormat.CODE_93
BarcodeFormat.CODE_128
BarcodeFormat.ITF
BarcodeFormat.CODABAR
BarcodeFormat.QR_CODE
BarcodeFormat.DATA_MATRIX
BarcodeFormat.PDF_417

ZBar

Installation

Add the following dependency to your build.gradle file.

repositories {
   jcenter()
}
implementation 'me.dm7.barcodescanner:zbar:1.9.13'

Simple Usage

1.) Add camera permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" />

2.) A very basic activity would look like this:

public class SimpleScannerActivity extends Activity implements ZBarScannerView.ResultHandler {
    private ZBarScannerView mScannerView;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZBarScannerView(this);    // Programmatically initialize the scanner view
        setContentView(mScannerView);                // Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v(TAG, rawResult.getContents()); // Prints scan results
        Log.v(TAG, rawResult.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Please take a look at the zbar-sample project for a full working example.

Advanced Usage

Take a look at the FullScannerActivity.java or FullScannerFragment.java classes to get an idea on advanced usage.

Interesting methods on the ZBarScannerView include:

// Toggle flash:
void setFlash(boolean);

// Toogle autofocus:
void setAutoFocus(boolean);

// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);

Specify front-facing or rear-facing cameras by using the void startCamera(int cameraId); method.

Supported Formats:

BarcodeFormat.PARTIAL
BarcodeFormat.EAN8
BarcodeFormat.UPCE
BarcodeFormat.ISBN10
BarcodeFormat.UPCA
BarcodeFormat.EAN13
BarcodeFormat.ISBN13
BarcodeFormat.I25
BarcodeFormat.DATABAR
BarcodeFormat.DATABAR_EXP
BarcodeFormat.CODABAR
BarcodeFormat.CODE39
BarcodeFormat.PDF417
BarcodeFormat.QR_CODE
BarcodeFormat.CODE93
BarcodeFormat.CODE128

Rebuilding ZBar Libraries

mkdir some_work_dir
cd work_dir
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz

Patch the localcharset.c file: vim libiconv-1.14/libcharset/lib/localcharset.c

On line 48, add the following line of code:

#undef HAVE_LANGINFO_CODESET

Save the file and continue with steps below:

cd libiconv-1.14
./configure
cd ..
hg clone http://hg.code.sf.net/p/zbar/code zbar-code
cd zbar-code/android
android update project -p . -t 'android-19'

Open jni/Android.mk file and add fPIC flag to LOCAL_C_FLAGS. Open jni/Application.mk file and specify APP_ABI targets as needed.

ant -Dndk.dir=$NDK_HOME  -Diconv.src=some_work_dir/libiconv-1.14 zbar-clean zbar-all

Upon completion you can grab the .so and .jar files from the libs folder.

Credits

Almost all of the code for these library projects is based on:

  1. CameraPreview app from Android SDK APIDemos
  2. The ZXing project: https://github.com/zxing/zxing
  3. The ZBar Android SDK: https://github.com/ZBar/ZBar/tree/master/android (Previously: http://sourceforge.net/projects/zbar/files/AndroidSDK/)

Contributors

https://github.com/dm77/barcodescanner/graphs/contributors

License

License for code written in this project is: Apache License, Version 2.0

License for zxing and zbar projects is here:

Comments
  • Exception: java.lang.RuntimeException: autoFocus failed

    Exception: java.lang.RuntimeException: autoFocus failed

    Hi, the barcodescanner initially works fine but after trying to auto focus two to three times i get a RuntimeException: autoFocus failed:

    java.lang.RuntimeException: autoFocus failed
                at android.hardware.Camera.native_autoFocus(Native Method)
                at android.hardware.Camera.autoFocus(Camera.java:975)
                at me.dm7.barcodescanner.core.CameraPreview$1.run(CameraPreview.java:196)
                at android.os.Handler.handleCallback(Handler.java:730)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:213)
                at android.app.ActivityThread.main(ActivityThread.java:5225)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:525)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
                at dalvik.system.NativeStart.main(Native Method)
    
    

    any idea how to fix that? Thanks Criss

    opened by crissk047 37
  • 1.7.1 : Zbar example basic : autofocus failed

    1.7.1 : Zbar example basic : autofocus failed

    Hello,

    In the last version (before it works). When I do a simple activity like a exemple in the home page of your project I have a error :

    java.lang.RuntimeException: autoFocus failed at android.hardware.Camera.native_autoFocus(Native Method) at android.hardware.Camera.autoFocus(Camera.java:1299) at me.dm7.barcodescanner.core.CameraPreview$1.run(CameraPreview.java:245) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5317) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

    Thanks

    opened by Paul75 30
  • Bug with Zbar

    Bug with Zbar

    Anyone else have this problem with the newest version of zbar ?

    java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/de.xxx.xxx-2/lib/arm/libiconv.so: has text relocations
    
    opened by larsdecker 25
  • Add option for scaling the camera preview

    Add option for scaling the camera preview

    Whether or not to scale the preview is defined by the boolean resource cameraPreviewShouldFillView. Default is false, but can be overridden by applications by defining

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <bool name="cameraPreviewShouldFillView">true</bool>
    </resources> 
    

    This should not cause problems due to stretching, as aspect ratio is maintained, by not wrapping the preview in a RelativeLayout (keeping FrameLayout as parent, which clips/crops).

    opened by xolan 23
  • An error occured while connecting to camera

    An error occured while connecting to camera

    I'm getting W/CameraBaseοΉ• An error occurred while connecting to camera when the app opens up, a black screen with the red scan line appears. What's causing this? I'm using Nexus 4 with Android Lollipop 5.0

    bug 
    opened by RonCan 18
  • E/ZBarScannerView: java.lang.RuntimeException: Camera is being used after Camera.release() was called

    E/ZBarScannerView: java.lang.RuntimeException: Camera is being used after Camera.release() was called

    I just open scanner simple activity and click back button then it throws this error "E/ZBarScannerView: java.lang.RuntimeException: Camera is being used after Camera.release() was called" p.s. My phone is Huawei P9.

    wontfix 
    opened by BigWattanachai 17
  • Scanner does not work in portrairt mode

    Scanner does not work in portrairt mode

    First of all thanks for this amazing library, really made my android developer life easier :) I have some problem with the last version of the app (1.9.5). I basically copied and pasted the SampleScanner example provided and the scanner works fine only in landscape mode, while in portrait mode the result handler is not even called. I have a galaxy s5 mini with Android 6.0 installed. Notice that I do not have the same problem if the dependency I add to gradle is 1.9.4.

    opened by dovidio 16
  • White screen in a fragment (zbar barcode scanning)

    White screen in a fragment (zbar barcode scanning)

    Thanks for a nice library. I have an application with old version of this library. I added a fragment to activity as usual, started camera similar to https://github.com/dm77/barcodescanner/blob/master/zbar-sample/src/main/java/me/dm7/barcodescanner/zbar/sample/SimpleScannerFragment.java. I saw an image and could get a EAN_13 code. Then closed the fragment. The second time I opened it I saw a black screen instead of camera preview. Sometimes after several efforts camera worked again. Also a standard photo application wrote that the camera is in use. When I changed a fragment to activity, everything worked right. Then I updated a library to v. 1.9. Instead of the black screen I got white. It appears on the second opening of the fragment. Third, fourth, etc. times don't change the behaviour. Again, changing to activity reuses camera well. A device is Samsung Galaxy S4 running Android 5.0.1. Also tested on another Android 5.0 device.

    Update

    After one day of testing scanning with an activity I noticed that in seldom cases the activity also shows white screen.

    wontfix 
    opened by CoolMind 14
  • Crashes on Samsung S phones

    Crashes on Samsung S phones

    java.lang.RuntimeException: getParameters failed (empty parameters) at android.hardware.Camera.native_getParameters(Native Method) at android.hardware.Camera.getParameters(Camera.java:1952) at me.dm7.barcodescanner.core.CameraUtils.isFlashSupported(CameraUtils.java:47) at me.dm7.barcodescanner.core.BarcodeScannerView.setFlash(BarcodeScannerView.java:252) at me.dm7.barcodescanner.core.BarcodeScannerView.setupCameraPreview(BarcodeScannerView.java:189) at me.dm7.barcodescanner.core.CameraHandlerThread$1$1.run(CameraHandlerThread.java:31) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

    wontfix 
    opened by tibigeorgescu90 12
  • java.lang.RuntimeException: cancelAutoFocus failed in onPause()

    java.lang.RuntimeException: cancelAutoFocus failed in onPause()

    Hi, got this Issue after upgrading Andoid OS in my ActivityCodeScanner in method onPause():

    @Override public void onPause() { super.onPause(); mScannerView.stopCamera(); }

    Where mScannerView is:

    @Override public void onCreate(Bundle state) { super.onCreate(state); mScannerView = new ZXingScannerView(this); setContentView(mScannerView); .............................................. }

    My Device is Huawei Honor 4x (Android 6.0)

    StackTrace: java.lang.RuntimeException: cancelAutoFocus failed at android.hardware.Camera.native_cancelAutoFocus(Native Method) at android.hardware.Camera.cancelAutoFocus(Camera.java:1476) at me.dm7.barcodescanner.core.CameraPreview.stopCameraPreview(CameraPreview.java:109) at me.dm7.barcodescanner.core.BarcodeScannerView.stopCamera(BarcodeScannerView.java:85) at com.android.app.mysuperapp.ui.ActivityCodeScanner.onPause(ActivityCodeScanner.java:57) at android.app.Activity.performPause(Activity.java:6495) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1322) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3398) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3371) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3346) at android.app.ActivityThread.access$1100(ActivityThread.java:165) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:5546) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)

    What other information need to provide?

    opened by whizzzkey 12
  • Valid QR Code not being read.

    Valid QR Code not being read.

    I have a valid QR code that gets read in iOS (using a different library) but fails to be read in Android. I have followed the example verbatim and still not getting it. Please advise -- Is there anything I'm doing wrong? Perhaps in generating the QR code? But it works in iOS?

    I have attached my QR code below:

    0bb5668edbb67eb

    opened by kalvish21 12
Releases(1.9.8)
Owner
Dushyanth
Dushyanth
ZATAC Scanner is Android Kotlin-based QR code scanner and parser which de-crypt TLV qr codes and parse them into their values.

ZATAC Scanner is Android Kotlin-based QR code scanner and parser which de-crypt TLV qr codes and parse them into their values.

Enozom 12 Apr 23, 2022
Android app and Python library for turning mobile phone into a WebSocket-based, remotely controllable Barcode/QR code reader

Remote Barcode Reader suite Android app and Python library for turning mobile phone into a remotely controllable Barcode/QR code reader. It exposes a

Krystian DuΕΌyΕ„ski 3 Dec 6, 2022
A library to help implement barcode scanning

A library to help implement barcode scanning

Brightec 99 Nov 30, 2022
QrX: Camera with ML Kit QR/Barcode detection

QrX: Camera with ML Kit QR/Barcode detection This library allows you to easily add CameraX Preview with attached ML Kit to detect and display Qr codes

Mateusz Lutecki 1 Apr 7, 2022
Qart is Android App that's based on CuteR project. App generate QR code that merge a picture. So the QR code looks more beautiful.

Qart is Android App that's based on CuteR project. App generate QR code that merge a picture. So the QR code looks more beautiful.

scola 1k Dec 16, 2022
QRAlarm - an Android alarm clock application lets the user turn off alarms by scanning the QR Code.

QRAlarm is an Android alarm clock application that does not only wake You up, but also makes You get up to disable the alarm by scanning the QR Code.

null 39 Jan 8, 2023
Android library for creating QR-codes with logo, custom pixel/eyes shapes, background image. Powered by ZXing.

custom-qr-generator Android library for creating QR-codes with logo, custom pixel/eyes shapes, background image. Powerd by ZXing. Installation To get

Alexander Zhirkevich 34 Dec 17, 2022
Barcode Scanner Libraries for Android

Project Archived July 1 2020 This project is no longer maintained. When I first started this project in late 2013 there were very few libraries to hel

Dushyanth 5.4k Jan 9, 2023
Barcode scanner library for Android, based on the ZXing decoder

ZXing Android Embedded Barcode scanning library for Android, using ZXing for decoding. The project is loosely based on the ZXing Android Barcode Scann

JourneyApps 5.3k Jan 4, 2023
Yet another barcode scanner for Android

Binary Eye Yet another barcode scanner for Android. As if there weren't enough. This one is free, without any ads and open source. Works in portrait a

Markus Fisch 802 Dec 31, 2022
Android barcode scanner with ML-Kit vision api

MLBarcodeScanner A demo project to show how to implement barcode scanner using Google ML-Kit Vision api Supported barcode types 2D formats: QR Code, A

Mahdi Javaheri 5 Dec 26, 2022
This app contains feature barcode and QR scanner, and video recording in-app.

BarcodeQRScannerAndVideoRecord This app contains feature barcode and QR scanner, and video recording in-app. Scanner using ML Kit and CameraX. Video R

Abdullah Fahmi 1 Dec 23, 2021
ZATAC Scanner is Android Kotlin-based QR code scanner and parser which de-crypt TLV qr codes and parse them into their values.

ZATAC Scanner is Android Kotlin-based QR code scanner and parser which de-crypt TLV qr codes and parse them into their values.

Enozom 12 Apr 23, 2022
D4rK QR & Bar Code Scanner Plus is a FOSS scanner app for every Android. πŸ“·

?? QR & Bar Code Scanner Plus ?? ╔╦╦╦═╦╗╔═╦═╦══╦═╗ β•‘β•‘β•‘β•‘β•©β•£β•šβ•£β•β•£β•‘β•‘β•‘β•‘β•‘β•©β•£ β•šβ•β•β•©β•β•©β•β•©β•β•©β•β•©β•©β•©β•©β•β• D4rK QR & Bar Code Scanner Plus is a FOSS scanner app for every

D4rK 8 Dec 19, 2022
ZXing ("Zebra Crossing") barcode scanning library for Java, Android

Project in Maintenance Mode Only The project is in maintenance mode, meaning, changes are driven by contributed patches. Only bug fixes and minor enha

ZXing Project 30.5k Dec 27, 2022
Android app and Python library for turning mobile phone into a WebSocket-based, remotely controllable Barcode/QR code reader

Remote Barcode Reader suite Android app and Python library for turning mobile phone into a remotely controllable Barcode/QR code reader. It exposes a

Krystian DuΕΌyΕ„ski 3 Dec 6, 2022
A library to help implement barcode scanning

A library to help implement barcode scanning

Brightec 99 Nov 30, 2022
SmartLens uses Google's ML Kit for Barcode scanning, Face recognition, Text recognition, and Image labeling.

SmartLens SmartLens uses Google's ML Kit for Barcode scanning, Face recognition, Text recognition, and Image labeling. ?? Screen Shots ?? Features It

Prince Fahad 2 Sep 14, 2022
QrX: Camera with ML Kit QR/Barcode detection

QrX: Camera with ML Kit QR/Barcode detection This library allows you to easily add CameraX Preview with attached ML Kit to detect and display Qr codes

Mateusz Lutecki 1 Apr 7, 2022
Android development recommendation for outdated libraries and current libraries preferred as of 2021

GADS21-Android-Recomendation-For currently used libraries and dependencies Android development recommendation for outdated libraries and current libra

Timz Owen 6 Nov 22, 2021