From 29733a08ec0d5545aef45429bbedd3122fdd1ce2 Mon Sep 17 00:00:00 2001 From: gorkemhacioglu Date: Sat, 1 Mar 2025 21:29:45 +0300 Subject: [PATCH 1/4] Implemented functionality to get processed image data as base64 --- example/pubspec.lock | 2 +- lib/flutter_scalable_ocr.dart | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) 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..5eeb6aa 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.getScannedImageBase64}) : super(key: key); /// Offset on recalculated image left @@ -59,6 +61,9 @@ class ScalableOCR extends StatefulWidget { /// Lock camera orientation final bool lockCamera; + /// Returns the base64 encoded string of the last scanned image + final Function(String)? getScannedImageBase64; + @override ScalableOCRState createState() => ScalableOCRState(); } @@ -84,6 +89,7 @@ class ScalableOCRState extends State { double maxWidth = 0; double maxHeight = 0; String convertingAmount = ""; + String? _lastImageBase64; @override void initState() { @@ -265,6 +271,14 @@ class ScalableOCRState extends State { } final bytes = allBytes.done().buffer.asUint8List(); + // Convert bytes to base64 + final base64Image = base64Encode(bytes); + _lastImageBase64 = base64Image; + + if (widget.getScannedImageBase64 != null) { + widget.getScannedImageBase64!(base64Image); + } + final Size imageSize = Size(image.width.toDouble(), image.height.toDouble()); @@ -275,16 +289,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 +313,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; From 5d56142aab33c99bb1b680fae3ed9702628eda83 Mon Sep 17 00:00:00 2001 From: gorkemhacioglu Date: Sat, 1 Mar 2025 21:44:57 +0300 Subject: [PATCH 2/4] Fixed stress issue --- lib/flutter_scalable_ocr.dart | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/flutter_scalable_ocr.dart b/lib/flutter_scalable_ocr.dart index 5eeb6aa..7efda84 100644 --- a/lib/flutter_scalable_ocr.dart +++ b/lib/flutter_scalable_ocr.dart @@ -62,7 +62,7 @@ class ScalableOCR extends StatefulWidget { final bool lockCamera; /// Returns the base64 encoded string of the last scanned image - final Function(String)? getScannedImageBase64; + final Function(String) getScannedImageBase64; @override ScalableOCRState createState() => ScalableOCRState(); @@ -89,7 +89,7 @@ class ScalableOCRState extends State { double maxWidth = 0; double maxHeight = 0; String convertingAmount = ""; - String? _lastImageBase64; + String _lastImageBase64 = ""; @override void initState() { @@ -275,10 +275,6 @@ class ScalableOCRState extends State { final base64Image = base64Encode(bytes); _lastImageBase64 = base64Image; - if (widget.getScannedImageBase64 != null) { - widget.getScannedImageBase64!(base64Image); - } - final Size imageSize = Size(image.width.toDouble(), image.height.toDouble()); @@ -393,6 +389,7 @@ class ScalableOCRState extends State { inputImage.metadata!.rotation, renderBox, (value) { widget.getScannedText(value); + widget.getScannedImageBase64(_lastImageBase64); }, getRawData: (value) { if (widget.getRawData != null) { widget.getRawData!(value); From d94d3cedf8c76ff5cc86a7e012655640cb120a6f Mon Sep 17 00:00:00 2001 From: gorkemhacioglu Date: Sun, 2 Mar 2025 15:31:18 +0300 Subject: [PATCH 3/4] Implemented getImageBase64 --- lib/flutter_scalable_ocr.dart | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/flutter_scalable_ocr.dart b/lib/flutter_scalable_ocr.dart index 7efda84..0a30466 100644 --- a/lib/flutter_scalable_ocr.dart +++ b/lib/flutter_scalable_ocr.dart @@ -25,7 +25,7 @@ class ScalableOCR extends StatefulWidget { this.cameraSelection = 0, this.torchOn, this.lockCamera = true, - this.getScannedImageBase64}) + this.getScannedImageBase64Function}) : super(key: key); /// Offset on recalculated image left @@ -61,8 +61,8 @@ class ScalableOCR extends StatefulWidget { /// Lock camera orientation final bool lockCamera; - /// Returns the base64 encoded string of the last scanned image - final Function(String) getScannedImageBase64; + /// Function to get base64 image on demand + final Function(Future Function())? getScannedImageBase64Function; @override ScalableOCRState createState() => ScalableOCRState(); @@ -89,11 +89,13 @@ class ScalableOCRState extends State { double maxWidth = 0; double maxHeight = 0; String convertingAmount = ""; - String _lastImageBase64 = ""; @override void initState() { super.initState(); + if (widget.getScannedImageBase64Function != null) { + widget.getScannedImageBase64Function!(getImageBase64); + } startLiveFeed(); } @@ -271,10 +273,6 @@ class ScalableOCRState extends State { } final bytes = allBytes.done().buffer.asUint8List(); - // Convert bytes to base64 - final base64Image = base64Encode(bytes); - _lastImageBase64 = base64Image; - final Size imageSize = Size(image.width.toDouble(), image.height.toDouble()); @@ -389,7 +387,6 @@ class ScalableOCRState extends State { inputImage.metadata!.rotation, renderBox, (value) { widget.getScannedText(value); - widget.getScannedImageBase64(_lastImageBase64); }, getRawData: (value) { if (widget.getRawData != null) { widget.getRawData!(value); @@ -415,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); + } } From 7fae5c868496e86d526a03def6f3677803034496 Mon Sep 17 00:00:00 2001 From: gorkemhacioglu Date: Sun, 2 Mar 2025 15:34:12 +0300 Subject: [PATCH 4/4] Renamed function to getImageBase64 --- lib/flutter_scalable_ocr.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/flutter_scalable_ocr.dart b/lib/flutter_scalable_ocr.dart index 0a30466..c561679 100644 --- a/lib/flutter_scalable_ocr.dart +++ b/lib/flutter_scalable_ocr.dart @@ -25,7 +25,7 @@ class ScalableOCR extends StatefulWidget { this.cameraSelection = 0, this.torchOn, this.lockCamera = true, - this.getScannedImageBase64Function}) + this.getImageBase64}) : super(key: key); /// Offset on recalculated image left @@ -62,7 +62,7 @@ class ScalableOCR extends StatefulWidget { final bool lockCamera; /// Function to get base64 image on demand - final Function(Future Function())? getScannedImageBase64Function; + final Function(Future Function())? getImageBase64; @override ScalableOCRState createState() => ScalableOCRState(); @@ -93,8 +93,8 @@ class ScalableOCRState extends State { @override void initState() { super.initState(); - if (widget.getScannedImageBase64Function != null) { - widget.getScannedImageBase64Function!(getImageBase64); + if (widget.getImageBase64 != null) { + widget.getImageBase64!(getImageBase64); } startLiveFeed(); }