Skip to content
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

refactor: Adds better error handling for image format YUV_420_888. #730

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.google_mlkit_commons;

import android.content.Context;
import android.graphics.ImageFormat;
import android.net.Uri;
import android.util.Log;

Expand All @@ -17,8 +18,8 @@ public class InputImageConverter {

//Returns an [InputImage] from the image data received
public static InputImage getInputImageFromData(Map<String, Object> imageData,
Context context,
MethodChannel.Result result) {
Context context,
MethodChannel.Result result) {
//Differentiates whether the image data is a path for a image file or contains image data in form of bytes
String model = (String) imageData.get("type");
InputImage inputImage;
Expand All @@ -29,7 +30,7 @@ public static InputImage getInputImageFromData(Map<String, Object> imageData,
} catch (IOException e) {
Log.e("ImageError", "Getting Image failed");
Log.e("ImageError", e.toString());
result.error("InputImageConverterError", e.toString(), null);
result.error("InputImageConverterError", e.toString(), e);
return null;
}
} else {
Expand All @@ -39,16 +40,27 @@ public static InputImage getInputImageFromData(Map<String, Object> imageData,
Map<String, Object> metaData = (Map<String, Object>) imageData.get("metadata");

assert metaData != null;
inputImage = InputImage.fromByteArray((byte[]) Objects.requireNonNull(imageData.get("bytes")),
Double.valueOf(Objects.requireNonNull(metaData.get("width")).toString()).intValue(),
Double.valueOf(Objects.requireNonNull(metaData.get("height")).toString()).intValue(),
Integer.parseInt(Objects.requireNonNull(metaData.get("rotation")).toString()),
Integer.parseInt(Objects.requireNonNull(metaData.get("image_format")).toString()));
return inputImage;
byte[] data = (byte[]) Objects.requireNonNull(imageData.get("bytes"));
int imageFormat = Integer.parseInt(Objects.requireNonNull(metaData.get("image_format")).toString());
int rotationDegrees = Integer.parseInt(Objects.requireNonNull(metaData.get("rotation")).toString());
int width = Double.valueOf(Objects.requireNonNull(metaData.get("width")).toString()).intValue();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to:

int width = Integer.parseInt(Objects.requireNonNull(metaData.get("width")).toString());

do the same for height

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize, I think I was too quick at resolving the last comment on this.

On the dart side, where this JSON object is created, it is set as double. That is because the dart type of Size.weight and Size.height is double.

Considering that, I feel the code does the right thing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

int height = Double.valueOf(Objects.requireNonNull(metaData.get("height")).toString()).intValue();
if (imageFormat == ImageFormat.NV21 || imageFormat == ImageFormat.YV12) {
return InputImage.fromByteArray(
data,
width,
height,
rotationDegrees,
imageFormat);
}
result.error("InputImageConverterError", "ImageFormat is not supported.", null);
// TODO: Use InputImage.fromMediaImage, which supports more types, e.g. IMAGE_FORMAT_YUV_420_888.
// See https://developers.google.com/android/reference/com/google/mlkit/vision/common/InputImage#fromMediaImage(android.media.Image,%20int)
return null;
} catch (Exception e) {
Log.e("ImageError", "Getting Image failed");
Log.e("ImageError", e.toString());
result.error("InputImageConverterError", e.toString(), null);
result.error("InputImageConverterError", e.toString(), e);
return null;
}
} else {
Expand Down
Loading