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.

compile 'me.dm7.barcodescanner:zbar:1.9.8' 1 compile 'me.dm7.barcodescanner:zbar:1.9.8'

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

<uses-permission android:name="android.permission.CAMERA" /> 1 < uses - permission android : name = "android.permission.CAMERA" / >

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

//Setting the main view to ZBarScannerView private lateinit var mScannerView: ZBarScannerView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mScannerView = ZBarScannerView(this) setContentView(mScannerView) } 1 2 3 4 5 6 7 8 9 //Setting the main view to ZBarScannerView private lateinit var mScannerView : ZBarScannerView override fun onCreate ( savedInstanceState : Bundle ? ) { super . onCreate ( savedInstanceState ) mScannerView = ZBarScannerView ( this ) setContentView ( mScannerView ) }

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

override fun onResume() { super.onResume() mScannerView.startCamera() } override fun onPause() { super.onPause() mScannerView.stopCamera() } 1 2 3 4 5 6 7 8 9 override fun onResume ( ) { super . onResume ( ) mScannerView . startCamera ( ) } override fun onPause ( ) { super . onPause ( ) mScannerView . stopCamera ( ) }

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.

//Implementing the ResultHandler interface and overriding handleResult function class BarcodeScanningActivity : AppCompatActivity(), ZBarScannerView.ResultHandler { ... ... override fun handleResult(result: Result?) { Toast.makeText(this, result?.contents, Toast.LENGTH_SHORT).show() //Camera will stop after scanning result, so we need to resume the //preview in order scan more codes mScannerView.resumeCameraPreview(this) } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //Implementing the ResultHandler interface and overriding handleResult function class BarcodeScanningActivity : AppCompatActivity ( ) , ZBarScannerView . ResultHandler { . . . . . . override fun handleResult ( result : Result ? ) { Toast . makeText ( this , result ? . contents , Toast . LENGTH_SHORT ) . show ( ) //Camera will stop after scanning result, so we need to resume the //preview in order scan more codes mScannerView . resumeCameraPreview ( this ) } }

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.

compile 'com.github.kenglxn.QRGen:android:2.3.0' 1 compile 'com.github.kenglxn.QRGen:android:2.3.0'

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

val bitmap = QRCode.from(text).withSize(1000, 1000).bitmap() generationImageView.setImageBitmap(bitmap) 1 2 val bitmap = QRCode . from ( text ) . withSize ( 1000 , 1000 ) . bitmap ( ) generationImageView . setImageBitmap ( bitmap )

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.