Flutter二维码扫描插件flutter_cidscan的使用
Flutter二维码扫描插件flutter_cidscan的使用
Flutter插件flutter_cidscan提供了全屏相机条形码扫描预览功能以及创建高性能应用程序所需的所有功能。该插件旨在简化开发过程,减少工作量。
如果您需要更多的用户界面控制或希望使用自己的小部件,可以使用我们的CIDScanView小部件。您可以将该小部件放置在屏幕上的任何位置,并指定所需的大小。
开始使用
要开始使用flutter_cidscan插件,您可以查看我们的在线文档,其中包含教程、示例、移动开发指南以及完整的API参考。
完整示例代码
以下是一个完整的示例代码,展示了如何使用flutter_cidscan插件进行二维码扫描:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_cidscan/cidscanview.dart';
import 'package:flutter_cidscan/flutter_cidscan.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var activeTL = false;
var _visible = false;
[@override](/user/override)
void initState() {
super.initState();
}
void handleLicenseEvent(Map event) async {
if (event["body"]["FunctionName"].compareTo('onActivationResult') == 0) {
String value = await FlutterCidscan.decoderVersion();
print(value);
// startScanner();
}
}
void handleInit(Map event) async {
if (event["body"]["FunctionName"].compareTo('initCaptureID') == 0) {
FlutterCidscan.activateEDKLicense(
'BNX9965QNM3ahzCaE514DD2zcGruYvEAJw2Aet6zYmxSW20x97BcZyWuzY/m+kckR3zIQB3DucWcvsHLuJK4/+z+lA/0YYf7x9zcwvxxkxr5NYMKl+MtPzoGeOtq+cTv0ThTapUOCsd2tLKzivOd52Y+gVStOJuD1mJchvjMX16Jzk8U/BFvRbuZBLEd6u7FvVzWNAS4tUNXPGvpU8zQNJDlvicgmELUu6dK4wr1VcbxepSpaAC6OW2g9L3Efq8ZTX9PtdAS3SRw4seuwpk5CDo5cS9dQ3vriY+AZVbIaqfkIN0OkbXgJIIAmYIBTgTDP9B2AqXSwYYNgbguOLCGGjrer9oZgpN+SgkhParPOX9ihr81jDMtNKvvDRYYDk5JjZj5e6cerRO1/sPtZ8dofg==',
'P4I082220190001')!.listen((event) => {handleLicenseEvent(event)});
}
}
void torchlight() {
if (!activeTL) {
activeTL = true;
FlutterCidscan.setTorch(true);
} else {
activeTL = false;
FlutterCidscan.setTorch(false);
}
}
void startScanner() {
FlutterCidscan.enableAllDecoders(true);
FlutterCidscan.startCameraPreview();
FlutterCidscan.startDecoding()!.listen((event) => {handleDecode(event)});
}
void restartScanner() {
FlutterCidscan.startCameraPreview();
FlutterCidscan.startDecoding()!.listen((event) => {handleDecode(event)});
}
void handleDecode(Map event) {
print(event);
if (event["body"]["FunctionName"].compareTo('receivedDecodedData') == 0) {
FlutterCidscan.stopDecoding();
setState(() {
_visible = !_visible;
});
}
}
void _toggle() {
if (_visible) {
FlutterCidscan.stopDecoding();
} else {
startDecode();
}
setState(() {
_visible = !_visible;
});
}
void startDecode() {
FlutterCidscan.startDecoding()!.listen((event) => {handleDecode(event)});
}
Future<void> init() async {
FlutterCidscan.initCaptureID('Wir benötigen die Kamera Berechtigung zum Scannen der Barcodes', false, true)!.listen((event) => {handleInit(event)});
}
void onCreated(CIDScanViewController ctrl) {}
[@override](/user/override)
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Container(
color: Color(0xff619ff9),
),
Visibility(
visible: _visible,
child: Container(
height: 200.0,
child: new Directionality(
textDirection: TextDirection.ltr,
child: CIDScanView(
height: 200,
width: 400,
onCIDScanViewCreated: onCreated,
),
),
),
),
Positioned(
top: 70,
left: 30,
child: Container(
alignment: Alignment.topCenter,
height: 50,
width: 50,
child: Directionality(
textDirection: TextDirection.ltr,
child: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: TextButton(
onPressed: _toggle,
child: Image(
alignment: Alignment.center,
matchTextDirection: true,
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
),
)),
),
),
),
Positioned(
top: 70,
right: 30,
child: Container(
alignment: Alignment.topCenter,
height: 50,
width: 50,
child: Directionality(
textDirection: TextDirection.ltr,
child: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: TextButton(
onPressed: torchlight,
child: Image(
alignment: Alignment.center,
matchTextDirection: true,
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
),
)),
),
),
),
Positioned(
bottom: 200,
child: Container(
alignment: Alignment.bottomCenter,
height: 50,
width: 150,
child: Directionality(
textDirection: TextDirection.ltr,
child: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: TextButton(
onPressed: init,
child: Text(
'Init',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
),
),
),
),
Positioned(
bottom: 100,
child: Container(
alignment: Alignment.bottomCenter,
height: 50,
width: 150,
child: Directionality(
textDirection: TextDirection.ltr,
child: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: TextButton(
onPressed: _toggle,
child: Text(
'Starten',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
),
),
),
),
],
);
}
}
更多关于Flutter二维码扫描插件flutter_cidscan的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter二维码扫描插件flutter_cidscan的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_cidscan 是一个用于在 Flutter 应用中实现二维码扫描的插件。它基于 MLKit 和 ZXing 技术,提供了快速且准确的二维码识别功能。以下是如何在 Flutter 项目中使用 flutter_cidscan 插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml 文件中添加 flutter_cidscan 依赖:
dependencies:
flutter:
sdk: flutter
flutter_cidscan: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get 来获取依赖。
2. 配置权限
为了使用摄像头进行扫描,你需要在 Android 和 iOS 项目中配置相应的权限。
Android
在 android/app/src/main/AndroidManifest.xml 文件中添加以下权限:
<uses-permission android:name="android.permission.CAMERA" />
iOS
在 ios/Runner/Info.plist 文件中添加以下权限:
<key>NSCameraUsageDescription</key>
<string>我们需要访问您的相机以进行二维码扫描</string>
3. 使用插件进行二维码扫描
接下来,你可以在你的 Flutter 应用中使用 flutter_cidscan 插件来扫描二维码。
import 'package:flutter/material.dart';
import 'package:flutter_cidscan/flutter_cidscan.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: QRScanScreen(),
);
}
}
class QRScanScreen extends StatefulWidget {
[@override](/user/override)
_QRScanScreenState createState() => _QRScanScreenState();
}
class _QRScanScreenState extends State<QRScanScreen> {
String _scanResult = 'Scan a QR Code';
Future<void> _scanQR() async {
try {
String result = await FlutterCidscan.scanQRCode;
setState(() {
_scanResult = result ?? 'No result';
});
} catch (e) {
setState(() {
_scanResult = 'Failed to scan QR Code';
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('QR Code Scanner'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_scanResult),
SizedBox(height: 20),
ElevatedButton(
onPressed: _scanQR,
child: Text('Scan QR Code'),
),
],
),
),
);
}
}

