In scan barcode and QRcode using zxing Android Kotlin example, we will learn how to scan the barcode and QRcode using a zxing library in Android studio.
We need to implement scan feature when developing apps for ecommerce or grocery stores.
First of all check output of this scan barcode and QRcode using zxing android kotlin tutorial then we will implement it programmatically.
Creating scan barcode and qrcode using zxing android kotlin Example
Step 1: Create a new project in Android Studio.
Step 2: Updating build.gradle(Module:app) file
add following code into dependencies{}
compile 'me.dm7.barcodescanner:zxing:1.9'
Here we are using ZXing library. You can also use ZBar library.
Github link for ZBar or ZXing library: ZXing or ZBar
Step 3: Updating AndroidManifest.xml file
<uses-permission android:name="android.permission.CAMERA" />
Note: If you are targeting SDK version above 22 (Above Lollipop) then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.
Step 4: Updating activity_main.xml file:
Copy following code into android_main.xml file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.parsaniahardik.kotlin_scanxing.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="Scan Barcode or QR code" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvresult" android:textColor="#000" android:layout_marginTop="10dp" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Result will be here"/> </LinearLayout>
Step 5: Updating MainActivity.kt file:
Update MainActivity.kt as per below code:
import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.TextView class MainActivity : AppCompatActivity() { private var btn: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) tvresult = findViewById(R.id.tvresult) as TextView btn = findViewById(R.id.btn) as Button btn!!.setOnClickListener { val intent = Intent(this@MainActivity, ScanActivity::class.java) startActivity(intent) } } companion object { var tvresult: TextView? = null } }
Here we will open ScanActivity on the button’s click.
Step 6: Opening new activity in android studio
To open new activity in the android studio, click on File tab which is present at the left top bar.
Now go to File->New->Activity->Empty Activity and place name of activity as ScanActivity.
Step 7: Updating ScanActivity.kt file:
Add below code to ScanActivity.kt
import android.os.Bundle import android.support.v7.app.AppCompatActivity import com.google.zxing.Result import me.dm7.barcodescanner.zxing.ZXingScannerView /** * Created by Parsania Hardik on 19-Mar-18. */ class ScanActivity : AppCompatActivity(), ZXingScannerView.ResultHandler { private var mScannerView: ZXingScannerView? = null public override fun onCreate(state: Bundle?) { super.onCreate(state) mScannerView = ZXingScannerView(this) // Programmatically initialize the scanner view setContentView(mScannerView) // Set the scanner view as the content view } public override fun onResume() { super.onResume() mScannerView!!.setResultHandler(this) // Register ourselves as a handler for scan results. mScannerView!!.startCamera() // Start camera on resume } public override fun onPause() { super.onPause() mScannerView!!.stopCamera() // Stop camera on pause } override fun handleResult(rawResult: Result) { // Do something with the result here // Log.v("tag", rawResult.getText()); // Prints scan results // Log.v("tag", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.) MainActivity.tvresult!!.setText(rawResult.text) onBackPressed() // If you would like to resume scanning, call this method below: //mScannerView.resumeCameraPreview(this); } }
Note: We do not need to update activity_scan.xml file because we will open camera preview here so no need to do anything in layout file.
Step 8: Brief Description of ScanActivity.java
- In onCreate() method, we have set the view as scannerview instead of xml file layout.
- In onResume() method, we have set the resulthandler and started camera preview.
- In onPause() method, we are stopping camera preview.
Step 9: Explaining handleResult() method
override fun handleResult(rawResult: Result) { // Do something with the result here // Log.v("tag", rawResult.getText()); // Prints scan results // Log.v("tag", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.) MainActivity.tvresult!!.setText(rawResult.text) onBackPressed() // If you would like to resume scanning, call this method below: //mScannerView.resumeCameraPreview(this); }
- As you can see in above code, we get result object which contains resulting Barcode or QRcode after scanning is completed.
- We will get final result by running result.getText() method and we have set it into TextView of MainActivity.
Java Version
Java version of this tutorial : Android Scan Barcode QRCode ZXing
So it was all about scan barcode and qrcode using zxing android kotlin example programmatically. Thank you.
Download Source Code
[sociallocker]Download Kotlin_ScanXing Example[/sociallocker]