4
4
import android .graphics .BitmapFactory ;
5
5
import android .graphics .Matrix ;
6
6
import android .net .Uri ;
7
+ import android .util .SparseArray ;
7
8
import androidx .exifinterface .media .ExifInterface ;
9
+ import com .google .firebase .ml .vision .FirebaseVision ;
8
10
import com .google .firebase .ml .vision .common .FirebaseVisionImage ;
9
11
import com .google .firebase .ml .vision .common .FirebaseVisionImageMetadata ;
10
12
import io .flutter .plugin .common .MethodCall ;
18
20
19
21
/** FirebaseMlVisionPlugin */
20
22
public class FirebaseMlVisionPlugin implements MethodCallHandler {
23
+ private final SparseArray <Detector > detectors = new SparseArray <>();
24
+
21
25
private Registrar registrar ;
22
26
23
27
private FirebaseMlVisionPlugin (Registrar registrar ) {
@@ -33,47 +37,97 @@ public static void registerWith(Registrar registrar) {
33
37
34
38
@ Override
35
39
public void onMethodCall (MethodCall call , Result result ) {
36
- Map <String , Object > options = call .argument ("options" );
37
40
String modelName = call .argument ("model" );
38
- FirebaseVisionImage image ;
39
- Map <String , Object > imageData = call .arguments ();
40
- try {
41
- image = dataToVisionImage (imageData );
42
- } catch (IOException exception ) {
43
- result .error ("MLVisionDetectorIOError" , exception .getLocalizedMessage (), null );
44
- return ;
45
- }
46
-
47
41
switch (call .method ) {
48
- case "BarcodeDetector#detectInImage " :
49
- BarcodeDetector .instance .handleDetection ( image , options , result );
42
+ case "ModelManager#setupLocalModel " :
43
+ SetupLocalModel .instance .setup ( modelName , result );
50
44
break ;
51
- case "FaceDetector#processImage " :
52
- FaceDetector .instance .handleDetection ( image , options , result );
45
+ case "ModelManager#setupRemoteModel " :
46
+ SetupRemoteModel .instance .setup ( modelName , result );
53
47
break ;
48
+ case "BarcodeDetector#detectInImage" :
49
+ case "FaceDetector#processImage" :
54
50
case "ImageLabeler#processImage" :
55
- ImageLabeler .instance .handleDetection (image , options , result );
56
- break ;
57
51
case "TextRecognizer#processImage" :
58
- TextRecognizer .instance .handleDetection (image , options , result );
59
- break ;
60
- case "VisionEdgeImageLabeler#processLocalImage" :
61
- LocalVisionEdgeDetector .instance .handleDetection (image , options , result );
62
- break ;
52
+ case "VisionEdgeImageLabeler#processLocalImage" :
63
53
case "VisionEdgeImageLabeler#processRemoteImage" :
64
- RemoteVisionEdgeDetector . instance . handleDetection (image , options , result );
54
+ handleDetection (call , result );
65
55
break ;
66
- case "ModelManager#setupLocalModel" :
67
- SetupLocalModel .instance .setup (modelName , result );
68
- break ;
69
- case "ModelManager#setupRemoteModel" :
70
- SetupRemoteModel .instance .setup (modelName , result );
56
+ case "BarcodeDetector#close" :
57
+ case "FaceDetector#close" :
58
+ case "ImageLabeler#close" :
59
+ case "TextRecognizer#close" :
60
+ case "VisionEdgeImageLabeler#close" :
61
+ closeDetector (call , result );
71
62
break ;
72
63
default :
73
64
result .notImplemented ();
74
65
}
75
66
}
76
67
68
+ private void handleDetection (MethodCall call , Result result ) {
69
+ Map <String , Object > options = call .argument ("options" );
70
+
71
+ FirebaseVisionImage image ;
72
+ Map <String , Object > imageData = call .arguments ();
73
+ try {
74
+ image = dataToVisionImage (imageData );
75
+ } catch (IOException exception ) {
76
+ result .error ("MLVisionDetectorIOError" , exception .getLocalizedMessage (), null );
77
+ return ;
78
+ }
79
+
80
+ Detector detector = getDetector (call );
81
+ if (detector == null ) {
82
+ switch (call .method ) {
83
+ case "BarcodeDetector#detectInImage" :
84
+ detector = new BarcodeDetector (FirebaseVision .getInstance (), options );
85
+ break ;
86
+ case "FaceDetector#processImage" :
87
+ detector = new FaceDetector (FirebaseVision .getInstance (), options );
88
+ break ;
89
+ case "ImageLabeler#processImage" :
90
+ detector = new ImageLabeler (FirebaseVision .getInstance (), options );
91
+ break ;
92
+ case "TextRecognizer#processImage" :
93
+ detector = new TextRecognizer (FirebaseVision .getInstance (), options );
94
+ break ;
95
+ case "VisionEdgeImageLabeler#processLocalImage" :
96
+ detector = new LocalVisionEdgeDetector (FirebaseVision .getInstance (), options );
97
+ break ;
98
+ case "VisionEdgeImageLabeler#processRemoteImage" :
99
+ detector = new RemoteVisionEdgeDetector (FirebaseVision .getInstance (), options );
100
+ break ;
101
+ }
102
+
103
+ final Integer handle = call .argument ("handle" );
104
+ addDetector (handle , detector );
105
+ }
106
+
107
+ detector .handleDetection (image , result );
108
+ }
109
+
110
+ private void closeDetector (final MethodCall call , final Result result ) {
111
+ final Detector detector = getDetector (call );
112
+
113
+ if (detector == null ) {
114
+ final Integer handle = call .argument ("handle" );
115
+ final String message = String .format ("Object for handle does not exists: %s" , handle );
116
+ throw new IllegalArgumentException (message );
117
+ }
118
+
119
+ try {
120
+ detector .close ();
121
+ result .success (null );
122
+ } catch (IOException e ) {
123
+ final String code = String .format ("%sIOError" , detector .getClass ().getSimpleName ());
124
+ result .error (code , e .getLocalizedMessage (), null );
125
+ } finally {
126
+ final Integer handle = call .argument ("handle" );
127
+ detectors .remove (handle );
128
+ }
129
+ }
130
+
77
131
private FirebaseVisionImage dataToVisionImage (Map <String , Object > imageData ) throws IOException {
78
132
String imageType = (String ) imageData .get ("type" );
79
133
assert imageType != null ;
@@ -148,4 +202,18 @@ private int getRotation(int rotation) {
148
202
throw new IllegalArgumentException (String .format ("No rotation for: %d" , rotation ));
149
203
}
150
204
}
205
+
206
+ private void addDetector (final int handle , final Detector detector ) {
207
+ if (detectors .get (handle ) != null ) {
208
+ final String message = String .format ("Object for handle already exists: %s" , handle );
209
+ throw new IllegalArgumentException (message );
210
+ }
211
+
212
+ detectors .put (handle , detector );
213
+ }
214
+
215
+ private Detector getDetector (final MethodCall call ) {
216
+ final Integer handle = call .argument ("handle" );
217
+ return detectors .get (handle );
218
+ }
151
219
}
0 commit comments