Flutter视觉处理插件fluttervisionsdkplugin的功能
Flutter视觉处理插件fluttervisionsdkplugin的功能
Flutter Vision SDK Plugin 功能概述
Flutter Vision SDK Plugin 是一个用于将 Flutter Vision SDK 整合到您的 Flutter 应用程序中的插件。该插件提供了扫描条形码、二维码和文本的功能,并支持 OCR(光学字符识别)。您可以根据需要自定义扫描和捕捉模式。
示例代码
import 'package:fluttervisionsdkplugin/visionsdk.dart';
import 'package:fluttervisionsdkplugin/visioncamerawidget.dart';
import 'package:fluttervisionsdkplugin/ondeviceocrmanager.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FlutterToPluginCommunicator? communicator;
OnDeviceOCRManagerCommunicator? onDeviceOCRManagerCommunicator;
void updateScannedType(bool isText, bool isBarcode, bool isQrCode, bool isDocument) {
setState(() {
isTextBeingDetected = isText;
isBarcodeBeingDetected = isBarcode;
isQRCodeBeingDetected = isQrCode;
isDocumentBeingDetected = isDocument;
});
}
@override
void initState() {
super.initState();
VisionSDK().initialize(Environment.sandbox);
onDeviceOCRManagerCommunicator = OnDeviceOCRManagerCommunicator(
MainOnDeviceOCRManagerListener(this)
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<PermissionStatus>(
future: Permission.camera.status,
builder: (context, snapshot) {
if (Platform.isIOS || snapshot.data?.isGranted == true) {
return Stack(
children: [
SizedBox(
width: double.infinity,
height: double.infinity,
child: VisionCameraWidget(
onViewCreated: (communicator) {
this.communicator = communicator;
communicator?.setScanAuto();
communicator?.startCamera();
},
listener: MainNativeViewWidgetListener(this),
),
),
Align(
alignment: Alignment.topRight,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Barcode', style: TextStyle(color: isBarcodeBeingDetected ? Colors.green : Colors.white)),
Text('QRCode', style: TextStyle(color: isQRCodeBeingDetected ? Colors.green : Colors.white)),
Text('Text', style: TextStyle(color: isTextBeingDetected ? Colors.green : Colors.white)),
Text('Document', style: TextStyle(color: isDocumentBeingDetected ? Colors.green : Colors.white)),
],
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: double.infinity,
color: const Color(0x88FFFFFF),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
DropdownMenu(
dropDownOptions: ['Barcode', 'QRCode', 'OCR'],
onItemSelected: (selectedItemIndex) {
switch (selectedItemIndex) {
case 0:
setState(() {
captureMode = 1; // BARCODE
});
communicator?.setCaptureModeBarcode();
break;
case 1:
setState(() {
captureMode = 2; // QRCODE
});
communicator?.setCaptureModeQrCode();
break;
case 2:
setState(() {
captureMode = 3; // OCR
});
communicator?.setCaptureModeOCR();
break;
}
},
),
],
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(iconSize: 34, icon: Icon(Icons.switch_camera,), onPressed: () {
communicator?.setCameraSettings(CameraSettings(nthFrameToProcess: cameraSettings.nthFrameToProcess, cameraLensFace: cameraSettings.cameraLensFace == CameraLensFace.back ? CameraLensFace.front : CameraLensFace.back));
}),
SizedBox(height: 20,),
Container(
color: const Color(0x88FFFFFF),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: [
RadioListTile(
value: true,
title: const Text('Online OCR'),
groupValue: getOnlinePredictions,
onChanged: (i) {
setState(() {
getOnlinePredictions = true;
});
},
),
RadioListTile(
value: false,
title: const Text('On-Device OCR'),
groupValue: getOnlinePredictions,
onChanged: (i) {
setState(() {
getOnlinePredictions = false;
});
},
),
],
),
),
DropdownMenu(
dropDownOptions: ['Shipping Label', 'Bill Of Lading', 'Item Label', 'Document Classification'],
onItemSelected: (selectedItemIndex) {
switch (selectedItemIndex) {
case 0:
selectedModel = ModelClass.shippingLabel;
break;
case 1:
selectedModel = ModelClass.billOf Lading;
break;
case 2:
selectedModel = ModelClass.itemLabel;
break;
case 3:
selectedModel = ModelClass.documentClassification;
break;
}
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: RadioListTile(
value: true,
title: const Text('Auto'),
groupValue: isAutoScanning,
onChanged: (i) {
setState(() {
isAutoScanning = true;
});
},
),
),
Expanded(
child: RadioListTile(
value: false,
title: const Text('Manual'),
groupValue: isAutoScanning,
onChanged: (i) {
setState(() {
isAutoScanning = false;
});
},
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenlyy,
children: [
ElevatedButton(
onPressed: () {
communicator?.capturePhoto();
},
child: const Text('Capture Photo'),
),
],
),
],
),
),
],
);
} else {
return Center(
child: FutureBuilder<bool>(
future: Permission.camera.isPermanentlyDenied,
builder: (context, snapshot) {
if (snapshot.data == true) {
return const Text("Go to Settings and allow camera permission");
} else {
return ElevatedButton(
onPressed: () async {
await Permission.camera.request();
},
child: const Text('Request Camera Permission'),
);
}
},
),
);
}
},
),
);
}
}
示例说明
- 初始化VisionSDK:在使用任何其他类之前,必须先初始化
VisionSDK
。
VisionSDK().initialize(Environment.sandbox);
- 设置捕捉模式:可以设置捕捉模式为条形码、二维码或OCR。
communicator?.setCaptureModeBarcode();
- 设置扫描模式:可以设置扫描模式为自动或手动。
communicator?.setScanAuto();
- 手动扫描条形码或二维码:
communicator?.capturePhoto();
- 手动进行OCR处理:
communicator?.capturePhoto();
- 调用云服务进行物流处理:
communicator?.callShippingLabelApi(
apiKey: 'YOUR_API_KEY_HERE',
image: byteArrayImage,
barcodes: codes,
sender: {
'contact_id': 'ctct_hNxjevb74gV62i5XTQvq6'
},
recipient: {
'contact_id': 'ctct_hNxjevb74gV62i5XTQvq6'
},
options: {
'match': {
'search': ['recipients'],
'location': true
},
'postprocess': {'require_unique_hash': false},
'transform': {
'tracker': 'outbound',
'use_existing_tracking_number': false
}
},
metadata: {
'Test': 'Pass'
});
- 配置On-Device OCR模型:
configureOnDeviceSLModel(OnDeviceOCRManagerCommunicator? communicator) {
communicator?.configureOnDeviceOCR(
apiKey: 'YOUR_API_KEY_HERE',
modelClass: ModelClass.shippingLabel,
modelSize: ModelSize.large);
}
- 报告错误:
reportAnIssue(
OnDeviceOCRManagerCommunicator? communicator,
String? apiKey,
String? token,
required ModelClass modelClass,
required ModelSize modelSize,
required String report,
Map<String, dynamic>? customData,
String? base64ImageToReportOn
) {
communicator?.reportAnIssue(
apiKey: apiKey,
token: tokem,
modelClass: modelClass,
modelSize: modelSize,
report: report,
customData: customData,
base64ImageToReportOn: base64ImageToReportOn
);
}
更多关于Flutter视觉处理插件fluttervisionsdkplugin的功能的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter视觉处理插件fluttervisionsdkplugin的功能的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,Flutter Vision SDK Plugin 是一个强大的 Flutter 插件,用于在 Flutter 应用中实现各种视觉处理功能。它通常包括图像识别、图像分析、面部检测、条形码扫描等功能。以下是一个关于如何使用 Flutter Vision SDK Plugin 进行简单图像识别的代码示例。
首先,确保你已经在 pubspec.yaml
文件中添加了 Flutter Vision SDK Plugin 的依赖项:
dependencies:
flutter:
sdk: flutter
camera: ^0.x.x # 假设你需要使用相机插件来获取图像
flutter_vision_sdk_plugin: ^x.x.x # 替换为实际版本号
然后,运行 flutter pub get
来获取这些依赖项。
接下来,编写 Flutter 代码来使用这个插件。以下是一个简单的示例,展示了如何从相机获取图像并使用 Flutter Vision SDK 进行图像识别:
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:flutter_vision_sdk_plugin/flutter_vision_sdk_plugin.dart'; // 假设包名正确
List<CameraDescription> cameras;
late CameraController controller;
final VisionService visionService = VisionService();
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Vision SDK Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late String imagePath;
@override
void initState() {
super.initState();
availableCameras().then((availableCameras) {
cameras = availableCameras;
if (cameras.isEmpty) {
return;
}
controller = CameraController(cameras[0], ResolutionPreset.medium);
controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
});
}).catchError((err) {
print('Error: ${err.message}');
});
}
@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return Scaffold(
appBar: AppBar(
title: Text('Flutter Vision SDK Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CameraPreview(controller),
ElevatedButton(
onPressed: () async {
final image = await controller.takePicture();
imagePath = image.path;
// 使用 Flutter Vision SDK 进行图像识别
final result = await visionService.detectObjects(imagePath);
print('Detected Objects: $result');
// 你可以在这里处理识别结果,比如显示在 UI 上
},
child: Text('Capture and Detect'),
),
],
),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
注意:
VisionService
是假设的 Flutter Vision SDK Plugin 提供的服务类,实际使用时需要参考插件的官方文档来确定正确的类名和方法。detectObjects
方法也是假设的,实际插件可能提供不同的方法名和功能。camera
插件用于获取相机图像,你需要确保相机权限和插件配置正确。
这个示例展示了如何从相机获取图像并使用假设的 VisionService
进行图像识别。实际使用时,你需要参考 Flutter Vision SDK Plugin 的官方文档来获取正确的类名、方法名以及使用方式。