diff --git a/example/pubspec.lock b/example/pubspec.lock index dd35c3f..f5b00d2 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -132,7 +132,7 @@ packages: path: ".." relative: true source: path - version: "2.2.0" + version: "2.2.1" flutter_test: dependency: "direct dev" description: flutter diff --git a/lib/flutter_scalable_ocr.dart b/lib/flutter_scalable_ocr.dart index 1196c13..c561679 100644 --- a/lib/flutter_scalable_ocr.dart +++ b/lib/flutter_scalable_ocr.dart @@ -2,6 +2,7 @@ library flutter_scalable_ocr; import 'dart:developer'; import 'dart:io'; +import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -23,7 +24,8 @@ class ScalableOCR extends StatefulWidget { this.paintboxCustom, this.cameraSelection = 0, this.torchOn, - this.lockCamera = true}) + this.lockCamera = true, + this.getImageBase64}) : super(key: key); /// Offset on recalculated image left @@ -59,6 +61,9 @@ class ScalableOCR extends StatefulWidget { /// Lock camera orientation final bool lockCamera; + /// Function to get base64 image on demand + final Function(Future Function())? getImageBase64; + @override ScalableOCRState createState() => ScalableOCRState(); } @@ -88,6 +93,9 @@ class ScalableOCRState extends State { @override void initState() { super.initState(); + if (widget.getImageBase64 != null) { + widget.getImageBase64!(getImageBase64); + } startLiveFeed(); } @@ -275,16 +283,19 @@ class ScalableOCRState extends State { if (Platform.isIOS) { imageRotation = InputImageRotationValue.fromRawValue(sensorOrientation); } else if (Platform.isAndroid) { - var rotationCompensation = _orientations[_controller!.value.deviceOrientation]; + var rotationCompensation = + _orientations[_controller!.value.deviceOrientation]; if (rotationCompensation == null) return null; if (camera.lensDirection == CameraLensDirection.front) { // front-facing rotationCompensation = (sensorOrientation + rotationCompensation) % 360; } else { // back-facing - rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360; + rotationCompensation = + (sensorOrientation - rotationCompensation + 360) % 360; } - imageRotation = InputImageRotationValue.fromRawValue(rotationCompensation); + imageRotation = + InputImageRotationValue.fromRawValue(rotationCompensation); } if (imageRotation == null) return null; @@ -296,7 +307,8 @@ class ScalableOCRState extends State { // * bgra8888 for iOS if (imageFormat == null || (Platform.isAndroid && imageFormat != InputImageFormat.nv21) || - (Platform.isIOS && imageFormat != InputImageFormat.bgra8888)) return null; + (Platform.isIOS && imageFormat != InputImageFormat.bgra8888)) + return null; // since format is constraint to nv21 or bgra8888, both only have one plane if (image.planes.length != 1) return null; @@ -400,4 +412,14 @@ class ScalableOCRState extends State { } }); } + + Future getImageBase64() async { + if (_controller == null) { + throw Exception('Camera controller is not initialized'); + } + + final image = await _controller!.takePicture(); + final bytes = await image.readAsBytes(); + return base64Encode(bytes); + } }