In this article, we will read How QR Code Scanner Work in Android Studio . QR code stands for quick response code. It is 2 dimension bar code.QR code may store some data and this data can read by a Camera.
To generate your own QR Code you can go to this link.
Let’s start code
build.gradle
For the Camera and bar code Detector we will use the following dependency
1 |
implementation 'com.google.android.gms:play-services-vision:20.1.3' |
MainActivity
In MainActivity we create a button and when you click on that button you will be redirected to Scanner Acitivity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MainActivity extends AppCompatActivity { Button btnScanBarcode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnScanBarcode = findViewById(R.id.btnScanBarcode); btnScanBarcode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this, ScanBarCodeActivity.class)); } }); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/btnScanBarcode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="44dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="@string/scan_barcode" /> </RelativeLayout> |
when you click on the button you will be redirected to ScanBarCodeActivity.
ScanBarCodeActivity
In initViews() method we parse intent data that is scanned from the scanner and on the resume method, we initialized initialiseDetectorsAndSources(). When we detect any code then reciveDetection() method will be called and intent data will be displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
public class ScanBarCodeActivity extends AppCompatActivity { SurfaceView surfaceView; TextView txtBarcodeValue; private BarcodeDetector barcodeDetector; private CameraSource cameraSource; private static final int REQUEST_CAMERA_PERMISSION = 201; Button btnAction; String intentData = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scan_bar_code); initViews(); } private void initViews() { txtBarcodeValue = findViewById(R.id.txtBarcodeValue); surfaceView = findViewById(R.id.surfaceView); btnAction = findViewById(R.id.btnAction); btnAction.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (intentData.length() > 0) { startActivity(new Intent(Intent.ACTION_VIEW , Uri.parse(intentData))); } } }); } private void initialiseDetectorsAndSources() { Toast.makeText(getApplicationContext() , "Barcode scanner started" , Toast.LENGTH_SHORT).show(); barcodeDetector = new BarcodeDetector.Builder(this) .setBarcodeFormats(Barcode.ALL_FORMATS) .build(); cameraSource = new CameraSource.Builder(this , barcodeDetector) .setRequestedPreviewSize(1920 , 1080) .setAutoFocusEnabled(true) //you should add this feature .build(); surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { try { if (ActivityCompat.checkSelfPermission(ScanBarCodeActivity.this , Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { cameraSource.start(surfaceView.getHolder()); } else { ActivityCompat.requestPermissions(ScanBarCodeActivity.this , new String[]{Manifest.permission.CAMERA} , REQUEST_CAMERA_PERMISSION); } } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder , int format , int width , int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { cameraSource.stop(); } }); barcodeDetector.setProcessor(new Detector.Processor<Barcode>() { @Override public void release() { Toast.makeText(getApplicationContext() , "To prevent memory leaks barcode scanner has been stopped" , Toast.LENGTH_SHORT).show(); } @Override public void receiveDetections(Detector.Detections<Barcode> detections) { final SparseArray<Barcode> barcodes = detections.getDetectedItems(); if (barcodes.size() != 0) { txtBarcodeValue.post(new Runnable() { @Override public void run() { btnAction.setText("LAUNCH URL"); intentData = barcodes.valueAt(0).displayValue; txtBarcodeValue.setText(intentData); } }); } } }); } @Override protected void onPause() { super.onPause(); cameraSource.release(); } @Override protected void onResume() { super.onResume(); initialiseDetectorsAndSources(); } } |
activity_scan_bar_code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="10dp"> <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/btnAction" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" /> <TextView android:id="@+id/txtBarcodeValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:text="No Barcode Detected" android:textColor="@android:color/white" android:textSize="20sp" /> <Button android:id="@+id/btnAction" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="MOVE TO QR CODE" /> </RelativeLayout> |
So Here we discussed How QR Code Scanner Work in Android Studio . Thanks for reading this blog and You can get other blogs from here