Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Last active November 9, 2018 17:48
Show Gist options
  • Save jaisonfdo/22d8aa2fb7aadbb3a8cd431d55e0b936 to your computer and use it in GitHub Desktop.
Save jaisonfdo/22d8aa2fb7aadbb3a8cd431d55e0b936 to your computer and use it in GitHub Desktop.
MlKit demo sample code snippets
implementation 'com.google.firebase:firebase-ml-vision:18.0.1'
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="barcode" />
// To initialise the detector
FirebaseVisionBarcodeDetectorOptions options =
new FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
.build();
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
.getVisionBarcodeDetector(options);
// // To create the FirebaseVisionImage
FirebaseVisionImageMetadata metadata =
new FirebaseVisionImageMetadata.Builder()
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setWidth(frameMetadata.getWidth())
.setHeight(frameMetadata.getHeight())
.setRotation(frameMetadata.getRotation())
.build();
Bitmap bitmap = BitmapUtils.getBitmap(data, frameMetadata);
FirebaseVisionImage firebaseVisionImage=FirebaseVisionImage.fromByteBuffer(data, metadata);
// To connect the camera resource with the detector
mCameraSource = new CameraSource(this, barcodeOverlay);
mCameraSource.setFacing(CameraSource.CAMERA_FACING_BACK);
barcodeScanningProcessor = new BarcodeScanningProcessor(detector);
barcodeScanningProcessor.setBarcodeResultListener(getBarcodeResultListener());
mCameraSource.setMachineLearningFrameProcessor(barcodeScanningProcessor);
// To detect the barcodes from the image
@Override
protected Task<List<FirebaseVisionBarcode>> detectInImage(FirebaseVisionImage image) {
return detector.detectInImage(image);
}
// To decode the results
BarcodeResultListener barcodeResultListener = new BarcodeResultListener() {
@Override
public void onSuccess(@Nullable Bitmap originalCameraImage, @NonNull List<FirebaseVisionBarcode> barcodes,
@NonNull FrameMetadata frameMetadata, @NonNull GraphicOverlay graphicOverlay) {
Log.d(TAG, "onSuccess: " + barcodes.size());
for (FirebaseVisionBarcode barCode : barcodes)
{
// To get the raw response from the result
Log.d(TAG, "onSuccess: " + barCode.getRawValue());
Log.d(TAG, "onSuccess: " + barCode.getFormat());
Log.d(TAG, "onSuccess: " + barCode.getValueType());
// To get the parsed data
FirebaseVisionBarcode.ContactInfo contactInfo = barCode.getContactInfo();
Log.d(TAG, "Title: "+contactInfo.getTitle());
Log.d(TAG, "Phone number: "+contactInfo.getPhones().get(0).getNumber());
Log.d(TAG, "Email: "+contactInfo.getEmails().get(0).getAddress());
Log.d(TAG, "Address: "+contactInfo.getAddresses().get(0).getAddressLines()[0]);
String[] urlList = contactInfo.getUrls();
Log.d(TAG, "Web: " + urlList[0]);
}
}
@Override
public void onFailure(@NonNull Exception e) {
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment