Flutter人脸识别验证插件facedapter_idv的使用
Flutter人脸识别验证插件facedapter_idv的使用
Facedapter IDV Onboarding Flutter SDK
一个提供快速、安全和准确的身份验证SDK。
示例
要运行示例项目,请克隆仓库,设置你的API密钥,然后在Example目录下运行以下命令:
cd example/
flutter run
Demo
在线演示:联系我们获取演示
功能
安全特性:
- 移动智能欺诈检测(Shield)
- 面部活体检测和照片/视频欺骗攻击检测
- 快速准确的身份证扫描
- 准确的身份证照片和自拍匹配
安全和快速:
- 身份验证SDK非常快,在不到10秒内即可完成,并提供大量数据供智能决策
更好的用户体验:
- 清晰简单的用户界面
- 无需等待,快速检测和身份验证(<10秒)
SDK集成
安装
facedapter_idv
可通过 pub.dev 获取。要安装它,请将 facedapter_idv
依赖项添加到你的 pubspec.yaml
文件中:
dependencies:
...
facedapter_idv:
执行首次身份验证
初始化SDK
首先,你需要从Facedapter仪表板获取API密钥。在视图控制器中初始化SDK时,需要传入API密钥。
class _MyAppState extends State<MyApp> {
final FacedapterIdvPlugin = FacedapterIdv();
@override
void initState() {
super.initState();
initialize();
}
Future<void> initialize() async {
await FacedapterIdvPlugin.initialize("YOUR_API_KEY");
}
// 在视图控制器中调用此方法开始身份验证。
Future<void> startOnboarding() async {
Map<dynamic, dynamic> result = {};
try {
result = await FacedapterIdvPlugin.startOnboarding() ?? {};
print(result);
} on PlatformException {
// 处理异常
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Facedapter IDV Onboarding'),
),
body: Center(
child: TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
),
onPressed: startOnboarding,
child: const Text('启动SDK'),
),
)
),
);
}
}
最大尝试次数选项
你可以通过向initialize
函数传递第二个参数来限制用户可以进行的身份验证尝试次数。
例如,使用值2
,当用户达到2次不成功的身份验证尝试后,身份验证将结束:
await FacedapterIdvPlugin.initialize("YOUR_API_KEY", 2);
如果使用了最大尝试次数选项,并且身份验证成功,它将返回预期的结果对象。但如果失败,则会返回带有值MAX_ATTEMPTS
的reasonCode
。
Future<void> startOnboarding() async {
Map<dynamic, dynamic> result = {};
try {
result = await FacedapterIdvPlugin.startOnboarding() ?? {};
print(result.reasonCode);
// 如果用户达到最大尝试次数而未成功身份验证,将返回原因码。
// 输出为:"MAX_ATTEMPTS"
} on PlatformException {
// 处理异常
}
}
平台特定实现
插件实现位于lib
文件夹中,而平台特定实现位于android
和ios
文件夹中。
Android
安装后,Android文件夹将完全初始化。
iOS
-
在添加了对
facedapter_idv
的依赖项到pubspec.yaml
后,转到NameOfYourProject/ios
并运行pod install
。 我们的facedapter_idv
依赖项将自动安装。 -
添加描述到你的
Info.plist
以支持iOS,打开NameOfYourProject/ios/Runner.xcworkspace
并在NameOfYourProject/ios/Runner/Info.plist
中设置以下键:NSCameraUsageDescription
NSMicrophoneUsageDescription
NSPhotoLibraryAddUsageDescription
NSLocationWhenInUseUsageDescription
身份验证结果
所有身份验证结果都可用作映射对象。
{
'userInfo': {
'firstName': '...',
'lastName': '...',
'email': '...',
'phoneModel': '...',
'actionDuration': 10, // 秒
'totalDuration': 20 // 秒
},
'location': {
'latitude': 1.12345678,
'longitude': 1.12345678,
'accuracy': 16.399999618530273,
'altitude': 10.899999618530273
},
'address': {
'country': 'Switzerland',
'city': 'Geneva',
'countryCode': 'CH'
},
'shield': {
'device_intelligence': {
'app_tampering': false,
'device_score': 115,
'hooking': false,
'is_emulated': false,
'is_jailbroken': false,
'running_clone_apps': false,
'running_gps_spoofers': false,
'running_vpn_spoofers': false,
'shield_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'suspicious_factory_reset': false,
'virtual_os': false
},
'platform': 'Android',
'session_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'timestamp': '1646876268',
'version': '1.1.0'
},
'actions': {
'success': true,
'images': {
'image_path',
'image_path',
'image_path'
},
'actions': {'eyes_close', 'look_up'}
},
'blinkId': {
'fullName': '...',
'age': '...',
'address': '...',
'documentNumber': '...',
'dateOfExpiry': '...',
'frontImage': 'image_path',
'backImage': 'image_path',
'faceImage': 'image_path'
},
'selfieLive': {
'result': ' live',
'liveTries': 1,
'success': true
} ,
'selfieCropping': {
'img': 'image_path',
'vector': {23.42342, 3.2342, 5.322, ...},
'success': true
},
'idCropping': {
'img': 'image_path',
'vector': {23.42342, 3.2342, 5.322, ...},
'success': true
},
'matching':{
'value': 0.531421528331689,
'matched': true
}
}
不成功的身份验证结果
{
'message': '.......',
'reasonCode': 'MAX_ATTEMPTS, // one of 'MAX_ATTEMPTS', 'INTERNAL_ERROR'
}
更多关于Flutter人脸识别验证插件facedapter_idv的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter人脸识别验证插件facedapter_idv的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
facedapter_idv
是一个用于 Flutter 的人脸识别验证插件,它可以帮助开发者在应用中集成人脸识别功能。以下是如何使用 facedapter_idv
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 facedapter_idv
插件的依赖:
dependencies:
flutter:
sdk: flutter
facedapter_idv: ^1.0.0 # 请根据实际情况使用最新版本
然后,运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入 facedapter_idv
插件:
import 'package:facedapter_idv/facedapter_idv.dart';
3. 初始化插件
在使用插件之前,你需要进行初始化。通常你可以在 main.dart
中进行初始化:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FacedapterIdv.initialize();
runApp(MyApp());
}
4. 使用人脸识别功能
你可以使用 FacedapterIdv
提供的 API 来进行人脸识别验证。以下是一个简单的示例:
class FaceRecognitionScreen extends StatefulWidget {
[@override](/user/override)
_FaceRecognitionScreenState createState() => _FaceRecognitionScreenState();
}
class _FaceRecognitionScreenState extends State<FaceRecognitionScreen> {
String _verificationResult = '';
Future<void> _startFaceVerification() async {
try {
bool isVerified = await FacedapterIdv.verifyFace();
setState(() {
_verificationResult = isVerified ? 'Verification Successful' : 'Verification Failed';
});
} catch (e) {
setState(() {
_verificationResult = 'Error: $e';
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Face Recognition'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _startFaceVerification,
child: Text('Start Face Verification'),
),
SizedBox(height: 20),
Text(_verificationResult),
],
),
),
);
}
}
5. 处理权限
人脸识别通常需要访问摄像头,因此你需要在 AndroidManifest.xml
和 Info.plist
中添加相应的权限。
Android (AndroidManifest.xml
):
<uses-permission android:name="android.permission.CAMERA" />
iOS (Info.plist
):
<key>NSCameraUsageDescription</key>
<string>We need access to your camera for face verification.</string>
6. 运行应用
确保你已经正确配置了所有内容,然后运行你的 Flutter 应用。你应该能够启动人脸识别验证并查看结果。
7. 处理错误和异常
在实际使用中,可能会遇到各种错误和异常,例如权限问题、设备不支持等。确保你正确处理这些情况,并给用户提供适当的反馈。
8. 进一步定制
facedapter_idv
插件可能还提供了更多的 API 和配置选项,你可以根据需要进行进一步定制。查看插件的官方文档以了解更多详细信息。
示例代码
以下是一个完整的示例代码,展示了如何使用 facedapter_idv
插件进行人脸识别验证:
import 'package:flutter/material.dart';
import 'package:facedapter_idv/facedapter_idv.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FacedapterIdv.initialize();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Face Recognition Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FaceRecognitionScreen(),
);
}
}
class FaceRecognitionScreen extends StatefulWidget {
[@override](/user/override)
_FaceRecognitionScreenState createState() => _FaceRecognitionScreenState();
}
class _FaceRecognitionScreenState extends State<FaceRecognitionScreen> {
String _verificationResult = '';
Future<void> _startFaceVerification() async {
try {
bool isVerified = await FacedapterIdv.verifyFace();
setState(() {
_verificationResult = isVerified ? 'Verification Successful' : 'Verification Failed';
});
} catch (e) {
setState(() {
_verificationResult = 'Error: $e';
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Face Recognition'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _startFaceVerification,
child: Text('Start Face Verification'),
),
SizedBox(height: 20),
Text(_verificationResult),
],
),
),
);
}
}