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-12-29 15:23:58 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
a51f9f6309310128b4dfb9c244fca5332aa0fe6f
a51f9f63
1 parent
d28930d1
format
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
57 additions
and
24 deletions
lib/src/web/barcode_reader.dart
lib/src/web/media_track_constraints_delegate.dart
lib/src/web/mobile_scanner_web.dart
lib/src/web/zxing/result.dart
lib/src/web/zxing/zxing_barcode_reader.dart
lib/src/web/barcode_reader.dart
View file @
a51f9f6
...
...
@@ -96,7 +96,9 @@ abstract class BarcodeReader {
}
/// Set a listener for the media stream settings.
void
setMediaTrackSettingsListener
(
void
Function
(
MediaTrackSettings
)
listener
)
{
void
setMediaTrackSettingsListener
(
void
Function
(
MediaTrackSettings
)
listener
,
)
{
throw
UnimplementedError
(
'setMediaTrackConstraintsListener() has not been implemented.'
,
);
...
...
lib/src/web/media_track_constraints_delegate.dart
View file @
a51f9f6
...
...
@@ -10,9 +10,9 @@ final class MediaTrackConstraintsDelegate {
/// Get the settings for the given [mediaStream].
MediaTrackSettings
?
getSettings
(
MediaStream
?
mediaStream
)
{
final
List
<
JSAny
?>
tracks
=
mediaStream
?.
getVideoTracks
().
toDart
??
const
[]
;
final
List
<
JSAny
?>
?
tracks
=
mediaStream
?.
getVideoTracks
().
toDart
;
if
(
tracks
.
isEmpty
)
{
if
(
tracks
==
null
||
tracks
.
isEmpty
)
{
return
null
;
}
...
...
@@ -24,7 +24,9 @@ final class MediaTrackConstraintsDelegate {
/// Returns a list of supported flashlight modes for the given [mediaStream].
///
/// The [TorchState.off] mode is always supported, regardless of the return value.
Future
<
List
<
TorchState
>>
getSupportedFlashlightModes
(
MediaStream
?
mediaStream
)
async
{
Future
<
List
<
TorchState
>>
getSupportedFlashlightModes
(
MediaStream
?
mediaStream
,
)
async
{
if
(
mediaStream
==
null
)
{
return
[];
}
...
...
@@ -61,7 +63,10 @@ final class MediaTrackConstraintsDelegate {
}
/// Set the flashlight state of the given [mediaStream] to the given [value].
Future
<
void
>
setFlashlightState
(
MediaStream
?
mediaStream
,
TorchState
value
)
async
{
Future
<
void
>
setFlashlightState
(
MediaStream
?
mediaStream
,
TorchState
value
,
)
async
{
if
(
mediaStream
==
null
)
{
return
;
}
...
...
lib/src/web/mobile_scanner_web.dart
View file @
a51f9f6
...
...
@@ -23,7 +23,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
final
BarcodeReader
_barcodeReader
=
ZXingBarcodeReader
();
/// The stream controller for the barcode stream.
final
StreamController
<
BarcodeCapture
>
_barcodesController
=
StreamController
.
broadcast
();
final
StreamController
<
BarcodeCapture
>
_barcodesController
=
StreamController
.
broadcast
();
/// The subscription for the barcode stream.
StreamSubscription
<
Object
?>?
_barcodesSubscription
;
...
...
@@ -34,7 +35,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
HTMLDivElement
?
_divElement
;
/// The stream controller for the media track settings stream.
final
StreamController
<
MediaTrackSettings
>
_settingsController
=
StreamController
.
broadcast
();
final
StreamController
<
MediaTrackSettings
>
_settingsController
=
StreamController
.
broadcast
();
/// The view type for the platform view factory.
final
String
_viewType
=
'MobileScannerWeb'
;
...
...
@@ -70,7 +72,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
throw
const
MobileScannerException
(
errorCode:
MobileScannerErrorCode
.
controllerUninitialized
,
errorDetails:
MobileScannerErrorDetails
(
message:
'The controller was not yet initialized. Call start() before calling buildCameraView().'
,
message:
'The controller was not yet initialized. Call start() before calling buildCameraView().'
,
),
);
}
...
...
@@ -98,7 +101,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
throw
const
MobileScannerException
(
errorCode:
MobileScannerErrorCode
.
controllerAlreadyInitialized
,
errorDetails:
MobileScannerErrorDetails
(
message:
'The scanner was already started. Call stop() before calling start() again.'
,
message:
'The scanner was already started. Call stop() before calling start() again.'
,
),
);
}
...
...
@@ -108,7 +112,9 @@ class MobileScannerWeb extends MobileScannerPlatform {
_barcodesController
.
add
(
const
BarcodeCapture
());
// Listen for changes to the media track settings.
_barcodeReader
.
setMediaTrackSettingsListener
(
_handleMediaTrackSettingsChange
);
_barcodeReader
.
setMediaTrackSettingsListener
(
_handleMediaTrackSettingsChange
,
);
await
_barcodeReader
.
start
(
startOptions
,
...
...
@@ -120,7 +126,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
MobileScannerErrorCode
errorCode
=
MobileScannerErrorCode
.
genericError
;
if
(
error
is
DOMException
)
{
if
(
errorMessage
.
contains
(
'NotFoundError'
)
||
errorMessage
.
contains
(
'NotSupportedError'
))
{
if
(
errorMessage
.
contains
(
'NotFoundError'
)
||
errorMessage
.
contains
(
'NotSupportedError'
))
{
errorCode
=
MobileScannerErrorCode
.
unsupported
;
}
else
if
(
errorMessage
.
contains
(
'NotAllowedError'
))
{
errorCode
=
MobileScannerErrorCode
.
permissionDenied
;
...
...
@@ -137,13 +144,15 @@ class MobileScannerWeb extends MobileScannerPlatform {
}
try
{
_barcodesSubscription
=
_barcodeReader
.
detectBarcodes
().
listen
((
BarcodeCapture
barcode
)
{
_barcodesSubscription
=
_barcodeReader
.
detectBarcodes
().
listen
(
(
BarcodeCapture
barcode
)
{
if
(
_barcodesController
.
isClosed
)
{
return
;
}
_barcodesController
.
add
(
barcode
);
});
},
);
final
bool
hasTorch
=
await
_barcodeReader
.
hasTorch
();
...
...
lib/src/web/zxing/result.dart
View file @
a51f9f6
...
...
@@ -95,7 +95,8 @@ extension ResultExt on Result {
/// Get the raw bytes of the result.
Uint8List
?
get
rawBytes
{
final
JSUint8Array
?
rawBytes
=
getRawBytes
.
callAsFunction
()
as
JSUint8Array
?;
final
JSUint8Array
?
rawBytes
=
getRawBytes
.
callAsFunction
()
as
JSUint8Array
?;
return
rawBytes
?.
toDart
;
}
...
...
lib/src/web/zxing/zxing_barcode_reader.dart
View file @
a51f9f6
...
...
@@ -22,7 +22,8 @@ final class ZXingBarcodeReader extends BarcodeReader {
void
Function
(
web
.
MediaTrackSettings
)?
_onMediaTrackSettingsChanged
;
/// The internal media stream track constraints delegate.
final
MediaTrackConstraintsDelegate
_mediaTrackConstraintsDelegate
=
const
MediaTrackConstraintsDelegate
();
final
MediaTrackConstraintsDelegate
_mediaTrackConstraintsDelegate
=
const
MediaTrackConstraintsDelegate
();
/// The internal barcode reader.
ZXingBrowserMultiFormatReader
?
_reader
;
...
...
@@ -93,7 +94,8 @@ final class ZXingBarcodeReader extends BarcodeReader {
return
null
;
}
final
capabilities
=
web
.
window
.
navigator
.
mediaDevices
.
getSupportedConstraints
();
final
capabilities
=
web
.
window
.
navigator
.
mediaDevices
.
getSupportedConstraints
();
final
web
.
MediaStreamConstraints
constraints
;
...
...
@@ -112,7 +114,9 @@ final class ZXingBarcodeReader extends BarcodeReader {
);
}
final
JSAny
?
mediaStream
=
await
web
.
window
.
navigator
.
mediaDevices
.
getUserMedia
(
constraints
).
toDart
;
final
JSAny
?
mediaStream
=
await
web
.
window
.
navigator
.
mediaDevices
.
getUserMedia
(
constraints
)
.
toDart
;
return
mediaStream
as
web
.
MediaStream
?;
}
...
...
@@ -135,11 +139,13 @@ final class ZXingBarcodeReader extends BarcodeReader {
final
web
.
MediaStream
?
stream
=
await
_prepareMediaStream
(
cameraDirection
);
if
(
stream
!=
null
)
{
final
JSPromise
?
result
=
_reader
?.
attachStreamToVideo
.
callAsFunction
(
null
,
stream
,
videoElement
)
as
JSPromise
?;
final
JSPromise
?
result
=
_reader
?.
attachStreamToVideo
.
callAsFunction
(
null
,
stream
,
videoElement
)
as
JSPromise
?;
await
result
?.
toDart
;
final
web
.
MediaTrackSettings
?
settings
=
_mediaTrackConstraintsDelegate
.
getSettings
(
stream
);
final
web
.
MediaTrackSettings
?
settings
=
_mediaTrackConstraintsDelegate
.
getSettings
(
stream
);
if
(
settings
!=
null
)
{
_onMediaTrackSettingsChanged
?.
call
(
settings
);
...
...
@@ -193,7 +199,9 @@ final class ZXingBarcodeReader extends BarcodeReader {
}
@override
void
setMediaTrackSettingsListener
(
void
Function
(
web
.
MediaTrackSettings
)
listener
)
{
void
setMediaTrackSettingsListener
(
void
Function
(
web
.
MediaTrackSettings
)
listener
,
)
{
_onMediaTrackSettingsChanged
??=
listener
;
}
...
...
@@ -210,9 +218,13 @@ final class ZXingBarcodeReader extends BarcodeReader {
return
Future
<
void
>.
value
();
}
await
_mediaTrackConstraintsDelegate
.
setFlashlightState
(
mediaStream
,
value
);
await
_mediaTrackConstraintsDelegate
.
setFlashlightState
(
mediaStream
,
value
,
);
final
web
.
MediaTrackSettings
?
settings
=
_mediaTrackConstraintsDelegate
.
getSettings
(
mediaStream
);
final
web
.
MediaTrackSettings
?
settings
=
_mediaTrackConstraintsDelegate
.
getSettings
(
mediaStream
);
if
(
settings
!=
null
)
{
_onMediaTrackSettingsChanged
?.
call
(
settings
);
...
...
@@ -221,7 +233,10 @@ final class ZXingBarcodeReader extends BarcodeReader {
}
@override
Future
<
void
>
start
(
StartOptions
options
,
{
required
web
.
HTMLElement
containerElement
})
async
{
Future
<
void
>
start
(
StartOptions
options
,
{
required
web
.
HTMLElement
containerElement
,
})
async
{
final
int
detectionTimeoutMs
=
options
.
detectionTimeoutMs
;
final
List
<
BarcodeFormat
>
formats
=
options
.
formats
;
...
...
@@ -243,7 +258,8 @@ final class ZXingBarcodeReader extends BarcodeReader {
_reader
=
ZXingBrowserMultiFormatReader
(
hints
.
jsify
(),
detectionTimeoutMs
);
final
web
.
HTMLVideoElement
videoElement
=
web
.
document
.
createElement
(
'video'
)
as
web
.
HTMLVideoElement
;
final
web
.
HTMLVideoElement
videoElement
=
web
.
document
.
createElement
(
'video'
)
as
web
.
HTMLVideoElement
;
await
_prepareVideoElement
(
videoElement
,
...
...
Please
register
or
login
to post a comment