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
Navaron Bracke
2023-11-08 12:58:17 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
6fd59da44e6444dde02d469a92537a0c04e8798b
6fd59da4
1 parent
c2b57f25
refactor analyze image in the platform interface
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
5 deletions
lib/src/method_channel/mobile_scanner_method_channel.dart
lib/src/mobile_scanner_platform_interface.dart
lib/src/method_channel/mobile_scanner_method_channel.dart
View file @
6fd59da
import
'dart:async'
;
import
'dart:io'
;
import
'package:flutter/services.dart'
;
import
'package:flutter/widgets.dart'
;
import
'package:mobile_scanner/src/enums/barcode_format.dart'
;
import
'package:mobile_scanner/src/enums/camera_facing.dart'
;
import
'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart'
;
import
'package:mobile_scanner/src/enums/torch_state.dart'
;
import
'package:mobile_scanner/src/mobile_scanner_exception.dart'
;
import
'package:mobile_scanner/src/mobile_scanner_platform_interface.dart'
;
import
'package:mobile_scanner/src/objects/barcode.dart'
;
import
'package:mobile_scanner/src/objects/barcode_capture.dart'
;
/// An implementation of [MobileScannerPlatform] that uses method channels.
class
MethodChannelMobileScanner
extends
MobileScannerPlatform
{
...
...
@@ -31,6 +37,56 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
int
?
_textureId
;
/// Parse a [BarcodeCapture] from the given [event].
BarcodeCapture
?
_parseBarcode
(
Map
<
Object
?,
Object
?>?
event
)
{
if
(
event
==
null
)
{
return
null
;
}
final
Object
?
data
=
event
[
'data'
];
if
(
data
==
null
||
data
is
!
List
<
Object
?>)
{
return
null
;
}
final
List
<
Map
<
Object
?,
Object
?>>
barcodes
=
data
.
cast
<
Map
<
Object
?,
Object
?>>();
if
(
Platform
.
isMacOS
)
{
return
BarcodeCapture
(
raw:
event
,
barcodes:
barcodes
.
map
(
(
barcode
)
=>
Barcode
(
rawValue:
barcode
[
'payload'
]
as
String
?,
format:
BarcodeFormat
.
fromRawValue
(
barcode
[
'symbology'
]
as
int
?
??
-
1
,
),
),
)
.
toList
(),
);
}
if
(
Platform
.
isAndroid
||
Platform
.
isIOS
)
{
final
double
?
width
=
event
[
'width'
]
as
double
?;
final
double
?
height
=
event
[
'height'
]
as
double
?;
return
BarcodeCapture
(
raw:
data
,
barcodes:
barcodes
.
map
(
Barcode
.
fromNative
).
toList
(),
image:
event
[
'image'
]
as
Uint8List
?,
size:
width
==
null
||
height
==
null
?
Size
.
zero
:
Size
(
width
,
height
),
);
}
throw
const
MobileScannerException
(
errorCode:
MobileScannerErrorCode
.
genericError
,
errorDetails:
MobileScannerErrorDetails
(
message:
'Only Android, iOS and macOS are supported.'
,
),
);
}
@override
Stream
<
TorchState
>
get
torchStateStream
{
return
eventsStream
...
...
@@ -46,13 +102,13 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
}
@override
Future
<
bool
>
analyzeImage
(
String
path
)
async
{
final
bool
?
result
=
await
methodChannel
.
invokeMethod
<
bool
>(
Future
<
BarcodeCapture
?>
analyzeImage
(
String
path
)
async
{
final
Map
<
String
,
Object
?>?
result
=
await
methodChannel
.
invokeMapMethod
<
String
,
Object
?>(
'analyzeImage'
,
path
,
);
return
result
??
false
;
return
_parseBarcode
(
result
)
;
}
@override
...
...
lib/src/mobile_scanner_platform_interface.dart
View file @
6fd59da
...
...
@@ -2,6 +2,7 @@ import 'package:flutter/widgets.dart';
import
'package:mobile_scanner/src/enums/camera_facing.dart'
;
import
'package:mobile_scanner/src/enums/torch_state.dart'
;
import
'package:mobile_scanner/src/method_channel/mobile_scanner_method_channel.dart'
;
import
'package:mobile_scanner/src/objects/barcode_capture.dart'
;
import
'package:plugin_platform_interface/plugin_platform_interface.dart'
;
/// The platform interface for the `mobile_scanner` plugin.
...
...
@@ -28,8 +29,10 @@ abstract class MobileScannerPlatform extends PlatformInterface {
/// Analyze a local image file for barcodes.
///
/// Returns whether the file at the given [path] contains a barcode.
Future
<
bool
>
analyzeImage
(
String
path
)
{
/// The [path] is the path to the file on disk.
///
/// Returns the barcodes that were found in the image.
Future
<
BarcodeCapture
?>
analyzeImage
(
String
path
)
{
throw
UnimplementedError
(
'analyzeImage() has not been implemented.'
);
}
...
...
Please
register
or
login
to post a comment