目录

ML Kit手工转换指导书

开发准备

您若需要应用支持ML Kit,必须完成接入准备,请参见开发准备。

接口转换

[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)

参数app需手动添加代码创建,对应原参数com.google.firebase.FirebaseApp的创建方法FirebaseApp.getInstance()获取默认FirebaseApp实例或使用FirebaseApp.getInstance(String appName)根据App名称获取FirebaseApp实例,可使用MLApplication.getInstance()获取默认MLApplication实例。

To HMS API代码示例:

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不支持重写finalize方法,可以调用stop方法释放资源。

废弃的常量

Google API

Add HMS API

To HMS API

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

不提供

不提供

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

不提供

不提供

switch-case语句中使用常量

如果在Google API代码中使用了switch-case语句,在Add HMS API场景下,需要将其转换为if-else语句,示例如下。

Google API代码:

int rotation;//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代码:
int rotation;//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");
}