Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Sander Roest
2024-09-17 16:16:13 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
36da196e9e302faa16852572dc8bda4384dbe614
36da196e
1 parent
9f000c4c
Working, but bad recognition in landscape.
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
57 deletions
example/lib/main.dart
example/lib/picklist/barcode_scanner_picklist.dart
example/lib/picklist/classes/detect_collision.dart → example/lib/picklist/classes/barcode_at_center.dart
example/lib/picklist/widgets/crosshair.dart
example/lib/main.dart
View file @
36da196
...
...
@@ -12,8 +12,6 @@ import 'package:mobile_scanner_example/mobile_scanner_overlay.dart';
import
'package:mobile_scanner_example/picklist/picklist_result.dart'
;
void
main
(
)
async
{
WidgetsFlutterBinding
.
ensureInitialized
();
await
SystemChrome
.
setPreferredOrientations
([
DeviceOrientation
.
portraitUp
]);
runApp
(
const
MaterialApp
(
title:
'Mobile Scanner Example'
,
...
...
example/lib/picklist/barcode_scanner_picklist.dart
View file @
36da196
import
'dart:async'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/services.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
import
'package:mobile_scanner_example/picklist/classes/detect_collision.dart'
;
import
'package:mobile_scanner_example/picklist/classes/barcode_at_center.dart'
;
import
'package:mobile_scanner_example/picklist/widgets/crosshair.dart'
;
import
'package:mobile_scanner_example/scanner_error_widget.dart'
;
...
...
@@ -21,13 +21,12 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
);
StreamSubscription
<
Object
?>?
_barcodesSubscription
;
final
_scanner
Disabled
=
ValueNotifier
(
fals
e
);
final
_scanner
Enabled
=
ValueNotifier
(
tru
e
);
bool
barcodeDetected
=
false
;
@override
void
initState
()
{
SystemChrome
.
setPreferredOrientations
([
DeviceOrientation
.
portraitUp
]);
WidgetsBinding
.
instance
.
addObserver
(
this
);
_barcodesSubscription
=
_mobileScannerController
.
barcodes
.
listen
(
_handleBarcodes
,
...
...
@@ -69,17 +68,14 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
}
void
_handleBarcodes
(
BarcodeCapture
capture
)
{
if
(
_scannerDis
abled
.
value
)
{
if
(
!
_scannerEn
abled
.
value
)
{
return
;
}
for
(
final
barcode
in
capture
.
barcodes
)
{
if
(
isPointInPolygon
(
Offset
(
_mobileScannerController
.
value
.
size
.
width
/
2
,
_mobileScannerController
.
value
.
size
.
height
/
2
,
),
barcode
.
corners
,
if
(
isBarcodeAtCenterOfImage
(
cameraOutputSize:
_mobileScannerController
.
value
.
size
,
barcode:
barcode
,
))
{
if
(!
barcodeDetected
)
{
barcodeDetected
=
true
;
...
...
@@ -92,46 +88,38 @@ class _BarcodeScannerPicklistState extends State<BarcodeScannerPicklist>
@override
Widget
build
(
BuildContext
context
)
{
const
boxFit
=
BoxFit
.
contain
;
return
PopScope
(
onPopInvokedWithResult:
(
didPop
,
result
)
{
if
(
didPop
)
{
SystemChrome
.
setPreferredOrientations
([...
DeviceOrientation
.
values
]);
}
},
child:
Scaffold
(
appBar:
AppBar
(
title:
const
Text
(
'Picklist scanner'
)),
backgroundColor:
Colors
.
black
,
body:
StreamBuilder
(
stream:
_mobileScannerController
.
barcodes
,
builder:
(
context
,
snapshot
)
{
return
Listener
(
behavior:
HitTestBehavior
.
opaque
,
onPointerDown:
(
_
)
=>
_scannerDisabled
.
value
=
true
,
onPointerUp:
(
_
)
=>
_scannerDisabled
.
value
=
false
,
onPointerCancel:
(
_
)
=>
_scannerDisabled
.
value
=
false
,
child:
Stack
(
fit:
StackFit
.
expand
,
children:
[
MobileScanner
(
controller:
_mobileScannerController
,
errorBuilder:
(
context
,
error
,
child
)
=>
ScannerErrorWidget
(
error:
error
),
fit:
boxFit
,
),
ValueListenableBuilder
(
valueListenable:
_scannerDisabled
,
builder:
(
context
,
value
,
child
)
{
return
Crosshair
(
scannerDisabled:
value
,
);
},
),
],
),
);
},
),
return
Scaffold
(
appBar:
AppBar
(
title:
const
Text
(
'Picklist scanner'
)),
backgroundColor:
Colors
.
black
,
body:
StreamBuilder
(
stream:
_mobileScannerController
.
barcodes
,
builder:
(
context
,
snapshot
)
{
return
Listener
(
behavior:
HitTestBehavior
.
opaque
,
onPointerDown:
(
_
)
=>
_scannerEnabled
.
value
=
false
,
onPointerUp:
(
_
)
=>
_scannerEnabled
.
value
=
true
,
onPointerCancel:
(
_
)
=>
_scannerEnabled
.
value
=
true
,
child:
Stack
(
fit:
StackFit
.
expand
,
children:
[
MobileScanner
(
controller:
_mobileScannerController
,
errorBuilder:
(
context
,
error
,
child
)
=>
ScannerErrorWidget
(
error:
error
),
fit:
BoxFit
.
contain
,
),
ValueListenableBuilder
(
valueListenable:
_scannerEnabled
,
builder:
(
context
,
value
,
child
)
{
return
Crosshair
(
scannerEnabled:
value
,
);
},
),
],
),
);
},
),
);
}
...
...
example/lib/picklist/classes/
detect_collision
.dart → example/lib/picklist/classes/
barcode_at_center
.dart
View file @
36da196
import
'package:flutter/material.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
bool
isPointInPolygon
(
Offset
point
,
List
<
Offset
>
polygon
)
{
bool
isBarcodeAtCenterOfImage
(
{
required
Size
cameraOutputSize
,
required
Barcode
barcode
,
})
{
final
centerOfCameraOutput
=
Offset
(
cameraOutputSize
.
width
/
2
,
cameraOutputSize
.
height
/
2
,
);
debugPrint
(
cameraOutputSize
.
toString
());
return
_isPointInPolygon
(
point:
centerOfCameraOutput
,
polygon:
barcode
.
corners
,
);
}
//This is what chatGPT came up with.
//https://en.wikipedia.org/wiki/Point_in_polygon
bool
_isPointInPolygon
(
{
required
Offset
point
,
required
List
<
Offset
>
polygon
,
})
{
int
i
;
int
j
=
polygon
.
length
-
1
;
bool
inside
=
false
;
...
...
example/lib/picklist/widgets/crosshair.dart
View file @
36da196
...
...
@@ -3,17 +3,17 @@ import 'package:flutter/material.dart';
class
Crosshair
extends
StatelessWidget
{
const
Crosshair
({
super
.
key
,
required
this
.
scanner
Dis
abled
,
required
this
.
scanner
En
abled
,
});
final
bool
scanner
Dis
abled
;
final
bool
scanner
En
abled
;
@override
Widget
build
(
BuildContext
context
)
{
return
Center
(
child:
Icon
(
Icons
.
close
,
color:
scanner
Disabled
?
Colors
.
green
:
Colors
.
red
,
color:
scanner
Enabled
?
Colors
.
red
:
Colors
.
green
,
),
);
}
...
...
Please
register
or
login
to post a comment