Android zxing Barcode scanner integration

Android zxing Barcode scanner integration

Applications where we need a barcode scanner integrated in our app, the best and first scanner library hits you is zxing. So lets get started how to build and integrate the library to our Project.

Final results:

td_scanner_img3

Step 1: Download the Zxing sources from Git: https://github.com/zxing/zxing
Step 2: For building the Zxing library project you need a library called core.jar

You can directly download the latest releases of jar from maven repository using either one of the below links:

http://repo1.maven.org/maven2/com/google/zxing/core/

https://oss.sonatype.org/content/repositories/snapshots/com/google/zxing/core/

OR You can always generate the core.jar from the sources downloaded.

Here are the steps to generate the build on a Mac:

1: Navigate to the ‘core’ folder in the zxing-master library from your terminal
2: Run: [mvn -DskipTests -Dgpg.skip=true install]

If Maven not installed you cant run the above command.
Install Maven:

a: I would recommend to install ‘brew’: an installer for third pary packages. (Go to http://brew.sh/ for instructions on brew installation)
b: From the terminal just run:

ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”

c: Now that you are done with ‘brew’ installation. Just tell brew to install maven by running:

brew install maven

Step 3:  After successful build a target folder will be created, just pick the core-x.y.z.jar file from the folder.

Now we got our heart of the application : core-x.y.z.jar Proceed to Building the zxing library.

Before integrating scanner to our app we need to first build the zxing Android library.

Note: for this tutorial i used zxing library version 4.5.1, As 4.5.2 uses java 1.7 compiler which has issues with eclipse but the same works well with Android studio/.

Step 4: From eclipse just import ‘android’ folder from zxing-master to projects.
Step 5: Now create a libs directory under the root then copy the core-x.y.z.jar.

Also add the core-x.y.z.jar to build path.
(Project Properties > Java Build Path > Libraries > Add JARs… > Select the library project then core.jar from libs > Click ‘Ok’)

td_scanner_img5

All your error are gone and the project is ready for build just run and check the App on a device.

Step 6: Now that our Example scanner App is running fine, we need to integrate this to our project.
Step 7: First mark the android zxing project built from the last step as library from properties and checking the ‘is library’ option.

td_scanner_img4

Step 8: Okay now from our project (Already existing one or i created a new project for the sake of this tutorial) just go to properties > under references add the scanner project.

td_scanner_img7

 

Step 9: First of all add this permission to your manifest:
<usespermission android:name=android.permission.CAMERA/>

then add this activity declaration to your Manifest File:

Step 10: Intent to call scanner:

Available Scan Modes:

SCAN_MODE : Decodes all barcodes that are understood by zxing.

PRODUCT_MODE : Decode only UPC and EAN barcodes. This is the right choice for shopping apps which get prices, reviews, etc. for products.

ONE_D_MODE : Decode only 1D barcodes.
QR_CODE_MODE : Decode only QR codes.
DATA_MATRIX_MODE : Decode only Data Matrix codes.
AZTEC_MODE : Decode only Aztec.
PDF417_MODE : Decode only PDF417.

Use can also set multiple formats using: “SCAN_FORMATS”, “Comma separated scan modes”
Ex: intent.putExtra(“SCAN_FORMATS”, “PRODUCT_MODE,ONE_D_MODE,DATA_MATRIX_MODE”);

Note: It is recommended to explicitly set the formats which are only required for the scan.

Optionally zxing also provides to set the scanning rectangle height and width in pixels.
The app will try to honor these, but will clamp them to the size of the preview frame.
You should specify both or neither, and pass the size as an int. [SCAN_WIDTH, SCAN_HEIGHT]

Also you can set the Prompt message to show on-screen when scanning by intent, using [PROMPT_MESSAGE]

Content has been abstracted from intent.java from the zxing library. Please refer the below link for more information on Intent options:
https://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/Intents.java

 Step 11: In on Activity results get the scanned results:

Complete code of MainActivity:

 

Final Manifest file:

 

More like this: android

Comments are closed.