|
2 | 2 |
|
3 | 3 | import android.net.Uri; |
4 | 4 | import com.google.firebase.ml.vision.common.FirebaseVisionImage; |
| 5 | +import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata; |
5 | 6 | import io.flutter.plugin.common.MethodCall; |
6 | 7 | import io.flutter.plugin.common.MethodChannel; |
7 | 8 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
@@ -29,55 +30,74 @@ public static void registerWith(Registrar registrar) { |
29 | 30 | @Override |
30 | 31 | public void onMethodCall(MethodCall call, Result result) { |
31 | 32 | Map<String, Object> options = call.argument("options"); |
| 33 | + |
32 | 34 | FirebaseVisionImage image; |
| 35 | + Map<String, Object> imageData = call.arguments(); |
| 36 | + try { |
| 37 | + image = dataToVisionImage(imageData); |
| 38 | + } catch (IOException exception) { |
| 39 | + result.error("MLVisionDetectorIOError", exception.getLocalizedMessage(), null); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
33 | 43 | switch (call.method) { |
34 | 44 | case "BarcodeDetector#detectInImage": |
35 | | - try { |
36 | | - image = filePathToVisionImage((String) call.argument("path")); |
37 | | - BarcodeDetector.instance.handleDetection(image, options, result); |
38 | | - } catch (IOException e) { |
39 | | - result.error("barcodeDetectorIOError", e.getLocalizedMessage(), null); |
40 | | - } |
| 45 | + BarcodeDetector.instance.handleDetection(image, options, result); |
41 | 46 | break; |
42 | 47 | case "FaceDetector#detectInImage": |
43 | | - try { |
44 | | - image = filePathToVisionImage((String) call.argument("path")); |
45 | | - FaceDetector.instance.handleDetection(image, options, result); |
46 | | - } catch (IOException e) { |
47 | | - result.error("faceDetectorIOError", e.getLocalizedMessage(), null); |
48 | | - } |
| 48 | + FaceDetector.instance.handleDetection(image, options, result); |
49 | 49 | break; |
50 | 50 | case "LabelDetector#detectInImage": |
51 | | - try { |
52 | | - image = filePathToVisionImage((String) call.argument("path")); |
53 | | - LabelDetector.instance.handleDetection(image, options, result); |
54 | | - } catch (IOException e) { |
55 | | - result.error("labelDetectorIOError", e.getLocalizedMessage(), null); |
56 | | - } |
| 51 | + LabelDetector.instance.handleDetection(image, options, result); |
57 | 52 | break; |
58 | 53 | case "CloudLabelDetector#detectInImage": |
59 | | - try { |
60 | | - image = filePathToVisionImage((String) call.argument("path")); |
61 | | - CloudLabelDetector.instance.handleDetection(image, options, result); |
62 | | - } catch (IOException e) { |
63 | | - result.error("cloudLabelDetectorIOError", e.getLocalizedMessage(), null); |
64 | | - } |
| 54 | + CloudLabelDetector.instance.handleDetection(image, options, result); |
65 | 55 | break; |
66 | 56 | case "TextRecognizer#processImage": |
67 | | - try { |
68 | | - image = filePathToVisionImage((String) call.argument("path")); |
69 | | - TextRecognizer.instance.handleDetection(image, options, result); |
70 | | - } catch (IOException e) { |
71 | | - result.error("textRecognizerIOError", e.getLocalizedMessage(), null); |
72 | | - } |
| 57 | + TextRecognizer.instance.handleDetection(image, options, result); |
73 | 58 | break; |
74 | 59 | default: |
75 | 60 | result.notImplemented(); |
76 | 61 | } |
77 | 62 | } |
78 | 63 |
|
79 | | - private FirebaseVisionImage filePathToVisionImage(String path) throws IOException { |
80 | | - File file = new File(path); |
81 | | - return FirebaseVisionImage.fromFilePath(registrar.context(), Uri.fromFile(file)); |
| 64 | + private FirebaseVisionImage dataToVisionImage(Map<String, Object> imageData) throws IOException { |
| 65 | + String imageType = (String) imageData.get("type"); |
| 66 | + |
| 67 | + switch (imageType) { |
| 68 | + case "file": |
| 69 | + File file = new File((String) imageData.get("path")); |
| 70 | + return FirebaseVisionImage.fromFilePath(registrar.context(), Uri.fromFile(file)); |
| 71 | + case "bytes": |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + Map<String, Object> metadataData = (Map<String, Object>) imageData.get("metadata"); |
| 74 | + |
| 75 | + FirebaseVisionImageMetadata metadata = |
| 76 | + new FirebaseVisionImageMetadata.Builder() |
| 77 | + .setWidth((int) (double) metadataData.get("width")) |
| 78 | + .setHeight((int) (double) metadataData.get("height")) |
| 79 | + .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) |
| 80 | + .setRotation(getRotation((int) metadataData.get("rotation"))) |
| 81 | + .build(); |
| 82 | + |
| 83 | + return FirebaseVisionImage.fromByteArray((byte[]) imageData.get("bytes"), metadata); |
| 84 | + default: |
| 85 | + throw new IllegalArgumentException(String.format("No image type for: %s", imageType)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private int getRotation(int rotation) { |
| 90 | + switch (rotation) { |
| 91 | + case 0: |
| 92 | + return FirebaseVisionImageMetadata.ROTATION_0; |
| 93 | + case 90: |
| 94 | + return FirebaseVisionImageMetadata.ROTATION_90; |
| 95 | + case 180: |
| 96 | + return FirebaseVisionImageMetadata.ROTATION_180; |
| 97 | + case 270: |
| 98 | + return FirebaseVisionImageMetadata.ROTATION_270; |
| 99 | + default: |
| 100 | + throw new IllegalArgumentException(String.format("No rotation for: %d", rotation)); |
| 101 | + } |
82 | 102 | } |
83 | 103 | } |
0 commit comments