Skip to content

Commit

Permalink
fix: Handle NullPointerException and some wanring at google_mlkit_com…
Browse files Browse the repository at this point in the history
…mons (#651)

* fix: Handle NullPointerException and some wanring at google_mlkit_commons
* chore: bump the version to 0.8.0
  • Loading branch information
nickf2k authored Jul 6, 2024
1 parent 9b974cf commit 57c686c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions packages/google_mlkit_commons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.8.0

* Handle NullPointer Exception in `InputImageConverter.java`
* Fix warnings

## 0.7.2

* Widened image format support for Android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

public class GoogleMlKitCommonsPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
private MethodChannel channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import io.flutter.plugin.common.MethodChannel;

Expand All @@ -21,27 +22,39 @@ public static InputImage getInputImageFromData(Map<String, Object> imageData,
//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;
if (model.equals("file")) {
if (model != null && model.equals("file")) {
try {
inputImage = InputImage.fromFilePath(context, Uri.fromFile(new File(((String) imageData.get("path")))));
return inputImage;
} catch (IOException e) {
Log.e("ImageError", "Getting Image failed");
e.printStackTrace();
Log.e("ImageError", e.toString());
result.error("InputImageConverterError", e.toString(), null);
return null;
}
} else if (model.equals("bytes")) {
Map<String, Object> metaData = (Map<String, Object>) imageData.get("metadata");
inputImage = InputImage.fromByteArray((byte[]) imageData.get("bytes"),
(int) (double) metaData.get("width"),
(int) (double) metaData.get("height"),
(int) metaData.get("rotation"),
(int) metaData.get("image_format"));
return inputImage;
} else {
result.error("InputImageConverterError", "Invalid Input Image", null);
return null;
if (model != null && model.equals("bytes")) {
try {
@SuppressWarnings("unchecked")
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;
} catch (Exception e) {
Log.e("ImageError", "Getting Image failed");
Log.e("ImageError", e.toString());
result.error("InputImageConverterError", e.toString(), null);
return null;
}
} else {
result.error("InputImageConverterError", "Invalid Input Image", null);
return null;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/google_mlkit_commons/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: google_mlkit_commons
description: A Flutter plugin with commons files to implement google's standalone ml kit made for mobile platform.
version: 0.7.2
version: 0.8.0
homepage: https://github.com/flutter-ml/google_ml_kit_flutter

environment:
Expand Down

0 comments on commit 57c686c

Please sign in to comment.