Barcode is a method of representing data in a visual, machine-readable form. It represents data by varying parallel lines in width and distance. In other words, we can say a barcode is a way to encode information in a visual pattern that a device (a barcode scanner) can read (those black lines and white spaces). A barcode scanner can read and translate this black and white bar pattern into a line of test that your retail point of sale system can understand.
Applications of Barcode:
1. Barcodes are used to identify products in retail stores.
2. Barcodes save time because It offers fast recognition and implementation of data.
3. Barcode provides security for our data.
Formats of Barcodes:
1. Code 128
(FORMAT_CODE_128)
2. Code 39
(FORMAT_CODE_39)
3. Code 93
(FORMAT_CODE_93)
4. Codabar
(FORMAT_CODABAR)
5. EAN-13
(FORMAT_EAN_13)
6. EAN-8
(FORMAT_EAN_8)
7. ITF
(FORMAT_ITF)
8. UPC-A
(FORMAT_UPC_A)
9. UPC-E
(FORMAT_UPC_E)
10. QR Code
(FORMAT_QR_CODE)
11. PDF417
(FORMAT_PDF417)
12. Aztec
(FORMAT_AZTEC)
13. Data Matrix
(FORMAT_DATA_MATRIX)
The Steps to Execution:
Barcodes deliver automatic product identification, extremely fast recognition and implementation of data. There are following steps as below:
1. At Build.Gradle file
Add dependency of google ml kit dependencies { // Use this dependency for implementing barcode in your app implementation 'com.google.mlkit:barcode-scanning:16.1.0' }
2. In your project-level build.gradle file, Ensure that both your buildscript and allprojects sections include Google’s Maven repository.
3. Include surfaceview in the layout file.
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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=".ui.activity.BarcodeScaneerActivity"> <SurfaceView android:id="@+id/sfvCameraPreview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#40000000"/> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="300dp" android:layout_gravity="center" android:layout_margin="30dp" android:background="#73FFFFFF" android:alpha="0.35"> </androidx.appcompat.widget.LinearLayoutCompat> </FrameLayout>
4. Create an Instance of BarcodeDetector class because It will detect the barcode of any format.
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build();
By default, it detects all format of barcodes.
5. If we want to detect QRCode or any specific type of barcode then we have to set barcode format.
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this) .setBarcodeFormats(Barcode.QR_CODE) .build();
6. Create an instance of CameraSource and pass the instance of Barcode detector.
CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector) .setFacing(CameraSource.CAMERA_FACING_BACK) .setAutoFocusEnabled(true) .setRequestedPreviewSize(1024, 768) .build();
7. Register the callbacks of surfaceview and start the camera after surface created
and stop the camera on destroying the surface.
cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() { @SuppressLint("MissingPermission") @Override public void surfaceCreated(SurfaceHolder holder) { try { cameraSource.start(cameraPreview.getHolder()); } 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(); } });
8. Add callback of barcode processor for receiving barcode after scanning the barcode.
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() { @Override public void release() { } @Override public void receiveDetections(@NonNull Detector.Detections<Barcode> detections) { SparseArray<Barcode> barCodes = detections.getDetectedItems(); if (barCodes.size() > 0) { Intent intent = new Intent(); intent.putExtra(AppConstants.BARCODE, barCodes.valueAt(0)); setResult(CommonStatusCodes.SUCCESS, intent); finish(); } } });