Flutter身份识别插件gts_myid的使用
gts_myid
GTS-MyID SDK flutter插件。
目录
Android SDK: 1.1.1
iOS SDK: 1.0.3
项目调整
iOS
更新你的iOS配置文件
将ios/Podfile
更改为使用版本12:
platform :ios, '13.0'
在ios/YourProjectName/Info.plist
中添加相机权限描述:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- 添加以下两个元素: -->
<key>NSCameraUsageDescription</key>
<string>Required for document and facial capture</string>
<!-- ... -->
</dict>
</plist>
使用
var result = await GtsClient.start(
config: GtsConfig(
locale: GtsLocale.english
),
);
参数详情:
方法 | 备注 | 默认值 |
---|---|---|
locale |
设置特定的语言环境 | GtsLocale.kyrgyz |
SDK错误码
调用SDK时可能会出现以下错误码。以下是参考列表。
代码 | 错误信息 |
---|---|
101 | 预期外的错误 |
102 | 拒绝访问相机 |
示例代码
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:gts_myid/enums.dart';
import 'package:gts_myid/gts_myid.dart';
import 'package:gts_myid/gts_myid_config.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _error;
GtsResult? _result;
Future<void> init() async {
String? error;
GtsResult? result;
try {
final gtsResult = await GtsClient.start(config: GtsConfig(
locale: GtsLocale.kyrgyz
));
error = null;
result = gtsResult;
} catch (e) {
error = e.toString();
result = null;
}
// 如果在异步平台消息传输期间小部件从树中移除,则我们希望丢弃回复而不是调用setState来更新我们的不存在的外观。
if (!mounted) return;
setState(() {
_error = error;
_result = result;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Gts-MyID Sample'),
),
body: Center(
child: Column(
children: [
MaterialButton(
onPressed: init,
child: const Text('Start SDK'),
),
Text(_result?.imageBase64?.substring(0, 100) ?? _error ?? 'Failure'),
],
),
),
),
);
}
}
更多关于Flutter身份识别插件gts_myid的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter身份识别插件gts_myid的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
gts_myid
是一个Flutter插件,用于在移动应用中进行身份识别。它通常用于集成第三方身份识别服务,如身份证识别、人脸识别等。以下是使用 gts_myid
插件的基本步骤和示例代码。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 gts_myid
插件的依赖。
dependencies:
flutter:
sdk: flutter
gts_myid: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入 gts_myid
插件。
import 'package:gts_myid/gts_myid.dart';
3. 初始化插件
在使用插件之前,通常需要先进行初始化。你可以通过调用 GtsMyId.init()
方法来完成初始化。
void initMyId() async {
bool isInitialized = await GtsMyId.init();
if (isInitialized) {
print("GtsMyId initialized successfully");
} else {
print("Failed to initialize GtsMyId");
}
}
4. 进行身份识别
你可以使用 GtsMyId.recognize()
方法来进行身份识别。这个方法通常需要传入一些参数,比如身份证图片的路径。
void recognizeIdCard(String imagePath) async {
Map<String, dynamic> result = await GtsMyId.recognize(imagePath);
if (result['success']) {
print("Recognition successful: ${result['data']}");
} else {
print("Recognition failed: ${result['error']}");
}
}
5. 处理识别结果
识别结果通常是一个包含识别信息的 Map
。你可以根据业务需求来处理这些信息。
void handleRecognitionResult(Map<String, dynamic> result) {
if (result['success']) {
String name = result['data']['name'];
String idNumber = result['data']['idNumber'];
print("Name: $name, ID Number: $idNumber");
} else {
print("Error: ${result['error']}");
}
}
6. 释放资源
在使用完插件后,建议调用 GtsMyId.release()
方法来释放资源。
void releaseMyId() async {
await GtsMyId.release();
print("GtsMyId resources released");
}
7. 示例代码
以下是一个完整的示例代码,展示了如何使用 gts_myid
插件进行身份识别。
import 'package:flutter/material.dart';
import 'package:gts_myid/gts_myid.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GtsMyId Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
await initMyId();
String imagePath = "path_to_your_id_card_image.jpg";
Map<String, dynamic> result = await GtsMyId.recognize(imagePath);
handleRecognitionResult(result);
await releaseMyId();
},
child: Text('Recognize ID Card'),
),
),
),
);
}
}
void initMyId() async {
bool isInitialized = await GtsMyId.init();
if (isInitialized) {
print("GtsMyId initialized successfully");
} else {
print("Failed to initialize GtsMyId");
}
}
void handleRecognitionResult(Map<String, dynamic> result) {
if (result['success']) {
String name = result['data']['name'];
String idNumber = result['data']['idNumber'];
print("Name: $name, ID Number: $idNumber");
} else {
print("Error: ${result['error']}");
}
}
void releaseMyId() async {
await GtsMyId.release();
print("GtsMyId resources released");
}