Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
aliyun_face_plugin
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
aliyun_face_plugin
Commits
2cc3c904
提交
2cc3c904
authored
2月 25, 2026
作者:
史晓晨
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
调试人脸识别
上级
0bf39d26
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
92 行增加
和
53 行删除
+92
-53
AliyunFacePlugin.kt
...in/com/aliyun/face/aliyun_face_plugin/AliyunFacePlugin.kt
+91
-23
AliyunFacePluginTest.kt
...om/aliyun/face/aliyun_face_plugin/AliyunFacePluginTest.kt
+0
-27
build.gradle.kts
example/android/build.gradle.kts
+1
-3
没有找到文件。
android/src/main/kotlin/com/aliyun/face/aliyun_face_plugin/AliyunFacePlugin.kt
浏览文件 @
2cc3c904
package
com.aliyun.face.aliyun_face_plugin
import
android.content.Context
import
android.os.Build
import
androidx.annotation.NonNull
import
io.flutter.Log
import
io.flutter.embedding.engine.plugins.FlutterPlugin
import
io.flutter.plugin.common.MethodCall
import
io.flutter.plugin.common.MethodChannel
import
io.flutter.plugin.common.MethodChannel.MethodCallHandler
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 */
class
AliyunFacePlugin
:
FlutterPlugin
,
MethodCallHandler
{
/// 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
/// when the Flutter Engine is detached from the Activity
private
lateinit
var
channel
:
MethodChannel
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
)
{
if
(
call
.
method
==
"getPlatformVersion"
)
{
result
.
success
(
"Android ${android.os.Build.VERSION.RELEASE}"
)
}
else
{
result
.
notImplemented
()
class
AliyunFacePlugin
:
FlutterPlugin
,
MethodCallHandler
{
/// 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
/// when the Flutter Engine is detached from the Activity
private
lateinit
var
channel
:
MethodChannel
private
var
mContext
:
Context
?
=
null
// Kotlin 空安全,默认 nullable
private
val
TAG
=
"AliyunFace"
override
fun
onAttachedToEngine
(
@NonNull
flutterPluginBinding
:
FlutterPlugin
.
FlutterPluginBinding
)
{
channel
=
MethodChannel
(
flutterPluginBinding
.
binaryMessenger
,
"aliyun_face_plugin"
)
channel
.
setMethodCallHandler
(
this
)
mContext
=
flutterPluginBinding
.
applicationContext
}
override
fun
onMethodCall
(
@NonNull
call
:
MethodCall
,
@NonNull
result
:
Result
)
{
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
)
{
channel
.
setMethodCallHandler
(
null
)
}
}
override
fun
onDetachedFromEngine
(
@NonNull
binding
:
FlutterPlugin
.
FlutterPluginBinding
)
{
channel
.
setMethodCallHandler
(
null
)
mContext
=
null
// 释放 Context 引用,避免内存泄漏
}
}
\ No newline at end of file
android/src/test/kotlin/com/aliyun/face/aliyun_face_plugin/AliyunFacePluginTest.kt
deleted
100644 → 0
浏览文件 @
0bf39d26
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
)
}
}
example/android/build.gradle.kts
浏览文件 @
2cc3c904
...
...
@@ -3,9 +3,7 @@ allprojects {
google()
mavenCentral()
flatDir {
dirs(project(":aliyun_face_plugin").file("libs"))
}
maven { url = uri("https://jitpack.io") }
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论