目录

ML Kit Manual Conversion Guide

Preparations

If you want your app to support ML Kit, complete the integration preparation by referring to Preparations.

API Conversion

[h2]FirebaseVision.getInstance

Google API

To HMS API

FirebaseVision.getInstance(com.google.firebase.FirebaseApp app)

com.huawei.hms.mlsdk.MLAnalyzerFactory.getInstance (com.huawei.hms.mlsdk.common.MLApplication app)

You need to manually add code to create the parameter app. Originally, the com.google.firebase.FirebaseApp parameter is created by using FirebaseApp.getInstance() to obtain the default FirebaseApp instance or using FirebaseApp.getInstance(String appName) to obtain the FirebaseApp instance based on the app name. Now, you can use MLApplication.getInstance() to obtain the default MLApplication instance.

To HMS API sample code:

com.huawei.hms.mlsdk.common.MLApplication app = com.huawei.hms.mlsdk.common.MLApplication.getInstance("appName");
com.huawei.hms.mlsdk.MLAnalyzerFactory factory = com.huawei.hms.mlsdk.MLAnalyzerFactory.getInstance(app);

[h2]FaceDetector.finalize

Google API

To HMS API

com.google.android.gms.vision.face.FaceDetector.finalize()

com.huawei.hms.mlsdk.face.MLFaceAnalyzer.stop()

MLFaceAnalyzer does not support overriding the finalize method. The stop method can be called to free resources.

Deprecated Constants

Google API

Add HMS API

To HMS API

com.google.firebase.ml.vision.face.FirebaseVisionFace.INVALID_ID

Not provided

Not provided

com.google.firebase.ml.vision.label.FirebaseVisionImageLabeler.ON_DEVICE_AUTOML

Not provided

Not provided

Using Constants in the switch-case Statement

If the switch-case statement is used in the Google API code, convert it to the if-else statement in the Add HMS API scenario. The following is the sample code.

Google API code:

int rotation;// Set rotation.
int rotationDegree = 0;
switch (rotation) {
    case FirebaseVisionImageMetadata.ROTATION_90:
        rotationDegree = 90;
        break;
    case FirebaseVisionImageMetadata.ROTATION_180:
        rotationDegree = 180;
        break;
    case FirebaseVisionImageMetadata.ROTATION_270:
        rotationDegree = 270;
        break;
    default:
        break;
 }
Add HMS API code:
int rotation;// Set rotation.
int rotationDegree = 0;
if (rotation == ExtensionVisionImageMetadata.getROTATION_90()) {
    rotationDegree = 90;
} else if (rotation == ExtensionVisionImageMetadata.getROTATION_180()) {
    rotationDegree = 180;
} else if (rotation == ExtensionVisionImageMetadata.getROTATION_270()) {
    rotationDegree = 270;
} else {
    Log.i("rotation Degree","Unknown rotation");
}