Sander Roest

Working, but bad recognition in landscape.

@@ -12,8 +12,6 @@ import 'package:mobile_scanner_example/mobile_scanner_overlay.dart'; @@ -12,8 +12,6 @@ import 'package:mobile_scanner_example/mobile_scanner_overlay.dart';
12 import 'package:mobile_scanner_example/picklist/picklist_result.dart'; 12 import 'package:mobile_scanner_example/picklist/picklist_result.dart';
13 13
14 void main() async { 14 void main() async {
15 - WidgetsFlutterBinding.ensureInitialized();  
16 - await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);  
17 runApp( 15 runApp(
18 const MaterialApp( 16 const MaterialApp(
19 title: 'Mobile Scanner Example', 17 title: 'Mobile Scanner Example',
1 import 'dart:async'; 1 import 'dart:async';
2 2
3 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
4 -import 'package:flutter/services.dart';  
5 import 'package:mobile_scanner/mobile_scanner.dart'; 4 import 'package:mobile_scanner/mobile_scanner.dart';
6 -import 'package:mobile_scanner_example/picklist/classes/detect_collision.dart'; 5 +import 'package:mobile_scanner_example/picklist/classes/barcode_at_center.dart';
  6 +
7 import 'package:mobile_scanner_example/picklist/widgets/crosshair.dart'; 7 import 'package:mobile_scanner_example/picklist/widgets/crosshair.dart';
8 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 8 import 'package:mobile_scanner_example/scanner_error_widget.dart';
9 9
@@ -21,13 +21,12 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> @@ -21,13 +21,12 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
21 ); 21 );
22 StreamSubscription<Object?>? _barcodesSubscription; 22 StreamSubscription<Object?>? _barcodesSubscription;
23 23
24 - final _scannerDisabled = ValueNotifier(false); 24 + final _scannerEnabled = ValueNotifier(true);
25 25
26 bool barcodeDetected = false; 26 bool barcodeDetected = false;
27 27
28 @override 28 @override
29 void initState() { 29 void initState() {
30 - SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);  
31 WidgetsBinding.instance.addObserver(this); 30 WidgetsBinding.instance.addObserver(this);
32 _barcodesSubscription = _mobileScannerController.barcodes.listen( 31 _barcodesSubscription = _mobileScannerController.barcodes.listen(
33 _handleBarcodes, 32 _handleBarcodes,
@@ -69,17 +68,14 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> @@ -69,17 +68,14 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
69 } 68 }
70 69
71 void _handleBarcodes(BarcodeCapture capture) { 70 void _handleBarcodes(BarcodeCapture capture) {
72 - if (_scannerDisabled.value) { 71 + if (!_scannerEnabled.value) {
73 return; 72 return;
74 } 73 }
75 74
76 for (final barcode in capture.barcodes) { 75 for (final barcode in capture.barcodes) {
77 - if (isPointInPolygon(  
78 - Offset(  
79 - _mobileScannerController.value.size.width / 2,  
80 - _mobileScannerController.value.size.height / 2,  
81 - ),  
82 - barcode.corners, 76 + if (isBarcodeAtCenterOfImage(
  77 + cameraOutputSize: _mobileScannerController.value.size,
  78 + barcode: barcode,
83 )) { 79 )) {
84 if (!barcodeDetected) { 80 if (!barcodeDetected) {
85 barcodeDetected = true; 81 barcodeDetected = true;
@@ -92,14 +88,7 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> @@ -92,14 +88,7 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
92 88
93 @override 89 @override
94 Widget build(BuildContext context) { 90 Widget build(BuildContext context) {
95 - const boxFit = BoxFit.contain;  
96 - return PopScope(  
97 - onPopInvokedWithResult: (didPop, result) {  
98 - if (didPop) {  
99 - SystemChrome.setPreferredOrientations([...DeviceOrientation.values]);  
100 - }  
101 - },  
102 - child: Scaffold( 91 + return Scaffold(
103 appBar: AppBar(title: const Text('Picklist scanner')), 92 appBar: AppBar(title: const Text('Picklist scanner')),
104 backgroundColor: Colors.black, 93 backgroundColor: Colors.black,
105 body: StreamBuilder( 94 body: StreamBuilder(
@@ -107,9 +96,9 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> @@ -107,9 +96,9 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
107 builder: (context, snapshot) { 96 builder: (context, snapshot) {
108 return Listener( 97 return Listener(
109 behavior: HitTestBehavior.opaque, 98 behavior: HitTestBehavior.opaque,
110 - onPointerDown: (_) => _scannerDisabled.value = true,  
111 - onPointerUp: (_) => _scannerDisabled.value = false,  
112 - onPointerCancel: (_) => _scannerDisabled.value = false, 99 + onPointerDown: (_) => _scannerEnabled.value = false,
  100 + onPointerUp: (_) => _scannerEnabled.value = true,
  101 + onPointerCancel: (_) => _scannerEnabled.value = true,
113 child: Stack( 102 child: Stack(
114 fit: StackFit.expand, 103 fit: StackFit.expand,
115 children: [ 104 children: [
@@ -117,13 +106,13 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> @@ -117,13 +106,13 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
117 controller: _mobileScannerController, 106 controller: _mobileScannerController,
118 errorBuilder: (context, error, child) => 107 errorBuilder: (context, error, child) =>
119 ScannerErrorWidget(error: error), 108 ScannerErrorWidget(error: error),
120 - fit: boxFit, 109 + fit: BoxFit.contain,
121 ), 110 ),
122 ValueListenableBuilder( 111 ValueListenableBuilder(
123 - valueListenable: _scannerDisabled, 112 + valueListenable: _scannerEnabled,
124 builder: (context, value, child) { 113 builder: (context, value, child) {
125 return Crosshair( 114 return Crosshair(
126 - scannerDisabled: value, 115 + scannerEnabled: value,
127 ); 116 );
128 }, 117 },
129 ), 118 ),
@@ -132,7 +121,6 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist> @@ -132,7 +121,6 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
132 ); 121 );
133 }, 122 },
134 ), 123 ),
135 - ),  
136 ); 124 );
137 } 125 }
138 } 126 }
1 import 'package:flutter/material.dart'; 1 import 'package:flutter/material.dart';
  2 +import 'package:mobile_scanner/mobile_scanner.dart';
2 3
3 -bool isPointInPolygon(Offset point, List<Offset> polygon) { 4 +bool isBarcodeAtCenterOfImage({
  5 + required Size cameraOutputSize,
  6 + required Barcode barcode,
  7 +}) {
  8 + final centerOfCameraOutput = Offset(
  9 + cameraOutputSize.width / 2,
  10 + cameraOutputSize.height / 2,
  11 + );
  12 + debugPrint(cameraOutputSize.toString());
  13 + return _isPointInPolygon(
  14 + point: centerOfCameraOutput,
  15 + polygon: barcode.corners,
  16 + );
  17 +}
  18 +
  19 +//This is what chatGPT came up with.
  20 +//https://en.wikipedia.org/wiki/Point_in_polygon
  21 +bool _isPointInPolygon({
  22 + required Offset point,
  23 + required List<Offset> polygon,
  24 +}) {
4 int i; 25 int i;
5 int j = polygon.length - 1; 26 int j = polygon.length - 1;
6 bool inside = false; 27 bool inside = false;
@@ -3,17 +3,17 @@ import 'package:flutter/material.dart'; @@ -3,17 +3,17 @@ import 'package:flutter/material.dart';
3 class Crosshair extends StatelessWidget { 3 class Crosshair extends StatelessWidget {
4 const Crosshair({ 4 const Crosshair({
5 super.key, 5 super.key,
6 - required this.scannerDisabled, 6 + required this.scannerEnabled,
7 }); 7 });
8 8
9 - final bool scannerDisabled; 9 + final bool scannerEnabled;
10 10
11 @override 11 @override
12 Widget build(BuildContext context) { 12 Widget build(BuildContext context) {
13 return Center( 13 return Center(
14 child: Icon( 14 child: Icon(
15 Icons.close, 15 Icons.close,
16 - color: scannerDisabled ? Colors.green : Colors.red, 16 + color: scannerEnabled ? Colors.red : Colors.green,
17 ), 17 ),
18 ); 18 );
19 } 19 }