Embed ZXing Barcode Scanner in an Android app

ZXing or ZebraCrossing is the creator of a bar-code scanner that is widely used. It is an open source project, primarily made for Android, that has ports to various languages. While importing the app itself is not very difficult, it has a caveat. A simple import needs the app to be installed in the user’s phone. And, while there are many third party open source projects which can help in importing an embedded or standalone bar-code scanner, they may not always be updated. So, the best option would be to embed the library itself into the android app source code. The procedure is not very difficult and is easy to understand. This procedure is detailed in the following.

Firstly, download the ZXing library source code from their github page here. While the full project can be downloaded, we will only need the android directory from this. So, one can download only the android directory, since the whole project’s size is a little large.

Download the latest core-x.x.x.jar from the maven repository of the zxing project here. At the time of this blog post, the latest one was 3.3.0, so if it is still the same download it from here.

Once everything is downloaded and extract, copy the “android” folder to the folder that is the easiest to access by the android app. The “android” folder can be renamed, for example to “ZXingBarcodeScanner”.

In Android Studio, select the “Project” view instead of “Android” view in the left hand side as below.

Now, click on the project and right click it. In the right click menu, select New -> Module.

In the “Create New Module” dialog, select “Import Gradle Project” and click “Next”.

In the dialog, click the browse button “…” and select the “ZXingBarcodeScanner” (or “Android” if it was not renamed) folder from the ZXing Master root folder.

Once “OK” is clicked, leave the module name as it is, and click “Next” and “Finish”.

Now, in the imported folder, create a “libs” folder and put the core-x.x.x.jar file you downloaded before.

Now, go to File->Project Structure. Select your project zXingBarcodeScanner, go to Dependencies. press the “+” button and locate the core-x.x.x.jar file the was copied in the libs folder.


Download CameraConfigurationUtils.java from the the following link and paste it in the zXingBarcodeScanner’s package com.google.zxing.client.android.camera.

Now, open the imported project’s gradle file and change the

apply plugin: 'com.android.application'

to

apply plugin: 'com.android.library'

so that the app knows that it is a library. In addition, remove the

applicationId "com.google.zxing.client.android"

.After this, in the ZXingBarcodeScanner project (the imported project)’s Manifest.xml file make the following modifications.

  1. In the <application> tag, remove the “android:icon” and the “android:logo” tag.
  2. Remove the <category android:name=”android.intent.category.LAUNCHER”/>

Finally, go to File->Project Structure again and in the “Modules” list, select app. Click the “+” button and select “Module dependency” and select the added module as a library.

After clicking “Ok” sync and clean the project. Then compile the project. After this you will see some errors in the imported project, like the following.

These are all errors with “switch” expressions. Double click and go to each of the errors, click the red bulb suggestion shown with the errors and select “Replace ‘switch’ with ‘if'” and fix the errors.

If you get issues with calendarResult.getStartTimeStamp() and calendarResult.getEndTimeStamp(), you can replace the core-3.x.x.jar (or core-3.3.0 as of this post) with core-3.3.1 from this link (this has not been tampered with by anybody and is a genuine jar). Add it the same way as before, and sync and compile the code again. Now the Zxing library can be used as an embedded library in your android project.

To use the added library, enter the Intents as below:

To scan:

Intent intent = new Intent(getApplicationContext(), CaptureActivity.class);
intent.setAction("com.google.zxing.client.android.SCAN");
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);

To get the result, add the OnActivityResult() method with the following:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == 0) {
      if (resultCode == RESULT_OK) {
         String contents = data.getStringExtra("SCAN_RESULT");
         Log.d("MainActivity", "contents: " + contents);
      } else if (resultCode == RESULT_CANCELED) {
         Log.d("MainActivity", "RESULT_CANCELED");
      }
   }
}

Thus, the library is completely integrated with the project.

References:

  1. ZXing Github – https://github.com/zxing/zxing
  2. Stackoverflow.com

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.