Detect Barcode Using Google Vision API

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


2. Code 39


3. Code 93


4. Codabar


5. EAN-13


6. EAN-8


7. ITF


8. UPC-A


9. UPC-E


10. QR Code


11. PDF417


12. Aztec


13. 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 ''


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=""




















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)



6. Create an instance of CameraSource and pass the instance of Barcode detector.

CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)



           .setRequestedPreviewSize(1024, 768)


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() {



       public void surfaceCreated(SurfaceHolder holder) {

           try {


           } catch (IOException e) {





       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {



       public void surfaceDestroyed(SurfaceHolder holder) {




8. Add callback of barcode processor for receiving barcode after scanning the barcode.    

barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {


       public void release() {



       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);






Leave a Reply