From 9eba130c9f06c7d90de2569c69d50eff644fc413 Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Sat, 4 Mar 2023 11:22:39 +0400 Subject: [PATCH] allow to set camera lens direction --- example/lib/main.dart | 1 + lib/flutter_zxing.dart | 2 +- lib/src/ui/reader_widget.dart | 10 +++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index b41a719..ab8a40e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -89,6 +89,7 @@ class _DemoPageState extends State { isMultiScan: isMultiScan, scanDelay: Duration(milliseconds: isMultiScan ? 50 : 500), resolution: ResolutionPreset.high, + lensDirection: CameraLensDirection.back, ), if (showDebugInfo) DebugInfoWidget( diff --git a/lib/flutter_zxing.dart b/lib/flutter_zxing.dart index 9bcc719..693613e 100644 --- a/lib/flutter_zxing.dart +++ b/lib/flutter_zxing.dart @@ -7,7 +7,7 @@ import 'zxing_cross.dart' if (dart.library.io) 'zxing_mobile.dart' if (dart.library.html) 'zxing_web.dart'; -export 'package:camera/camera.dart' show ResolutionPreset; +export 'package:camera/camera.dart' show ResolutionPreset, CameraLensDirection; export 'src/models/models.dart'; export 'src/ui/ui.dart'; diff --git a/lib/src/ui/reader_widget.dart b/lib/src/ui/reader_widget.dart index 32d5e67..9d3be57 100644 --- a/lib/src/ui/reader_widget.dart +++ b/lib/src/ui/reader_widget.dart @@ -37,6 +37,7 @@ class ReaderWidget extends StatefulWidget { this.scanDelaySuccess = const Duration(milliseconds: 1000), this.cropPercent = 0.5, // 50% of the screen this.resolution = ResolutionPreset.high, + this.lensDirection = CameraLensDirection.back, this.loading = const DecoratedBox(decoration: BoxDecoration(color: Colors.black)), }); @@ -111,6 +112,9 @@ class ReaderWidget extends StatefulWidget { /// Camera resolution final ResolutionPreset resolution; + /// Camera lens direction + final CameraLensDirection lensDirection; + /// Delay between scans when a code is detected, will be ignored if isMultiScan is true final Duration scanDelaySuccess; @@ -158,7 +162,11 @@ class _ReaderWidgetState extends State setState(() { this.cameras = cameras; if (cameras.isNotEmpty) { - selectedCamera = cameras.first; + selectedCamera = cameras.firstWhere( + (CameraDescription camera) => + camera.lensDirection == widget.lensDirection, + orElse: () => cameras.first, + ); onNewCameraSelected(selectedCamera); } });