gpt4 book ai didi

Flutter - 在容器中显示相机/BarcodeScanner

转载 作者:行者123 更新时间:2023-12-05 06:25:22 28 4
gpt4 key购买 nike

如标题所述,我想将相机功能存储在单独的容器中。这意味着,如果相机启动,它只显示在红色容器中,此时我该如何实现?

我的屏幕图片:https://imgur.com/a/0qFvHto

我使用以下依赖项进行扫描:

barcode_scan: ^1.0.0 


My Code for scanning looks like this:

Future _scanQR() async {
try {
String qrResult = await BarcodeScanner.scan();
setState(() {
result = qrResult;
Future.delayed(const Duration(seconds: 1));
showAlertDialog(context);
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
result = "Zugriff auf die Kamera wurde nicht gewährt!";
});
} else {
setState(() {
result = "Unknown Error $ex";
});
}
} on FormatException {
setState(() {
result = "Scan fehlgeschlagen";
});
} catch (ex) {
setState(() {
result = "Unknown Error $ex";
});
}
}

最佳答案

请检查此包使用的方法https://github.com/elratonmaton/LastQrScanner
或者改用这个包

此包的Dart代码使用了AndroidView和UiKitView
代码片段

class _QRViewState extends State<LastQrScannerPreview> {
@override
Widget build(BuildContext context) {
var androidView = AndroidView(
viewType: 'last_qr_scanner/qrview',
onPlatformViewCreated: _onPlatformViewCreated,
);

if (defaultTargetPlatform == TargetPlatform.android) {
return androidView;
}

if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: 'last_qr_scanner/qrview',
onPlatformViewCreated: _onPlatformViewCreated,
creationParams: _CreationParams.fromWidget(0, 0).toMap(),
creationParamsCodec: StandardMessageCodec(),
);
}

更多关于Kotlin部分的细节,可以引用源码
模拟器中的结果

enter image description here

您可以在示例代码中看到 Widget 构建体使用 LastQrScannerPreview

body: Column(
children: <Widget>[
Expanded(
child: LastQrScannerPreview(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
flex: 4,
),

此包的完整示例代码

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:last_qr_scanner/last_qr_scanner.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
const MyApp({
Key key,
}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
var qrText = "";
var controller;

@override
void initState() {
super.initState();
}

void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
final channel = controller.channel;
controller.init(qrKey);
channel.setMethodCallHandler((MethodCall call) async {
switch (call.method) {
case "onRecognizeQR":
dynamic arguments = call.arguments;
setState(() {
qrText = arguments.toString();
});
}
});
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Barcode Scanner Example'),
),
body: Column(
children: <Widget>[
Expanded(
child: LastQrScannerPreview(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
flex: 4,
),
Expanded(
child: Text("This is the result of scan: $qrText"),
flex: 1,
),
Expanded(
child: RaisedButton(
onPressed: () {
this.controller.toggleTorch();
},
child: Text("Toggle Torch"),
),
flex: 1,
)
],
),
),
);
}
}

关于Flutter - 在容器中显示相机/BarcodeScanner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57177135/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com