ML KIT入门-QR码检测

ML KIT入门-QR码检测

使用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 了,不需要对神经网络或模型优化有深入的了解就可以开始了。


分享到:


相關文章: