2023-08-18 20:05:21 +00:00
|
|
|
import 'dart:async';
|
2023-08-19 00:12:44 +00:00
|
|
|
import 'dart:ffi';
|
|
|
|
import 'dart:typed_data';
|
2023-08-18 20:05:21 +00:00
|
|
|
|
|
|
|
import 'package:camerawesome/camerawesome_plugin.dart';
|
2023-08-17 23:14:32 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-08-18 20:05:21 +00:00
|
|
|
import 'package:google_mlkit_barcode_scanning/google_mlkit_barcode_scanning.dart';
|
|
|
|
import 'package:rxdart/rxdart.dart';
|
2023-08-19 00:12:44 +00:00
|
|
|
import 'ffi.dart';
|
|
|
|
import 'utils/mlkit_utils.dart';
|
2023-08-17 23:14:32 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
2023-08-19 00:12:44 +00:00
|
|
|
enum CuttleState {
|
|
|
|
unitialized,
|
|
|
|
receiving,
|
|
|
|
received,
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:14:32 +00:00
|
|
|
class MyApp extends StatelessWidget {
|
2023-08-18 20:05:21 +00:00
|
|
|
const MyApp({super.key});
|
2023-08-17 23:14:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2023-08-18 20:05:21 +00:00
|
|
|
title: 'camerAwesome App',
|
2023-08-17 23:14:32 +00:00
|
|
|
theme: ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
2023-08-18 20:05:21 +00:00
|
|
|
home: const MyHomePage(),
|
2023-08-17 23:14:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
2023-08-18 20:05:21 +00:00
|
|
|
const MyHomePage({super.key});
|
2023-08-17 23:14:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
2023-08-18 20:05:21 +00:00
|
|
|
final _barcodeScanner = BarcodeScanner(formats: [BarcodeFormat.qrCode]);
|
|
|
|
|
|
|
|
final _buffer = <String>[];
|
|
|
|
final _barcodesController = BehaviorSubject<List<String>>();
|
|
|
|
late final Stream<List<String>> _barcodesStream = _barcodesController.stream;
|
|
|
|
final _scrollController = ScrollController();
|
2023-08-17 23:14:32 +00:00
|
|
|
|
2023-08-19 16:23:12 +00:00
|
|
|
TxConfig? _txConfig;
|
|
|
|
String? _rxText;
|
|
|
|
var _cuttleState = CuttleState.unitialized;
|
|
|
|
final _rxData = <int>[];
|
2023-08-19 00:12:44 +00:00
|
|
|
|
2023-08-17 23:14:32 +00:00
|
|
|
@override
|
2023-08-18 20:05:21 +00:00
|
|
|
void dispose() {
|
|
|
|
_barcodesController.close();
|
|
|
|
super.dispose();
|
2023-08-17 23:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2023-08-18 20:05:21 +00:00
|
|
|
body: CameraAwesomeBuilder.previewOnly(
|
|
|
|
onImageForAnalysis: (img) => _processImageBarcode(img),
|
|
|
|
imageAnalysisConfig: AnalysisConfig(
|
|
|
|
androidOptions: const AndroidAnalysisOptions.nv21(
|
|
|
|
width: 1024,
|
|
|
|
),
|
2023-08-19 16:23:12 +00:00
|
|
|
maxFramesPerSecond: null,
|
2023-08-18 20:05:21 +00:00
|
|
|
autoStart: false,
|
|
|
|
),
|
|
|
|
builder: (cameraModeState, previewSize, previewRect) {
|
|
|
|
return _BarcodeDisplayWidget(
|
|
|
|
barcodesStream: _barcodesStream,
|
|
|
|
scrollController: _scrollController,
|
|
|
|
analysisController: cameraModeState.analysisController!,
|
|
|
|
);
|
|
|
|
},
|
2023-08-17 23:14:32 +00:00
|
|
|
),
|
2023-08-18 20:05:21 +00:00
|
|
|
);
|
|
|
|
}
|
2023-08-17 23:14:32 +00:00
|
|
|
|
2023-08-18 20:05:21 +00:00
|
|
|
Future _processImageBarcode(AnalysisImage img) async {
|
|
|
|
final inputImage = toInputImage(img as Nv21Image);
|
|
|
|
|
|
|
|
try {
|
|
|
|
var recognizedBarCodes = await _barcodeScanner.processImage(inputImage);
|
|
|
|
for (Barcode barcode in recognizedBarCodes) {
|
2023-08-19 16:23:12 +00:00
|
|
|
var bytes = barcode.rawBytes;
|
|
|
|
if (bytes == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final dbytes = bytes;
|
|
|
|
switch (_cuttleState) {
|
|
|
|
case CuttleState.unitialized:
|
|
|
|
{
|
|
|
|
var txconf = await api.getTxConfig(bytes: dbytes);
|
|
|
|
if (txconf != null) {
|
|
|
|
_txConfig = txconf;
|
|
|
|
_cuttleState = CuttleState.receiving;
|
|
|
|
debugPrint(
|
|
|
|
"txconf: ${txconf.len} bytes, \"${txconf.description}\"");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// implicit else here; txconf was null
|
|
|
|
var text = barcode.rawValue;
|
|
|
|
if (text != null) {
|
|
|
|
// it's not a txconfig, and it's not a raptor packet, so it must be a regular qr code
|
|
|
|
_rxText = text;
|
|
|
|
_cuttleState = CuttleState.received;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case CuttleState.receiving:
|
|
|
|
{
|
|
|
|
final check = await api.checkRaptor(
|
|
|
|
buffer: Uint8List.fromList(_rxData), txconf: _txConfig!);
|
|
|
|
if (check) {
|
|
|
|
_cuttleState = CuttleState.received;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case CuttleState.received:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-18 20:05:21 +00:00
|
|
|
_addBarcode("[${barcode.format.name}]: ${barcode.rawValue}");
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
debugPrint("...sending image resulted error $error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _addBarcode(String value) {
|
|
|
|
try {
|
|
|
|
if (_buffer.length > 300) {
|
|
|
|
_buffer.removeRange(_buffer.length - 300, _buffer.length);
|
|
|
|
}
|
|
|
|
if (_buffer.isEmpty || value != _buffer[0]) {
|
|
|
|
_buffer.insert(0, value);
|
|
|
|
_barcodesController.add(_buffer);
|
|
|
|
_scrollController.animateTo(
|
|
|
|
0,
|
|
|
|
duration: const Duration(milliseconds: 400),
|
|
|
|
curve: Curves.fastLinearToSlowEaseIn,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
debugPrint("...logging error $err");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BarcodeDisplayWidget extends StatefulWidget {
|
|
|
|
final Stream<List<String>> barcodesStream;
|
|
|
|
final ScrollController scrollController;
|
|
|
|
|
|
|
|
final AnalysisController analysisController;
|
|
|
|
|
|
|
|
const _BarcodeDisplayWidget({
|
|
|
|
// ignore: unused_element
|
|
|
|
super.key,
|
|
|
|
required this.barcodesStream,
|
|
|
|
required this.scrollController,
|
|
|
|
required this.analysisController,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<_BarcodeDisplayWidget> createState() => _BarcodeDisplayWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BarcodeDisplayWidgetState extends State<_BarcodeDisplayWidget> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.tealAccent.withOpacity(0.7),
|
2023-08-17 23:14:32 +00:00
|
|
|
),
|
2023-08-18 20:05:21 +00:00
|
|
|
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
|
|
|
Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
child: CheckboxListTile(
|
|
|
|
value: widget.analysisController.enabled,
|
|
|
|
onChanged: (newValue) async {
|
|
|
|
if (widget.analysisController.enabled == true) {
|
|
|
|
await widget.analysisController.stop();
|
|
|
|
} else {
|
|
|
|
await widget.analysisController.start();
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
title: const Text(
|
|
|
|
"Enable barcode scan",
|
|
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
height: 120,
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
child: StreamBuilder<List<String>>(
|
|
|
|
stream: widget.barcodesStream,
|
|
|
|
builder: (context, value) => !value.hasData
|
|
|
|
? const SizedBox.expand()
|
|
|
|
: ListView.separated(
|
|
|
|
padding: const EdgeInsets.only(top: 8),
|
|
|
|
controller: widget.scrollController,
|
|
|
|
itemCount: value.data!.length,
|
|
|
|
separatorBuilder: (context, index) =>
|
|
|
|
const SizedBox(height: 4),
|
|
|
|
itemBuilder: (context, index) => Text(value.data![index]),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]),
|
2023-08-17 23:14:32 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|