Mobile Vision APIs which provide bar code detection APIs that read and decode different bar code types, faster, easily and on device.
Bar-code Formats is here,
- Configure Build.gradle
Add a dependency for play services like this — get latest version from here Google APIs.
dependencies {
implementation 'com.google.android.gms:play-services-vision:15.0.2'
}
2.Setup the Bar-code Detector
look for QR codes and Data Matrices by setting BarcodeFormat.
// Create your new BarcodeDetector using a builder, and tell it
// to look for QR codes and Data Matrices
BarcodeDetector detector =
new BarcodeDetector.Builder(getApplicationContext())
.setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE).build();
//check if our detector is operational before we use it.
if (!detector.isOperational()) {
Toast.makeText(getApplicationContext(),
"Could not set up the detector!", Toast.LENGTH_SHORT).show();
return;
}
3.Detect Bar-code
Build frame using ByteBuffer or Bitmaps, detector returns a SparseArray of barcodes.
Frame frame = new Frame.Builder()
.setImageData(yourByteBufferData, size.width,
size.height, camFormat)
.build();
Frame frame = new Frame.Builder().setBitmap(yourBitmap).build();
SparseArray<Barcode> barcodes = detector.detect(frame);
Barcode barcode = barcodes.valueAt(0);Textview.setText(barcode.displayValue);
and that’s it — it’s that simple and easy to code and deploy!
Go ahead and explore Firebase ML KIT for Bar-Code detection, HERE.