A requirement came up while developing an Android application for my client for scanning bar code. Obviously I went for the famous ZXing library and downloaded the approximately 120MB source code zip file from Git Hub but I soon started feeling frustrated that I am wasting more time in understanding this huge library.

ZXing is a good library having a lot of features but it was not the right fit for my requirement, The initial setup of the library was very time consuming and i realised soon that i have to look for something other than this.

Fortunately I came across another open source project ZBar bar code reader at Sourceforge.net website. 

ZBAR barcode/QR code scanner library is a very good lightweight alternative to ZXing library and very easy to use and lightweight too.

The ZBAR library supports variety of bar code standards and also QR codes as below,

  • EAN-13/UPC-A,
  • UPC-E, EAN-8,
  • Code 128,
  • Code 39,
  • Interleaved 2 of 5
  • QR Code.

ZBAR bar code library supports the following platforms and features,

  • Cross platform - Linux/Unix, Windows, IOS, Embedded systems
  • High speed - real time scanning from video streams
  • Small memory footprint
  • Small code size - the core scanner and EAN decoder represent under 1K lines of C code not limited to images
  • No floating point operations suitable for embedded applications using inexpensive processors/hardware modular components can be used together or separately

Using it : The ZBAR project is licensed under GNU/GPL license, means you can use it freely in any of your projects (Please read license details carefully before using, There are some constraints). 

I didn't bother to study all of its features, I just downloaded the demo application ZBarAndroidSDK-0.1.zip which has a JAVA library zbar.jar and two java files, CameraPreview.java and CameraTestActivity.java 

 Click here to download the demo application


Calling the Zbar library from you android application :

I included all the above mentioned files as exactly in the demo application and made the calls to the ZBAR library using Android Intent as follows,

Intent i = new Intent(context, CameraTestActivity.class);

                           startActivityForResult(i, 1);


Modifying the library to return result to your android application :

After calling the activity it opens a camera view and scans a image and generates a result, i.e Bar code/QR code value which we need to collect and return to the calling activity, I do this as below,

    PreviewCallback previewCb = new PreviewCallback() {

            public void onPreviewFrame(byte[] data, Camera camera) {

                Camera.Parameters parameters = camera.getParameters();

                Size size = parameters.getPreviewSize(); 

                Image barcode = new Image(size.width, size.height, "Y800");

                barcode.setData(data); 

                int result = scanner.scanImage(barcode);               

                if (result != 0) {

                    previewing = false;

                    mCamera.setPreviewCallback(null);

                    mCamera.stopPreview();                   

                    SymbolSet syms = scanner.getResults();

                    for (Symbol sym : syms) {

                        scanText.setText("barcode result " + sym.getData());

                        barcodeScanned = true;

                    } 

                    if(barcodeScanned == true)

                    {

                    Intent returnIntent = new Intent();

                    returnIntent.putExtra("result",scanText.getText());

                    setResult(RESULT_OK,returnIntent);

                     finish();

                    }

                }

            }

        };     


Collecting the result :

I added the if statement to check if the bar code scanning was successful and return the result to the calling activity.

Now that the result is generated and returned to the calling activity, I added the below function to the calling activity to collect the result from the scanner library activity,

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

        if (requestCode == 1) {

            if(resultCode == RESULT_OK){

                String result=data.getStringExtra("result");

                Toast.makeText(this, result,Toast.LENGTH_LONG).show();               

            }

            if (resultCode == RESULT_CANCELED) {

                //Write your code if there's no result

            }

        } 

    }

The above function generates a toast message on the screen with the scanned result. 

Hope it helps some one. :) 

 

Also see:

 




About the Author

is the author of this blog and is an Embedded systems design engineer by profession & Founder @ Valetron Systems Pvt Ltd


Email:This email address is being protected from spambots. You need JavaScript enabled to view it.

Find us on Google+

HIRE ME FOR YOUR PROJECT AS A FREELANCER

 

EasyEDA: Free Circuit Design Software

  • Easy Schematic Capture
  • Powerful Circuit Simulation
  • Online PCB design

Design Your Circuit and PCB here:

https://easyeda.com/editor

PCB Prototype Manufacturer - JLCPCB

Electronic Components Online Store