Skip to content

Implemented functionality to get processed image data as base64 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions lib/flutter_scalable_ocr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -59,6 +61,9 @@ class ScalableOCR extends StatefulWidget {
/// Lock camera orientation
final bool lockCamera;

/// Function to get base64 image on demand
final Function(Future<String> Function())? getImageBase64;

@override
ScalableOCRState createState() => ScalableOCRState();
}
Expand Down Expand Up @@ -88,6 +93,9 @@ class ScalableOCRState extends State<ScalableOCR> {
@override
void initState() {
super.initState();
if (widget.getImageBase64 != null) {
widget.getImageBase64!(getImageBase64);
}
startLiveFeed();
}

Expand Down Expand Up @@ -275,16 +283,19 @@ class ScalableOCRState extends State<ScalableOCR> {
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;

Expand All @@ -296,7 +307,8 @@ class ScalableOCRState extends State<ScalableOCR> {
// * 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;
Expand Down Expand Up @@ -400,4 +412,14 @@ class ScalableOCRState extends State<ScalableOCR> {
}
});
}

Future<String> 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);
}
}