提交 2cc3c904 authored 作者: 史晓晨's avatar 史晓晨

调试人脸识别

上级 0bf39d26
package com.aliyun.face.aliyun_face_plugin package com.aliyun.face.aliyun_face_plugin
import android.content.Context
import android.os.Build
import androidx.annotation.NonNull import androidx.annotation.NonNull
import io.flutter.Log
import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import com.alipay.face.api.ZIMCallback
import com.alipay.face.api.ZIMFacade
import com.alipay.face.api.ZIMFacadeBuilder
import com.alipay.face.api.ZIMResponse
/** AliyunFacePlugin */ /** AliyunFacePlugin */
class AliyunFacePlugin: FlutterPlugin, MethodCallHandler { class AliyunFacePlugin : FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android /// The MethodChannel that will the communication between Flutter and native Android
/// ///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it /// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity /// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel private lateinit var channel: MethodChannel
private var mContext: Context? = null // Kotlin 空安全,默认 nullable
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { private val TAG = "AliyunFace"
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "aliyun_face_plugin")
channel.setMethodCallHandler(this) override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
} channel = MethodChannel(flutterPluginBinding.binaryMessenger, "aliyun_face_plugin")
channel.setMethodCallHandler(this)
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { mContext = flutterPluginBinding.applicationContext
if (call.method == "getPlatformVersion") { }
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else { override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
result.notImplemented() when (call.method) {
"getPlatformVersion" -> {
result.success("android ${Build.VERSION.RELEASE}")
}
"init" -> {
Log.d(TAG, "enter init.")
// 空安全检查:避免 mContext 为空导致崩溃
mContext?.let { ZIMFacade.install(it) } ?: run {
Log.e(TAG, "Context is null when call init")
result.error("NULL_CONTEXT", "Context is null", null)
}
}
"getMetaInfos" -> {
Log.d(TAG, "enter getMetaInfos.")
// 空安全处理
val metaInfo = mContext?.let { ZIMFacade.getMetaInfos(it) } ?: ""
result.success(metaInfo)
}
"verify" -> {
Log.d(TAG, "enter verify.")
// 处理参数:Kotlin 强类型转换 + 空安全
val params = call.arguments as? Map<String, String>
val certifyId = params?.get("certifyId")
// 校验 certifyId 非空
if (certifyId.isNullOrEmpty()) {
Log.e(TAG, "certifyId is null or empty")
result.error("INVALID_PARAM", "certifyId is null or empty", null)
return
}
// 空安全检查 Context
val context = mContext ?: run {
Log.e(TAG, "Context is null when call verify")
result.error("NULL_CONTEXT", "Context is null", null)
return
}
// 构建 ZIMFacade 并调用验证
val zimFacade = ZIMFacadeBuilder.create(context)
zimFacade.verify(certifyId, false, object : ZIMCallback {
override fun response(response: ZIMResponse?): Boolean {
// 空安全处理 response
val code = response?.code ?: -1
val reason = response?.reason ?: "unknown error"
if (response != null && code == 1000) {
Log.d(TAG, "face verify success.")
} else {
Log.e(TAG, "face verify error. code: $code, reason: $reason")
}
// 返回结果给 Flutter
result.success("$code,$reason")
return true
}
})
}
else -> {
result.notImplemented()
}
}
} }
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null) channel.setMethodCallHandler(null)
} mContext = null // 释放 Context 引用,避免内存泄漏
} }
}
\ No newline at end of file
package com.aliyun.face.aliyun_face_plugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlin.test.Test
import org.mockito.Mockito
/*
* This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation.
*
* Once you have built the plugin's example app, you can run these tests from the command
* line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or
* you can run them directly from IDEs that support JUnit such as Android Studio.
*/
internal class AliyunFacePluginTest {
@Test
fun onMethodCall_getPlatformVersion_returnsExpectedValue() {
val plugin = AliyunFacePlugin()
val call = MethodCall("getPlatformVersion", null)
val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java)
plugin.onMethodCall(call, mockResult)
Mockito.verify(mockResult).success("Android " + android.os.Build.VERSION.RELEASE)
}
}
...@@ -3,9 +3,7 @@ allprojects { ...@@ -3,9 +3,7 @@ allprojects {
google() google()
mavenCentral() mavenCentral()
flatDir { maven { url = uri("https://jitpack.io") }
dirs(project(":aliyun_face_plugin").file("libs"))
}
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论