Browse Source

added pinch to zoom support

pull/3/head
Khoren Markosyan 3 years ago
parent
commit
24fd6f9e2d
  1. 45
      lib/reader_widget.dart

45
lib/reader_widget.dart

@ -18,7 +18,9 @@ class ReaderWidget extends StatefulWidget {
this.codeFormat = Format.Any, this.codeFormat = Format.Any,
this.beep = true, this.beep = true,
this.showCroppingRect = true, this.showCroppingRect = true,
this.scannerOverlay,
this.showFlashlight = true, this.showFlashlight = true,
this.allowPinchZoom = true,
this.scanDelay = const Duration(milliseconds: 1000), // 1000ms delay this.scanDelay = const Duration(milliseconds: 1000), // 1000ms delay
this.cropPercent = 0.5, // 50% of the screen this.cropPercent = 0.5, // 50% of the screen
this.resolution = ResolutionPreset.high, this.resolution = ResolutionPreset.high,
@ -29,7 +31,9 @@ class ReaderWidget extends StatefulWidget {
final int codeFormat; final int codeFormat;
final bool beep; final bool beep;
final bool showCroppingRect; final bool showCroppingRect;
final ScannerOverlay? scannerOverlay;
final bool showFlashlight; final bool showFlashlight;
final bool allowPinchZoom;
final Duration scanDelay; final Duration scanDelay;
final double cropPercent; final double cropPercent;
final ResolutionPreset resolution; final ResolutionPreset resolution;
@ -44,6 +48,11 @@ class _ReaderWidgetState extends State<ReaderWidget>
CameraController? controller; CameraController? controller;
var _cameraOn = false; var _cameraOn = false;
double _zoom = 1.0;
double _scaleFactor = 1.0;
double _maxZoomLevel = 1.0;
double _minZoomLevel = 1.0;
bool isAndroid() => Theme.of(context).platform == TargetPlatform.android; bool isAndroid() => Theme.of(context).platform == TargetPlatform.android;
// true when code detecting is ongoing // true when code detecting is ongoing
@ -111,16 +120,21 @@ class _ReaderWidgetState extends State<ReaderWidget>
imageFormatGroup: imageFormatGroup:
isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.bgra8888, isAndroid() ? ImageFormatGroup.yuv420 : ImageFormatGroup.bgra8888,
); );
final CameraController? cameraController = controller;
if (cameraController == null) {
return;
}
try { try {
await controller?.initialize(); await cameraController.initialize();
await controller?.setFlashMode(FlashMode.off); await cameraController.setFlashMode(FlashMode.off);
controller?.startImageStream(processCameraImage); _maxZoomLevel = await cameraController.getMaxZoomLevel();
_minZoomLevel = await cameraController.getMinZoomLevel();
cameraController.startImageStream(processCameraImage);
} on CameraException catch (e) { } on CameraException catch (e) {
debugPrint('${e.code}: ${e.description}'); debugPrint('${e.code}: ${e.description}');
} }
controller?.addListener(() { cameraController.addListener(() {
if (mounted) setState(() {}); if (mounted) setState(() {});
}); });
@ -129,7 +143,7 @@ class _ReaderWidgetState extends State<ReaderWidget>
setState(() {}); setState(() {});
} }
widget.onControllerCreated?.call(controller); widget.onControllerCreated?.call(cameraController);
} }
processCameraImage(CameraImage image) async { processCameraImage(CameraImage image) async {
@ -208,9 +222,10 @@ class _ReaderWidgetState extends State<ReaderWidget>
if (widget.showCroppingRect) if (widget.showCroppingRect)
Container( Container(
decoration: ShapeDecoration( decoration: ShapeDecoration(
shape: ScannerOverlay( shape: widget.scannerOverlay ??
ScannerOverlay(
borderColor: Theme.of(context).primaryColor, borderColor: Theme.of(context).primaryColor,
overlayColor: const Color.fromRGBO(0, 0, 0, 0.5), overlayColor: Colors.black45,
borderRadius: 1, borderRadius: 1,
borderLength: 16, borderLength: 16,
borderWidth: 8, borderWidth: 8,
@ -218,9 +233,20 @@ class _ReaderWidgetState extends State<ReaderWidget>
), ),
), ),
), ),
if (widget.allowPinchZoom)
GestureDetector(
onScaleStart: (details) {
_zoom = _scaleFactor;
},
onScaleUpdate: (details) {
_scaleFactor =
(_zoom * details.scale).clamp(_minZoomLevel, _maxZoomLevel);
cameraController.setZoomLevel(_scaleFactor);
},
),
if (widget.showFlashlight) if (widget.showFlashlight)
Positioned( Positioned(
top: 20, bottom: 20,
left: 20, left: 20,
child: FloatingActionButton( child: FloatingActionButton(
onPressed: () { onPressed: () {
@ -237,7 +263,6 @@ class _ReaderWidgetState extends State<ReaderWidget>
cameraController.setFlashMode(mode); cameraController.setFlashMode(mode);
setState(() {}); setState(() {});
}, },
mini: true,
backgroundColor: Colors.black26, backgroundColor: Colors.black26,
child: Icon(_flashIcon(cameraController)), child: Icon(_flashIcon(cameraController)),
), ),

Loading…
Cancel
Save