While scanning barcode in android is an old topic, some people still struggle to find a easy and reliable solution. So here is a tutorial on scanning as well as generating barcodes in android. We will use two libraries, one for scanning and one for generating. You can find the complete code of the demo app(written in Kotlin) at the this GitHub repo.

 

Scanning Barcode

To scan barcode we will use this library.

  • Add the following dependency in your app.gradle file.

  • Now you need to give camera permission to your app. Add the camera permission to your manifest file.

  • This library provides a view called ZBarScannerView which creates the camera interface with moving bar. Create an activity and change its view to ZBarScannerView.

  • Now you need to start and stop camera in onResume and onPause respectively.

  • To listen to barcode results you need to implement ZBarScannerView.ResultHandler interface, you will have to override the handleResult function. In the handle result function, fetch the data from the result object.

 

Generating Barcode

Generating barcode is even more easier than scanning. We will use this library for the purpose.

  • Add the following dependency in your app.gradle file.

  • Use the from(text) method in the QRCode object to create barcode of the given text.

  • You can also export the generated QR code to a file or have it in other formats, to find more functions and options go to the library’s GitHub README

 

The code for a compete working barcode demo app for the same is available at this GitHub Repo.

 

barcode 

One Response

Leave a Reply

Your email address will not be published.

nineteen + fourteen =