Integrate Zxing Barcode Scanner Into Your Android App



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. Barcode Scanner library for Java, Android with Zxing core - softotalss/BarcodeScanner. Barcode Scanner is a library that provides easy integration of Zxing library with your android applications. But in some cases you will need that your app. “Integrate zxing barcode scanner into your Android app natively using Eclipse Damian Flannery’s Blog” was a extremely pleasant post,. Keep authoring and I will continue reading through! I appreciate it -Charline. Therefore in this Android barcode scanner library tutorial, we’re going to demonstrate the process of scanning the image of QR code Android at the click of a button using the Zxing library. Before we start this tutorial, don’t miss checking the QR scanner app developed by our app developers.

Apk
  • Related Questions & Answers
  • Selected Reading
Kotlin Apps/ApplicationsMobile Development

This example demonstrates how to implement bar code scanning in Kotlin.

Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Integrate zxing barcode scanner into your android app windows 10

Example

Step 3 − Add the following the dependency to build.gradle (Module:app)

Step 4 − Add the following code to src/MainActivity.kt

Example

Step 5 − Add the following code to androidManifest.xml

Example

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

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.

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.

Zxing Barcode Scanner Apk

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.

Step 4 Return scan data from fragment to activity:

Zxing barcode scanner apk

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.

Integrate Zxing Barcode Scanner Into Your Android App

Create a new interface in your project folder named ScanResultReceiver

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:

Integrate Zxing Barcode Scanner Into Your Android App Store

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

Integrate Zxing Barcode Scanner Into Your Android App Windows 10

Scanner

C# Zxing

Related