Home / Basics / ZXing (Zebra Crossing) Android Library Tutorial – Library #4
cover

ZXing (Zebra Crossing) Android Library Tutorial – Library #4

This is library #4 in this series 100 Library in Android , in the previous article we had Android Sliding Menu Library Tutorial . In this tutorial we are having the ZXing (Zebra Crossing) Android Library Tutorial which is an Open Source Android library, multi-format 1D/2D barcode image processing library implemented in Java, that allows developers to easily carry out barcode scanning within an Android application. By importing the ZXing integration classes into our application, we can make user scans barcode easier and focus our development efforts on handling the scan results. The ZXing project is Apache licensed, so it is free to use without any kind of restrictions.

Applications integrated with ZXing:

Barcode Scanner is an Android application, from the open source project ZXing (Zebra Crossing), that allows a user to scan 1-D or 2-D “graphical barcodes” with the camera on their Android device.

Our Application:

Our application will use ZXing Library to make an application like to Barcode Scanner, allow the user to scan any bar code after clicking on “Scan Now” button, then the returned scan data -content and format of the barcode- will be written to a defined TextViews.

Main Steps:

  1. Download ZXing two classes that you will need to make our ZXing Example from here.
  2. Create an Android Application.
  3. Create a package called: com.google.zxing.integration.android, then copy the two classes that you downloaded from step 1 and paste them in this packge.
  4.  Then start to make the easiest part that we will discuss below.

Creating ZXing Example Project:

1.  In Android Studio, create a new activity, select Empty Activity and lets name ZXingMainActivity.

2. Create a package called: com.google.zxing.integration.android, then replace the two classes that you downloaded from here. After replacing the two classes, your project hierarchy might be like the below image:

ZXing (Zebra Crossing) Android Library3. Open activity_zxing_main.xml file and add this code: this would create a button called “scan_btn” since the user will press on this button to scan any barcode he/she wants, also create two TextViews called “scan_content_tv” to show the barcode content to the user and “scan_format_tv” to show the format of the barcode that the user scanned.

4. Then open ZXingMainActivity class, and this code:

  • The ZXing app will do all the work of scanning, you Activity will simply call the ZXing app using Intents and get the scanned results from it. In this class, we create 2 textviews, one to display the url which is contained in the QR code and other to display the format itself.
  • The scanner will start if it’s installed on the user’s device. If not, they’ll be prompted to download it.
  • The results of the scan will be returned to the ZXingMainActivity class where scanning was initiated, so we’ll be able to retrieve it in the onActivityResult() method.
5. Run your app on a device instead of an emulator so that you can see the scan functioning. Try scanning a book or any other barcode you might have, and the result should be like the below images:

This screen is the main screen that the user will press on “Scan Now” button to scan any barcode he/she wants.

After that, ZXing Example application will be redirected to Barcode Scanner application to scan the barcode that the user wants

ZXing (Zebra Crossing) Android Library

ZXing (Zebra Crossing) Android Library

And once the scanned barcode is valid, the content and the format of this barcode will be returned from Barcode Scanner application to be shown at ZXing (Zebra Crossing) Android Library Example

If the user press back button or the barcode is invalid, a Toast will be shown to the user telling him/her that no scan data are received.

ZXing (Zebra Crossing) Android Library

Summary
Article Title
ZXing (Zebra Crossing) Android Library Tutorial - Library #4
Author
Organization
Android Gifts

About Ahmed Adel

Ahmed Adel

Check Also

Fresco Android Library Tutorial

Fresco Android Library Tutorial – Library #8

This is library #8 in this series 100 Library in Android , in the previous article we …

Leave a Reply