Flutter保险卡扫描插件insurance_card_scanner的使用
Flutter保险卡扫描插件insurance_card_scanner的使用
我们的CardScanner
Flutter小部件使得在任何Flutter应用程序中添加保险卡扫描功能变得非常简单,只需5分钟或更短的时间即可完成。
兼容性
Android | iOS | |
---|---|---|
支持 | 5.0+ | 12.0+ |
安装
将insurance_card_scanner
包添加到您的pubspec.yaml
文件中。
dependencies:
flutter:
sdk: flutter
insurance_card_scanner: ^1.1.0
或者,运行以下命令:
flutter pub add insurance_card_scanner
使用
在项目文件中导入库小部件:
import 'package:insurance_card_scanner/insurance_card_scanner.dart';
您可以选择性地添加API客户端以进行更多定制应用:
import 'package:insurance_card_scanner/insurance_card_scanner_api.dart';
此包依赖于CardScan AI进行保险卡的扫描和数据捕获。有关包设置和使用的完整理解,您可以参考包文档。
对于配置保险卡扫描器的步骤列表,请参阅示例应用README。
开始使用
Android相机权限
在您的AndroidManifest.xml
文件中添加以下行:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIDEO_CAPTURE" />
iOS相机权限
在您的Info.plist
文件中添加以下行:
<key>NSCameraUsageDescription</key>
<string>您的描述,说明为什么需要访问相机</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
沙箱API密钥
在仪表板上创建一个免费账户以生成沙箱API密钥。
基本示例
CardScanner小部件
可以在自定义屏幕上像其他小部件一样使用它:
import 'package:insurance_card_scanner/insurance_card_scanner.dart';
class ScannerWidgetScreen extends StatelessWidget {
const ScannerWidgetScreen({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Scanner widget'),
),
body: CardScanner(
properties: CardScanConfig(
sessionToken: '<pass your token here>',
onSuccess: (card) {
print('Scan success');
},
onError: (message) {
print(message ?? 'Unknown Scan error');
},
onCancel: () {
Navigator.of(context).pop();
},
),
),
);
}
}
CardScannerModal小部件
通过Navigator推送以打开全屏扫描器:
Navigator.of(context).push(
CardScannerModal(
properties: CardScanConfig(
sessionToken: 'pass your token here',
onSuccess: (card) {
print('Scan success');
},
onError: (message) {
print(message ?? 'Scan error');
},
onCancel: () {
Navigator.of(context).pop();
},
),
),
);
可用属性
CardScanner
和 CardScannerModal
需要传递一个 CardScanConfig
实例,包含服务器连接、回调处理和UI定制的属性。
CardScanConfig(
// 必需
sessionToken: token,
onSuccess: onSuccess,
// 推荐
onCancel: onCancel,
onError: onError,
live: live,
// 可选
backsideSupport: scanBackside,
onRetry: onRetry,
onWebViewLoadError: onWebViewLoadError,
// UI 自定义
messages: messages,
messageStyle: messagesStyle,
autoSwitchActiveColor: autoSwitchActiveColor,
autoSwitchInactiveColor: autoSwitchInactiveColor,
progressBarColor: progressBarColor,
widgetBackgroundColor: widgetBackgroundColor,
)
认证
CardScan支持两种不同用途的认证:
- 服务器到服务器 - 后端系统、管理门户等。
- 最终用户 - 最可能是移动设备或网页上的患者或临床医生。
有关认证和生成JSON Web Token (JWT) 的详细信息,请查看认证文档。
官方文档
对于包设置和使用的完整理解,您可以参考包文档。
示例代码
import 'package:flutter/material.dart';
import 'package:insurance_card_scanner/insurance_card_scanner.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
// 设置有效的会话令牌
const token = String.fromEnvironment("CS_API_KEY", defaultValue: "");
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Card Scanner Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MainScreen(),
);
}
}
class MainScreen extends StatelessWidget {
const MainScreen({super.key});
void showWidget(BuildContext context) => Navigator.of(context).push(
MaterialPageRoute(
builder: (ctx) => const ScannerWidgetScreen(),
),
);
void showModal(BuildContext context) => Navigator.of(context).push(
CardScannerModal(
properties: CardScanConfig(
logging: CardScanLogLevel.verbose,
sessionToken: token,
live: false,
onSuccess: (card) {
debugPrint("Scanned card: $card");
showMessage(context, 'Scan success');
},
onError: (message) {
showMessage(context, message.toString() ?? 'Scan failed');
},
onWebViewLoadError: () {
showMessage(context, 'WebView failed to load');
},
onCancel: Navigator.of(context).pop,
autoSwitchActiveColor: Colors.purple,
progressBarColor: Colors.purple,
widgetBackgroundColor: Colors.purple,
backsideSupport: true,
messageStyle: const TextStyle(
fontSize: 70,
color: Color.fromARGB(255, 62, 176, 39),
backgroundColor: Colors.white,
),
messages: const CardScanMessages(
autoCaptureTitle: 'autoCaptureTitle',
cameraErrorTitle: 'cameraErrorTitle',
completedTitle: 'completedTitle',
errorTitle: 'errorTitle',
frontSideCompletedTitle: 'frontsideCompletedTitle',
manualCaptureTitle: 'manualCaptureTitle',
processingTitle: 'processingTitle',
retryTitle: 'retryTitle',
),
),
),
);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Card Scanner Example'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
MaterialButton(
onPressed: () => showWidget(context),
color: Colors.blue,
textColor: Colors.white,
child: const Text('Show widget'),
),
const SizedBox(height: 40),
MaterialButton(
onPressed: () => showModal(context),
color: Colors.green,
textColor: Colors.white,
child: const Text('Show modal'),
)
],
),
),
);
}
}
class ScannerWidgetScreen extends StatelessWidget {
const ScannerWidgetScreen({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Scanner widget'),
),
body: CardScanner(
properties: CardScanConfig(
logging: CardScanLogLevel.off,
eligibility: const EligibilityRequest(
provider: Provider(
firstName: "TestProviderFirstName",
lastName: "TestProviderLastName",
npi: "123456789"), // 对于有效的NPI,需要在9后面加3
subscriber: Subscriber(
firstName: "Walter",
lastName: "Witman",
dateOfBirth: "19020403")),
onEligibilitySuccess: (eligibility) {
showMessage(context, 'Eligibility success');
debugPrint("Eligibility sucess: $eligibility");
},
onEligibilityError: (error) {
showMessage(context, 'Eligibility error');
debugPrint("Eligibility error: $error");
},
sessionToken: token,
live: false,
onSuccess: (card) {
debugPrint("Scanned card: $card");
},
onError: (error) {
showMessage(context, error?.message ?? 'Scan failed');
debugPrint("Error: $error");
},
onWebViewLoadError: () {
showMessage(context, 'WebView failed to load');
},
onCancel: Navigator.of(context).pop,
autoSwitchActiveColor: Colors.blue,
progressBarColor: Colors.blue,
widgetBackgroundColor: Colors.purple,
autoSwitchInactiveColor: Colors.blue,
backsideSupport: false,
messageStyle: const TextStyle(
fontSize: 70,
color: Colors.blue,
backgroundColor: Colors.white,
),
messages: const CardScanMessages(
autoCaptureTitle: 'autoCaptureTitle',
cameraErrorTitle: 'cameraErrorTitle',
completedTitle: 'completedTitle',
errorTitle: 'errorTitle',
frontSideCompletedTitle: 'frontsideCompletedTitle',
manualCaptureTitle: 'manualCaptureTitle',
processingTitle: 'processingTitle',
retryTitle: 'retryTitle',
),
),
),
);
}
}
void showMessage(BuildContext context, String message) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text(message),
actions: [
TextButton(
onPressed: Navigator.of(context).pop,
child: const Text("OK"),
)
],
),
);
}
更多关于Flutter保险卡扫描插件insurance_card_scanner的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter保险卡扫描插件insurance_card_scanner的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中集成并使用insurance_card_scanner
插件的示例代码。这个插件通常用于扫描和解析保险卡信息。需要注意的是,insurance_card_scanner
是一个假设的插件名称,实际使用中你需要查找并确认具体的插件名称及其用法,因为Flutter社区中的插件名称可能会有所不同。不过,下面的代码结构适用于大多数图像扫描和处理插件。
首先,确保你已经在pubspec.yaml
文件中添加了相应的插件依赖:
dependencies:
flutter:
sdk: flutter
# 假设插件名称确实是 insurance_card_scanner
insurance_card_scanner: ^latest_version
然后,运行flutter pub get
来安装插件。
接下来,在你的Flutter项目中,你可以按照以下方式使用这个插件:
import 'package:flutter/material.dart';
import 'package:insurance_card_scanner/insurance_card_scanner.dart'; // 假设的导入路径
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Insurance Card Scanner Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: InsuranceCardScannerScreen(),
);
}
}
class InsuranceCardScannerScreen extends StatefulWidget {
@override
_InsuranceCardScannerScreenState createState() => _InsuranceCardScannerScreenState();
}
class _InsuranceCardScannerScreenState extends State<InsuranceCardScannerScreen> {
String _resultText = '';
Future<void> _scanInsuranceCard() async {
try {
// 启动扫描器
final InsuranceCardResult result = await InsuranceCardScanner.scanInsuranceCard();
// 处理扫描结果
setState(() {
_resultText = 'Name: ${result.name}\n'
'Policy Number: ${result.policyNumber}\n'
'Expiry Date: ${result.expiryDate}\n' // 假设这些是返回的数据字段
// 根据实际插件返回的数据结构进行调整
;
});
} catch (e) {
// 处理错误
setState(() {
_resultText = 'Error scanning insurance card: ${e.message}';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Insurance Card Scanner'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _scanInsuranceCard,
child: Text('Scan Insurance Card'),
),
SizedBox(height: 20),
if (_resultText.isNotEmpty)
Text(
_resultText,
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
),
],
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮用于启动保险卡扫描。扫描完成后,结果将显示在屏幕上。需要注意的是,InsuranceCardScanner.scanInsuranceCard()
是一个假设的方法调用,你需要根据实际的插件文档替换为正确的方法调用。
此外,InsuranceCardResult
和其中的字段(如name
、policyNumber
、expiryDate
)也是假设的,你需要根据插件返回的实际数据结构进行调整。
由于insurance_card_scanner
可能不是一个真实存在的插件名称,因此你需要搜索并找到适用于你的需求的Flutter插件,并参考其官方文档进行集成和使用。