Flutter集成HyperSnap SDK插件hypersnapsdk_flutter的使用
Flutter集成HyperSnap SDK插件hypersnapsdk_flutter的使用
概述
HyperSnapSDK 是由 HyperVerge 提供的一个软件开发工具包,提供了扫描二维码、文档或面部的功能,并简化了所有 HyperVerge 产品的 API 集成。它还允许你根据应用程序的独特需求自定义集成。
该框架通过先进的 AI 引擎来验证身份验证流程中的图像是否为活体人类主体,还是欺诈性的照片样本。
安装 HyperSnapSDK Flutter 插件
添加依赖
在你的 Flutter 项目目录中运行以下命令以添加最新版本的 hypersnapsdk_flutter
插件的所有依赖项:
$ flutter pub add hypersnapsdk_flutter
Android 设置
- 确保最低 SDK 版本为 19 或更高。
- 启用
multidex
。 - 在
android/build.gradle
文件中添加以下内容到allprojects
函数内:
allprojects {
repositories {
...
maven {
url "https://s3.ap-south-1.amazonaws.com/hvsdk/android/releases"
}
}
}
- 同步项目。
- 在发布构建的 ProGuard 文件中添加以下行:
-dontwarn co.hyperverge.**
-keepclassmembers class * implements javax.net.ssl.SSLSocketFactory {
private javax.net.ssl.SSLSocketFactory delegate;
}
iOS 设置
- 运行
pod install
。 - 在
Info.plist
文件中添加隐私 - 相机使用描述。
使用 HyperSnapSDK Flutter 插件
导入插件
在 Dart 文件中导入 HyperSnapSDK Flutter 插件:
import 'package:hypersnapsdk_flutter/hypersnapsdk_flutter.dart';
初始化 HyperSnapSDK
使用以下代码初始化 HyperSnapSDK:
void initHyperSnapSDK() async {
// 添加 appId 和 appKey
HyperSnapSDK.initialize(
'<appId>',
'<appKey>',
Region.india,
);
}
开始文档捕获
调用 HVDocsCapture.start()
方法开始文档捕获屏幕:
startDocCapture() async {
var hvDocConfig = HVDocConfig(
shouldShowInstructionPage: true, // 显示指令页面
hvOCRAPIDetails: HVOCRAPIDetails(
ocrEndpoint: "<your-ocr-endpoint>",
documentSide: DocumentSide.front,
params: {<your-ocr-params>},
headers: {<your-ocr-headers>},
), // 执行 OCR API 调用
hvDocCaptureUIStrings: HVDocCaptureUIStrings(
docInstructionsTitle: "custom doc instruction title",
docCaptureTitle: "custom doc capture title",
), // 自定义文本
);
await HVDocsCapture.start(
hvDocConfig: hvDocConfig,
onComplete: (hvResponse, hvError) {
if (hvError != null) {
log("error code: ${hvError.errorCode}");
log("error message: ${hvError.errorMessage}");
}
if (hvResponse != null) {
if (hvResponse.imageUri != null)
log("image uri: ${hvResponse.imageUri!}");
if (hvResponse.apiResult != null)
log("api result: ${hvResponse.apiResult!.toString()}");
}
},
);
}
开始面部捕获
调用 HVFaceCapture.start()
方法开始面部捕获屏幕:
startFaceCapture() async {
HVFaceConfig hvFaceConfig = HVFaceConfig(
shouldShowInstructionPage: true, // 显示指令页面
hvFaceCaptureUIStrings: HVFaceCaptureUIStrings(
faceInstructionsTitle: "custom face instruction title",
faceCaptureTitle: "custom face capture title",
), // 自定义文本
);
await HVFaceCapture.start(
hvFaceConfig: hvFaceConfig,
onComplete: (hvResponse, hvError) {
if (hvError != null) {
log("error code: ${hvError.errorCode}");
log("error message: ${hvError.errorMessage}");
}
if (hvResponse != null) {
if (hvResponse.imageUri != null)
log("image uri: ${hvResponse.imageUri!}");
if (hvResponse.apiResult != null)
log("api result: ${hvResponse.apiResult!.toString()}");
}
},
);
}
开始条形码捕获
调用 HVBarcodeScanCapture.start()
方法开始条形码捕获屏幕:
barcodeScanCapture() async {
var hvBarcodeConfig = HVBarcodeConfig(
shouldShowInstructionPage: true, // 显示指令页面
hvBarcodeScanUIStrings: HVBarcodeScanUIStrings(
barcodeInstructionsTitle: "custom barcode instruction title",
barcodeCaptureTitle: "custom barcode capture title",
),
);
await HVBarcodeScanCapture.start(
hvBarcodeConfig: hvBarcodeConfig,
onComplete: (hvResult, hvError) {
if (hvError != null) {
log("error code : ${hvError.errorCode}");
log("error message : ${hvError.errorMessage}");
}
if (hvResult != null) {
log('barcode result: $hvResult');
}
},
);
}
示例 Demo
以下是完整的示例代码,展示了如何在 Flutter 应用中集成 HyperSnapSDK:
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:hypersnapsdk_flutter/hypersnapsdk_flutter.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 faceImageUri = "";
String docImageUri = "";
@override
void initState() {
initHyperSnapSDK();
super.initState();
}
Future<void> initHyperSnapSDK() async {
String appId = "<your-app-id>";
String appKey = "<your-app-key>";
var initStatus = await HyperSnapSDK.initialize(
appId,
appKey,
Region.india,
);
log("hypersnapsdk initialisation status : $initStatus");
}
Future<void> startUserSession() async {
bool isStarted = await HyperSnapSDK.startUserSession('<session-id>');
log("start user session status : $isStarted");
}
Future<void> endUserSession() async {
await HyperSnapSDK.endUserSession();
}
Future<void> startDocCapture() async {
await HVDocsCapture.start(
hvDocConfig: HVDocConfig(),
onComplete: (hvResponse, hvError) {
printResult(hvResponse, hvError);
setState(() {
docImageUri = hvResponse?.imageUri ?? "";
});
},
);
}
Future<void> startFaceCapture() async {
await HVFaceCapture.start(
hvFaceConfig: HVFaceConfig(),
onComplete: (hvResponse, hvError) {
printResult(hvResponse, hvError);
setState(() {
faceImageUri = hvResponse?.imageUri ?? "";
});
},
);
}
Future<void> barcodeScanCapture() async {
await HVBarcodeScanCapture.start(
hvBarcodeConfig: HVBarcodeConfig(),
onComplete: (hvResult, hvError) {
if (hvResult != null) {
log('barcode result : $hvResult');
}
if (hvError != null) {
log("error code : ${hvError.errorCode}");
log("error message : ${hvError.errorMessage}");
}
},
);
}
Future<void> ocrCall() async {
await HVNetworkHelper.makeOCRCall(
endpoint: '<your-ocr-endpoint>',
documentUri: docImageUri,
parameters: {},
headers: {},
onComplete: (hvResponse, hvError) {
printResult(hvResponse, hvError);
},
);
}
Future<void> faceMatchCall() async {
await HVNetworkHelper.makeFaceMatchCall(
endpoint: '<your-face-match-endpoint>',
faceUri: faceImageUri,
documentUri: docImageUri,
faceMatchMode: FaceMatchMode.faceId,
parameters: {},
headers: {},
onComplete: (hvResponse, hvError) {
printResult(hvResponse, hvError);
},
);
}
void printResult(HVResponse? hvResponse, HVError? hvError) {
if (hvResponse != null) {
if (hvResponse.imageUri != null)
log("image uri : ${hvResponse.imageUri!}");
if (hvResponse.apiResult != null)
log("api result : ${hvResponse.apiResult!.toString()}");
}
if (hvError != null) {
log("error code : ${hvError.errorCode}");
log("error message : ${hvError.errorMessage}");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("HyperSnap Flutter Example"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async => await startUserSession(),
child: Text("Start User Session"),
),
ElevatedButton(
onPressed: () async => await endUserSession(),
child: Text("End User Session"),
),
ElevatedButton(
onPressed: () async => await startDocCapture(),
child: const Text("Start Document Capture Flow"),
),
ElevatedButton(
onPressed: () async => await startFaceCapture(),
child: const Text("Start Face Capture Flow"),
),
ElevatedButton(
onPressed: () async => await barcodeScanCapture(),
child: const Text("Start Barcode Capture Flow"),
),
ElevatedButton(
onPressed: () async => await ocrCall(),
child: const Text("Make OCR Call"),
),
ElevatedButton(
onPressed: () async => await faceMatchCall(),
child: const Text("Make Face Match Call"),
),
],
),
),
),
);
}
}
请确保替换 <your-app-id>
、<your-app-key>
、<your-ocr-endpoint>
等占位符为你自己的实际值。希望这个指南能帮助你在 Flutter 应用中成功集成 HyperSnapSDK。
更多关于Flutter集成HyperSnap SDK插件hypersnapsdk_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter集成HyperSnap SDK插件hypersnapsdk_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中集成和使用hypersnapsdk_flutter
插件的示例代码。假设你已经按照插件的官方文档完成了基本的环境配置和依赖添加。
1. 添加依赖
首先,在你的pubspec.yaml
文件中添加hypersnapsdk_flutter
依赖:
dependencies:
flutter:
sdk: flutter
hypersnapsdk_flutter: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
2. 导入插件
在你的Dart文件中导入插件:
import 'package:hypersnapsdk_flutter/hypersnapsdk_flutter.dart';
3. 初始化插件
在应用的入口文件(通常是main.dart
)中初始化插件:
void main() {
WidgetsFlutterBinding.ensureInitialized();
HypersnapSdkFlutter.instance.init(); // 初始化插件
runApp(MyApp());
}
4. 使用插件功能
以下是一个使用hypersnapsdk_flutter
插件进行屏幕截图的示例:
import 'package:flutter/material.dart';
import 'package:hypersnapsdk_flutter/hypersnapsdk_flutter.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
HypersnapSdkFlutter.instance.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hypersnap SDK Flutter Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Press the button to take a screenshot',
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _takeScreenshot,
child: Text('Take Screenshot'),
),
],
),
),
),
);
}
void _takeScreenshot() async {
try {
// 调用插件的截图方法
var screenshot = await HypersnapSdkFlutter.instance.captureScreen();
// 将截图保存到设备
final directory = (await getApplicationDocumentsDirectory()).path;
final filePath = '$directory/screenshot.png';
await screenshot.saveToPath(filePath);
print('Screenshot saved to $filePath');
} catch (e) {
print('Error capturing screenshot: $e');
}
}
}
5. 权限处理(如果需要)
根据插件的文档,如果截图功能需要特定的权限(如访问存储),你需要在AndroidManifest.xml
和Info.plist
中声明相应的权限。
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Info.plist
在iOS项目中,你可能需要添加对照片库的访问权限,但这取决于插件的具体实现和要求。
<key>NSPhotoLibraryAddUsageDescription</key>
<string>We need access to save screenshots to your photo library.</string>
注意事项
- 确保你已经正确配置了插件所需的原生依赖和权限。
- 插件的具体API和方法可能会随着版本的更新而变化,请参考官方文档获取最新的使用指南。
- 在实际项目中,请妥善处理异常和错误,以确保应用的稳定性和用户体验。
这个示例展示了如何在Flutter项目中集成和使用hypersnapsdk_flutter
插件进行屏幕截图。根据插件的具体功能,你可以进一步扩展和自定义这个示例。