![ML KIT入門-QR碼檢測](http://p2.ttnews.xyz/loading.gif)
使用ML KIT檢測條形碼/ Qr代碼
Firebase啟動了ML工具包來識別和解碼條形碼。本文將幫助您使用ML工具包集成條形碼檢測。
1.配置Build.gradle
dependencies {
implementation 'com.google.firebase:firebase-ml-vision:16.0.0'
}
2.更新Manifest 文件
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="barcode" />
3.配置Bar-code檢測器
如果您知道您希望讀取哪種條形碼格式,可以通過將其配置為僅檢測這些格式來提高條形碼檢測器的速度。
FirebaseVisionBarcodeDetectorOptions options =
new FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(
FirebaseVisionBarcode.FORMAT_QR_CODE,
FirebaseVisionBarcode.FORMAT_AZTEC)
.build();
支持以下格式:
- Code 128 (FORMAT_CODE_128)
- Code 39 (FORMAT_CODE_39)
- Code 93 (FORMAT_CODE_93)
- Codabar (FORMAT_CODABAR)
- EAN-13 (FORMAT_EAN_13)
- EAN-8 (FORMAT_EAN_8)
- ITF (FORMAT_ITF)
- UPC-A (FORMAT_UPC_A)
- UPC-E (FORMAT_UPC_E)
- QR Code (FORMAT_QR_CODE)
- PDF417 (FORMAT_PDF417)
- Aztec (FORMAT_AZTEC)
- Data Matrix (FORMAT_DATA_MATRIX)
4. 從圖像中創建一個FirebaseVisionImage對象。
要從Bitmap/ByteBuffer/ByteArray/File創建一個FirebaseVisionImage對象:
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata);
FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
FirebaseVisionImage image;
try {
image = FirebaseVisionImage.fromFilePath(context, uri);
} catch (IOException e) {
e.printStackTrace();
}
要從ByteBuffer或字節數組創建一個FirebaseVisionImage對象,首先要計算如下所述的圖像旋轉
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
/**
* Get the angle by which an image must be rotated given the device's current
* orientation.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private int getRotationCompensation(String cameraId, Activity activity, Context context)
throws CameraAccessException {
// Get the device's current rotation relative to its "native" orientation.
// Then, from the ORIENTATIONS table, look up the angle the image must be
// rotated to compensate for the device's rotation.
int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int rotationCompensation = ORIENTATIONS.get(deviceRotation);
// On most devices, the sensor orientation is 90 degrees, but for some
// devices it is 270 degrees. For devices with a sensor orientation of
// 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.
CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
int sensorOrientation = cameraManager
.getCameraCharacteristics(cameraId)
.get(CameraCharacteristics.SENSOR_ORIENTATION);
rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360;
// Return the corresponding FirebaseVisionImageMetadata rotation value.
int result;
switch (rotationCompensation) {
case 0:
result = FirebaseVisionImageMetadata.ROTATION_0;
break;
case 90:
result = FirebaseVisionImageMetadata.ROTATION_90;
break;
case 180:
result = FirebaseVisionImageMetadata.ROTATION_180;
break;
case 270:
result = FirebaseVisionImageMetadata.ROTATION_270;
break;
default:
result = FirebaseVisionImageMetadata.ROTATION_0;
Log.e(TAG, "Bad rotation value: " + rotationCompensation);
}
return result;
}
創建一個FirebaseVisionImageMetadata對象,該對象包含圖像的高度、寬度、顏色編碼格式和旋轉:
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
.setWidth(1280)
.setHeight(720)
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setRotation(rotation)
.build();
5.獲取一個FirebaseVisionBarcodeDetector實例:
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
.getVisionBarcodeDetector();
// Or, to specify the formats to recognize:
// FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
// .getVisionBarcodeDetector(options);
6.最後,將圖像傳遞給detectInImage方法:
Task
> result = detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener
>() {
@Override
public void onSuccess(List
barcodes) { // Task completed successfully
// ...
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
}
});
7、檢測到的條形碼信息:
如果條碼檢測器能夠確定由條碼編碼的數據類型,則可以獲得包含解析數據的對象
for (FirebaseVisionBarcode barcode: barcodes) {
Rect bounds = barcode.getBoundingBox();
Point[] corners = barcode.getCornerPoints();
String rawValue = barcode.getRawValue();
int valueType = barcode.getValueType();
// See API reference for complete list of supported types
switch (valueType) {
case FirebaseVisionBarcode.TYPE_WIFI:
String ssid = barcode.getWifi().getSsid();
String password = barcode.getWifi().getPassword();
int type = barcode.getWifi().getEncryptionType();
break;
case FirebaseVisionBarcode.TYPE_URL:
String title = barcode.getUrl().getTitle();
String url = barcode.getUrl().getUrl();
break;
}
}
就是這樣,你已經準備好使用ML VISION掃描Bar-Codes/QR Codes/Data Matrix codes 了,不需要對神經網絡或模型優化有深入的瞭解就可以開始了。
閱讀更多 不靠譜的貓 的文章