Integrate Zxing Barcode Scanner Into Your Android App

You simply copy the Intent Integrator and Intent Result classes into your app and call on them in your own classes. To use the ZXing integration classes in your app, import them to the Activity class you want to access them in, for example the following if you use the default package names: import com.google.zxing.integration.android. ZXing Android Embedded. Barcode scanning library for Android, using ZXing for decoding. The project is loosely based on the ZXing Android Barcode Scanner application, but is not affiliated with the official ZXing project. Features: Can be used via Intents (little code required). Can be embedded in an Activity, for advanced customization of UI.

  1. Integrate Zxing Barcode Scanner Into Your Android App Windows 10
  2. Zxing Android
  3. Zxing Barcode Scanner Android Studio
  4. Zxing Barcode Scanner Apk
  5. Integrate Zxing Barcode Scanner Into Your Android App Store
  6. Zxing Barcode Scanner Examples

Lets start the new year with new tutorial. This time, I will wrote how to capture barcode / QRCode data on Android using Kotlin language. I’ll assume you already have Android Studio and related dependencies (Android SDK, Emulator (of your phone’s adb driver if you want to live test on device), Java, etc) installed. Install Barcode Scanner app first and then use android-integration and android-core for Integration.Or Integrate code using android-integration and android-core, if Barcode Scanner app is not installed then pop up would display to install the app first. Obviously, both these options depend upon the Barcode Scanner app. Put the following codes into out activityinlinescan.xml: The basic idea is to have scanner view above and scan button along with scan result view below. We will need another layout to contain the actual scanner, so create a new layout file called custombarcodelayout.xml with the following codes.

Zxing barcode scanner android studioIntegrate Zxing Barcode Scanner Into Your Android App

I have received quite a few comments on my recent posts Android Barcode Scanner , Android Aadhaar Card Scanner and Android QR Code scanner about integrating the scanner inside a fragment instead of, directly in the activity.

Integrate Zxing Barcode Scanner Into Your Android App Windows 10

So this post will cover full details of how to integrate barcode scanner into a fragment.

Step 1: create application and include dependency

We will use Zxing library for scanning the barcodes. There is an opensource project named ZXing Android Minimal which has embeded this library so we will use this project in our app.Create a blank project in android studio and add ZXing Android Minimal as dependency in your build.gradle file located in app folder.

Step 2 create scan fragment:

Create a new class in your project named ScanFragment extending Fragment.

We will use intent to launch the barcode scanner, so override the onCreate function of fragment and launch scan

The intent will require the fragment to have onActivityResult method so that scan result can be returned. Add method onActivityResult to the fragment. I have also declared two string variables codeContent and codeFormat to hold scan data and format

Step 3 Integrate fragment in activity:

After creating the scan fragment, we have to invoke this from activity. Create an activity class in your project named HomeActivity. Add a layout in the layout folder named activity_home.xml. Add a button in the layout to start the scan. Add two text views to display the scan data and barcode format. Also add a layout to add fragment programmatically.

We have added scanNow function to the button in layout, now add this function to HomeActivity. This function will create and add the fragment to the current activity.

Zxing Android

Step 4 Return scan data from fragment to activity:

The advantage of using barcode scanner in a fragment is to re-use it with any activity. However we still need a way to send scanned data back to the parent activity. We can add a public method in the activity and call this method from fragment but this will make our fragment dependent on the activity class. So to make our fragment not dependent on the activity class we will create an interface with method scanResultData. Our activity will implement this interface and override this method.

Create a new interface in your project folder named ScanResultReceiver

Zxing Barcode Scanner Android Studio

Now update HomeActivity to implement this interface and override scanResultData method. We will use the data received in this method to populate our text views.

Now update the ScanFragment to use this method and send scanned data back to activity. Updated onActivityResult method will look like:

Step 5 Handle no scan data error:

There will be a case when we will not receive scan result in onActivityResult we need to handle this case and let the parent activity decide what to do. So I have created a custom exception NoScanResultException which will be returned back to the activity. I have used method overloading and added scanResultData twice in interface ScanResultReceiver

Exception class

Lets update HomeActivity to implement the second scanResultData. Just for simplicity I am displaying a toast with the error message.

This is a very basic implementation of barcode scanner in fragment. As always the full source code can be found on my github profile under android-barcode-scanner-in-fragment

Related

Zxing Barcode Scanner Apk

Barcode and QR Code use to give a unique identity in many sectors now. People use them for ticketing system, inventory system, logistic system, etc. It is used by many people because of its simplicity. You only need a scanner to read the data in a barcode in a second time.

There are many types of scanners you can use. In fact, you can also use your Android device as a scanner and integrate it into your other systems via web service, etc.

This is an example of Android apps to read barcode and QR code with the Zxing library. First, you have to make a new project on Android Studio and then update the dependencies on your build.gradle file.

Barcode scanner zxing team

Second, make a new layout. There is only a button view on the layout which has an on click event. So when you click the button, the scan mode will active.

Third, make an activity and show the layout when created.

Last, don’t forget to modify your manifests file. You need to add camera permission and your main activity.

Barcode

Integrate Zxing Barcode Scanner Into Your Android App Store

That’s all. Compile your code and run your application.

Integrate Zxing Barcode Scanner Into Your Android App

Zxing Barcode Scanner Examples

Reference:
https://github.com/journeyapps/zxing-android-embedded

Comments are closed.