Flutter活体检测插件intp_flutter_liveness_sdk的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter活体检测插件intp_flutter_liveness_sdk的使用

平台支持

Android iOS
✔️ ✔️

安装

iOS

ios/Runner/Info.plist文件中添加三行:

  • 一个键为Privacy - Camera Usage Description,并附上使用说明。
  • 一个键为Privacy - Microphone Usage Description,并附上使用说明。

以XML格式编辑Info.plist文件,添加以下内容:

  <key>NSCameraUsageDescription</key>
  <string>Camera Access</string>
  <key>NSMicrophoneUsageDescription</key>
  <string>Microphone Access</string>

Android

android/app/build.gradle文件中将最低Android SDK版本修改为21或更高。

minSdkVersion 21

使用

你可以使用LivenessCamera SDK来执行活体检测步骤,这适用于Android和iOS平台。

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intp_flutter_liveness_sdk/intp_flutter_liveness_sdk.dart';

Future<void> main() async {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Liveness detection'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<String?> buildDialog(Map<String, dynamic> value) {
    debugPrint("_buildDialog: $value");

    String content = "Detection failed !!!";

    if (value['Liveness'] != null && value['Liveness']['detectSuccess']) {
      content = "Detection success !!!";
    }

    if (value['statusCode'] != null && value['statusCode'] != 200) {
      content = "Error : $value";
    }

    return showDialog<String>(
      context: context,
      builder: (BuildContext context) => AlertDialog(
        title: const Text('Result'),
        content: Text(content),
        actions: <Widget>[
          TextButton(
            onPressed: () => Navigator.pop(context, 'OK'),
            child: const Text('OK'),
          ),
        ],
      ),
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SafeArea(
        child: LivenessCamera(
          livenessResponse: (Map<String, dynamic> value) {
            // 处理下一步事件或导航
            buildDialog(value);
          },
          endpoint: "API_ENDPOINT",
          apiKey: "API_KEY",
          transactionId: "TRANSACTION_ID",
          // actionsInstruction: const {
          //   'SHAKE_LEFT': 'New caption turn your head left',
          //   'SHAKE_RIGHT': 'New caption turn your head right',
          //   'NOD_HEAD': "New caption nod your head",
          //   'MOUTH': "New caption open your mouth",
          // },
          // numberOfActions: 2,
          // numberOfRetry: 1,
          // backgroundColor: Colors.red,
          // fontFamily: "Kanit-Regular", /* 加载字体来自assets */
        ),
      ),
    );
  }
}

更多关于Flutter活体检测插件intp_flutter_liveness_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter活体检测插件intp_flutter_liveness_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


intp_flutter_liveness_sdk 是一个用于Flutter应用的活体检测插件,通常用于验证用户的真实性,防止欺诈行为。以下是使用该插件的基本步骤和示例代码。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 intp_flutter_liveness_sdk 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  intp_flutter_liveness_sdk: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 导入插件

在你的Dart文件中导入插件:

import 'package:intp_flutter_liveness_sdk/intp_flutter_liveness_sdk.dart';

3. 初始化SDK

在使用活体检测之前,通常需要初始化SDK。你可以通过调用 initialize 方法来完成初始化。

void initializeSDK() async {
  try {
    await IntpFlutterLivenessSdk.initialize(
      apiKey: 'your_api_key',
      apiSecret: 'your_api_secret',
    );
    print('SDK initialized successfully');
  } catch (e) {
    print('Failed to initialize SDK: $e');
  }
}

4. 启动活体检测

初始化成功后,你可以调用 startLivenessDetection 方法来启动活体检测。

void startLivenessDetection() async {
  try {
    final result = await IntpFlutterLivenessSdk.startLivenessDetection();
    if (result['success']) {
      print('Liveness detection successful: ${result['data']}');
    } else {
      print('Liveness detection failed: ${result['error']}');
    }
  } catch (e) {
    print('Error during liveness detection: $e');
  }
}

5. 处理结果

startLivenessDetection 方法会返回一个包含检测结果的 Map。你可以根据 success 字段来判断检测是否成功,并从 data 字段中获取检测数据。

6. 配置选项(可选)

你可以根据需要配置活体检测的行为。例如,设置语言、超时时间等。

void configureSDK() async {
  try {
    await IntpFlutterLivenessSdk.configure(
      language: 'en',  // 设置语言为英文
      timeout: 30000,  // 设置超时时间为30秒
    );
    print('SDK configured successfully');
  } catch (e) {
    print('Failed to configure SDK: $e');
  }
}

7. 示例代码

以下是一个完整的示例代码,展示了如何初始化、配置和启动活体检测。

import 'package:flutter/material.dart';
import 'package:intp_flutter_liveness_sdk/intp_flutter_liveness_sdk.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Liveness Detection Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              await initializeSDK();
              await configureSDK();
              await startLivenessDetection();
            },
            child: Text('Start Liveness Detection'),
          ),
        ),
      ),
    );
  }
}

void initializeSDK() async {
  try {
    await IntpFlutterLivenessSdk.initialize(
      apiKey: 'your_api_key',
      apiSecret: 'your_api_secret',
    );
    print('SDK initialized successfully');
  } catch (e) {
    print('Failed to initialize SDK: $e');
  }
}

void configureSDK() async {
  try {
    await IntpFlutterLivenessSdk.configure(
      language: 'en',
      timeout: 30000,
    );
    print('SDK configured successfully');
  } catch (e) {
    print('Failed to configure SDK: $e');
  }
}

void startLivenessDetection() async {
  try {
    final result = await IntpFlutterLivenessSdk.startLivenessDetection();
    if (result['success']) {
      print('Liveness detection successful: ${result['data']}');
    } else {
      print('Liveness detection failed: ${result['error']}');
    }
  } catch (e) {
    print('Error during liveness detection: $e');
  }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!