I’m trying to use the Google ML Kit’s Text Recognition API in Flutter to extract text from an image after processing it. However, I’m encountering an error when converting the processed image into an InputImage. Here’s the relevant part of my code:
Future<void> _extractTextFromProcessedImage(img.Image image) async {
final imgBytes = Uint8List.fromList(img.encodeJpg(image));
final metadata = InputImageMetadata(
format: InputImageFormat.yuv420,
size: Size(image.width.toDouble(), image.height.toDouble()),
rotation: InputImageRotation.rotation0deg,
bytesPerRow: 0); // ignored
final inputImage = InputImage.fromBytes(bytes: imgBytes, metadata: metadata);
try {
final recognizedText = await textRecognizer.processImage(inputImage);
setState(() {
_extractedText = recognizedText.text;
});
} catch (e) {
print('Error recognizing text: $e');
}
}
When running the code, I get the following error:
2024-12-07 15:02:38.583 14697-14697 ImageError com.example.image_text_extractor E java.lang.IllegalArgumentException
2024-12-07 15:02:38.596 14697-14887 flutter com.example.image_text_extractor I Error recognizing text: PlatformException(InputImageConverterError, java.lang.IllegalArgumentException, null, null)
Details:
-
The error message PlatformException(InputImageConverterError, java.lang.IllegalArgumentException) indicates an issue during the image conversion process.
-
I am encoding the processed image to a JPG byte array using the img.encodeJpg(image) method.
-
The image format is set to InputImageFormat.yuv420, but I’m not sure if that’s appropriate for a JPG byte array.
My question:
-
Is the InputImageFormat.yuv420 the right format for a JPG image? If not, what should the format be for JPG images?
-
Am I handling the image conversion process incorrectly? How should I process the image before passing it to InputImage.fromBytes()?
-
Any suggestions for resolving the PlatformException related to the image conversion error?
**
pubspec.yaml** dependencies:
dependencies:
flutter:
sdk: flutter
google_mlkit_text_recognition: ^0.14.0
image_picker: ^1.1.2
image: ^3.0.1
Thanks in advance!
What I’ve tried:
- I attempted to use the encodeJpg(image) method to convert the image into a byte array.
- I set the format as InputImageFormat.yuv420, which might not be suitable for a JPG image.
- I’ve checked the image metadata, and it’s being passed as size, rotation, and bytesPerRow.
kingii is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.