Flutter plugin for scanning and generating QR codes using the ZXing library, supporting Android, iOS, and desktop platforms
flutterbarcode-generatorbarcode-scannergeneratorqrqrcodeqrcode-generatorqrcode-scannerscannerzxingbarcodezxscanner
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
3 years ago
|
import 'dart:isolate';
|
||
|
import 'dart:math';
|
||
|
|
||
|
import 'package:camera/camera.dart';
|
||
|
|
||
|
import 'flutter_zxing.dart';
|
||
|
import 'image_converter.dart';
|
||
|
|
||
|
// Inspired from https://github.com/am15h/object_detection_flutter
|
||
|
|
||
|
/// Bundles data to pass between Isolate
|
||
|
class IsolateData {
|
||
|
CameraImage cameraImage;
|
||
|
double cropPercent;
|
||
|
|
||
|
SendPort? responsePort;
|
||
|
|
||
|
IsolateData(
|
||
|
this.cameraImage,
|
||
|
this.cropPercent,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/// Manages separate Isolate instance for inference
|
||
|
class IsolateUtils {
|
||
|
static const String kDebugName = "ZxingIsolate";
|
||
|
|
||
|
// ignore: unused_field
|
||
|
Isolate? _isolate;
|
||
|
final _receivePort = ReceivePort();
|
||
|
SendPort? _sendPort;
|
||
|
|
||
|
SendPort? get sendPort => _sendPort;
|
||
|
|
||
|
Future<void> start() async {
|
||
|
_isolate = await Isolate.spawn<SendPort>(
|
||
|
entryPoint,
|
||
|
_receivePort.sendPort,
|
||
|
debugName: kDebugName,
|
||
|
);
|
||
|
|
||
|
_sendPort = await _receivePort.first;
|
||
|
}
|
||
|
|
||
|
void stop() {
|
||
|
_isolate?.kill(priority: Isolate.immediate);
|
||
|
_isolate = null;
|
||
|
_sendPort = null;
|
||
|
}
|
||
|
|
||
|
static void entryPoint(SendPort sendPort) async {
|
||
|
final port = ReceivePort();
|
||
|
sendPort.send(port.sendPort);
|
||
|
|
||
|
await for (final IsolateData? isolateData in port) {
|
||
|
if (isolateData != null) {
|
||
|
final image = isolateData.cameraImage;
|
||
|
final cropPercent = isolateData.cropPercent;
|
||
|
final bytes = await convertImage(image);
|
||
|
final cropSize = (min(image.width, image.height) * cropPercent).round();
|
||
|
|
||
|
final result =
|
||
|
FlutterZxing.zxingRead(bytes, image.width, image.height, cropSize);
|
||
|
|
||
|
isolateData.responsePort?.send(result);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|