ScannerController class
Drives the camera of a BarcodeScanner and decodes its frames.
Frames are decoded as fast as the camera delivers them, one at a time, in the background; there is no fixed delay and no frame queue.
final controller = ScannerController(formats: {BarcodeFormat.qrCode});
BarcodeScanner(controller: controller);
controller.barcodes.listen((capture) => print(capture.barcode.text));
await controller.toggleTorch();
controller.dispose(); // releases the camera
The current camera state is the value (ScannerState); listen to the
controller (or use ValueListenableBuilder) to rebuild on changes.
- Inheritance
-
- Object
- ChangeNotifier
- ValueNotifier<
ScannerState> - ScannerController
Constructors
-
ScannerController({Set<
BarcodeFormat> ? formats, ReaderOptions options = const ReaderOptions.camera(), DetectionMode detection = DetectionMode.distinct, ScanWindow scanWindow = const ScanWindow.centered(), int? maxBarcodes = 1, CameraFacing facing = CameraFacing.back, CameraResolution resolution = CameraResolution.hd, bool autoStart = true, bool stopInBackground = true, bool autoZoom = true, double? initialZoom, bool torch = false}) -
Creates a controller. The camera opens when the first
BarcodeScannerusing it is shown (ifautoStart) or when start is called. - ScannerController.fromTemplate(ScannerTemplate template, {ReaderOptions options = const ReaderOptions.camera(), CameraResolution resolution = CameraResolution.hd, bool autoStart = true, bool stopInBackground = true, bool autoZoom = true, bool torch = false})
-
Creates a controller with the camera settings of
template: its scan window, formats, detection, maxBarcodes, facing and initialZoom, the settingsBarcodeScannergives the controller it creates for a template. For a controller of your own that the template's screen uses:factory
Properties
- autoStart → bool
-
Whether a
BarcodeScannerstarts the camera when it is shown.final -
barcodes
→ Stream<
BarcodeCapture> -
Barcodes found in the camera frames, according to detection.
no setter
- detection ↔ DetectionMode
-
When a barcode is reported. Can be changed while scanning.
getter/setter pair
- facing → CameraFacing
-
The camera in use, or the one start opens: the
facinggiven to the constructor until switchCamera changes it.no setter -
formats
↔ Set<
BarcodeFormat> ? -
Formats to look for;
nullmeans all. Fewer formats decode faster. Can be changed while scanning.getter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- initialZoom ↔ double?
-
The zoom every start sets, such as 2 for small codes;
nullstarts at 1×. Set again after the app returns from the background and after switchCamera, and clamped to what the camera can do. Changed while the camera is open, the camera zooms there at once.getter/setter pair - maxBarcodes ↔ int?
-
How many barcodes to look for in each frame;
nullfor all of them. Can be changed while scanning.getter/setter pair - options ↔ ReaderOptions
-
Search strategies per frame. Can be changed while scanning.
getter/setter pair
- resolution → CameraResolution
-
The camera resolution requested by start.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scanWindow ↔ ScanWindow
-
The part of the preview that is decoded. Can be changed while scanning.
getter/setter pair
- stopInBackground → bool
-
Release the camera while the app is in the background and open it
again when the app returns: the camera is stopped, not paused, so that
other apps can use it. A scanner paused before stays paused.
final
- value ↔ ScannerState
-
The current value stored in this notifier.
getter/setter pairinherited
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
dispose(
) → void -
Discards any resources used by the object.
override
-
lastFrameImage(
) → Future< Image?> - The last decoded camera frame in color, turned and mirrored the way the preview shows it, at most 1280 pixels on its longest side: the whole picture a code was found in, for example to keep it with the result or to freeze the screen on it. After pause it is the frame decoded last before it.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
pause(
) → Future< void> - Stops decoding but keeps the camera and the preview running, e.g. while a result sheet is shown: the camera sends no frames meanwhile, so a paused scanner costs little more than the preview. While the camera is starting, it starts paused.
-
readImage(
Uint8List encodedImage) → Future< BarcodeCapture?> -
Reads the barcodes in an image file, such as a photo picked from the
gallery, with this controller's formats and maxBarcodes, and
reports them on barcodes like a camera frame:
onDetectand the scanner templates see them too. Returns the capture, ornullwhen the image has no barcode. -
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
resume(
) → Future< void> - Resumes decoding after pause (or after a DetectionMode.once result). With DetectionMode.distinct, the codes in view are reported anew.
-
setAutoZoom(
bool enabled) → Future< void> - Turns automatic zoom on or off. While it is on, an iPhone's scanner picks the lens and zoom by itself from where the camera is focused: the ultra-wide camera when a code is too close for the main lens to focus, a longer zoom when it is far, never while codes are being read. Zooming by hand (setZoom) turns it off. Where the camera does not report its focus (AutoZoomState.unavailable) the choice is kept for later.
-
setFocusPoint(
Offset point) → Future< void> -
Focuses and meters exposure at
point, given as fractions of the camera preview as shown, not of the widget: (0, 0) is the preview's top-left corner, (1, 1) its bottom-right, also where the widget covers only part of it (BoxFit.cover). Values outside are clamped.BarcodeScannercalls this on tap; from a tap of your own, map the position with ScannerLayout.previewRect: -
setTorch(
bool enabled) → Future< void> - Turns the torch on or off. No-op if ScannerState.torch is TorchState.unavailable.
-
setZoom(
double zoom) → Future< void> -
Zooms to
zoom, clamped to ScannerState.minZoom … ScannerState.maxZoom. Below 1 an iPhone's ultra-wide camera is used (0.5 is its whole view); it also focuses much closer. Turns automatic zoom off. -
start(
) → Future< void> - Opens the camera and starts decoding.
-
stop(
) → Future< void> - Stops decoding and releases the camera. Stopped in the background, the camera stays closed when the app returns.
-
switchCamera(
[CameraFacing? facing]) → Future< void> -
Switches to the camera with
facing, or to the other one ifnull. -
toggleAutoZoom(
) → Future< void> - Turns automatic zoom on if it is off, and off if it is on.
-
toggleTorch(
) → Future< void> - Turns the torch on if it is off, and off if it is on.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited