18
18
import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
19
19
import io .flutter .plugin .common .MethodChannel .Result ;
20
20
import io .flutter .plugin .common .PluginRegistry ;
21
- import io .flutter .plugin .common .PluginRegistry .Registrar ;
22
21
23
22
import com .microblink .blinkid .MicroblinkSDK ;
24
- import com .microblink .blinkid .entities .recognizers .Recognizer ;
25
23
import com .microblink .blinkid .entities .recognizers .RecognizerBundle ;
26
24
import com .microblink .blinkid .intent .IntentDataTransferMode ;
27
25
import com .microblink .blinkid .uisettings .UISettings ;
28
26
import com .microblink .blinkid .uisettings .ActivityRunner ;
29
27
import com .microblink .blinkid .locale .LanguageUtils ;
30
- import com .microblink .blinkid .directApi .DirectApiErrorListener ;
31
28
import com .microblink .blinkid .directApi .RecognizerRunner ;
32
- import com .microblink .blinkid .entities .recognizers .RecognizerBundle ;
33
29
import com .microblink .blinkid .hardware .orientation .Orientation ;
34
30
import com .microblink .blinkid .metadata .MetadataCallbacks ;
35
31
import com .microblink .blinkid .metadata .recognition .FirstSideRecognitionCallback ;
40
36
import com .microblink .blinkid .flutter .recognizers .RecognizerSerializers ;
41
37
import com .microblink .blinkid .flutter .overlays .OverlaySettingsSerializers ;
42
38
43
- import org .json .JSONException ;
44
39
import org .json .JSONObject ;
45
40
import org .json .JSONArray ;
46
41
@@ -71,14 +66,6 @@ public class BlinkIDFlutterPlugin implements FlutterPlugin, MethodCallHandler, P
71
66
72
67
private Result pendingResult ;
73
68
74
- // This static function is optional and equivalent to onAttachedToEngine. It supports the old
75
- // pre-Flutter-1.12 Android projects.
76
- public static void registerWith (Registrar registrar ) {
77
- final BlinkIDFlutterPlugin plugin = new BlinkIDFlutterPlugin ();
78
- plugin .setupPlugin (registrar .activity (), registrar .messenger ());
79
- registrar .addActivityResultListener (plugin );
80
- }
81
-
82
69
@ Override
83
70
public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
84
71
setupPlugin (
@@ -88,43 +75,43 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
88
75
}
89
76
90
77
private void setupPlugin (Context context , BinaryMessenger messenger ) {
91
- if (context != null ) {
92
- this .context = context ;
93
- }
78
+ if (context != null ) {
79
+ this .context = context ;
80
+ }
94
81
95
- this .channel = new MethodChannel (messenger , CHANNEL );
96
- this .channel .setMethodCallHandler (this );
82
+ this .channel = new MethodChannel (messenger , CHANNEL );
83
+ this .channel .setMethodCallHandler (this );
97
84
}
98
85
99
86
@ Override
100
87
public void onMethodCall (@ NonNull MethodCall call , @ NonNull Result result ) {
101
88
102
- if (call .method .equals (METHOD_SCAN_CAMERA )) {
103
- pendingResult = result ;
104
- boolean isLicenseKeyValid = setLicense ((Map )call .argument (ARG_LICENSE ));
105
-
106
- JSONObject jsonOverlaySettings = new JSONObject ((Map )call .argument (ARG_OVERLAY_SETTINGS ));
107
- JSONObject jsonRecognizerCollection = new JSONObject ((Map )call .argument (ARG_RECOGNIZER_COLLECTION ));
108
- if (isLicenseKeyValid ) {
109
- setLanguage (jsonOverlaySettings );
110
- mRecognizerBundle = RecognizerSerializers .INSTANCE .deserializeRecognizerCollection (jsonRecognizerCollection );
111
- UISettings uiSettings = OverlaySettingsSerializers .INSTANCE .getOverlaySettings (context , jsonOverlaySettings , mRecognizerBundle );
112
- startScanning (SCAN_REQ_CODE , uiSettings );
89
+ if (call .method .equals (METHOD_SCAN_CAMERA )) {
90
+ pendingResult = result ;
91
+ boolean isLicenseKeyValid = setLicense ((Map ) call .argument (ARG_LICENSE ));
92
+
93
+ JSONObject jsonOverlaySettings = new JSONObject ((Map ) call .argument (ARG_OVERLAY_SETTINGS ));
94
+ JSONObject jsonRecognizerCollection = new JSONObject ((Map ) call .argument (ARG_RECOGNIZER_COLLECTION ));
95
+ if (isLicenseKeyValid ) {
96
+ setLanguage (jsonOverlaySettings );
97
+ mRecognizerBundle = RecognizerSerializers .INSTANCE .deserializeRecognizerCollection (jsonRecognizerCollection );
98
+ UISettings uiSettings = OverlaySettingsSerializers .INSTANCE .getOverlaySettings (context , jsonOverlaySettings , mRecognizerBundle );
99
+ startScanning (SCAN_REQ_CODE , uiSettings );
100
+ }
101
+ } else if (call .method .equals (METHOD_SCAN_DIRECT_API )) {
102
+ pendingResult = result ;
103
+ boolean isLicenseKeyValid = setLicense ((Map ) call .argument (ARG_LICENSE ));
104
+
105
+ JSONObject jsonRecognizerCollection = new JSONObject ((Map ) call .argument (ARG_RECOGNIZER_COLLECTION ));
106
+ String frontImage = call .argument (ARG_FRONT_IMAGE );
107
+ String backImage = call .argument (ARG_BACK_IMAGE );
108
+ if (isLicenseKeyValid ) {
109
+ scanWithDirectApi (jsonRecognizerCollection , frontImage , backImage );
110
+ }
111
+
112
+ } else {
113
+ result .notImplemented ();
113
114
}
114
- } else if (call .method .equals (METHOD_SCAN_DIRECT_API )) {
115
- pendingResult = result ;
116
- boolean isLicenseKeyValid = setLicense ((Map )call .argument (ARG_LICENSE ));
117
-
118
- JSONObject jsonRecognizerCollection = new JSONObject ((Map )call .argument (ARG_RECOGNIZER_COLLECTION ));
119
- String frontImage = call .argument (ARG_FRONT_IMAGE );
120
- String backImage = call .argument (ARG_BACK_IMAGE );
121
- if (isLicenseKeyValid ) {
122
- scanWithDirectApi (jsonRecognizerCollection , frontImage , backImage );
123
- }
124
-
125
- } else {
126
- result .notImplemented ();
127
- }
128
115
}
129
116
130
117
@@ -164,7 +151,8 @@ private void setLanguage(JSONObject jsonOverlaySettings) {
164
151
LanguageUtils .setLanguageAndCountry (jsonOverlaySettings .getString ("language" ),
165
152
jsonOverlaySettings .getString ("country" ),
166
153
context );
167
- } catch (Exception e ) {}
154
+ } catch (Exception e ) {
155
+ }
168
156
}
169
157
170
158
private void startScanning (int requestCode , UISettings uiSettings ) {
@@ -184,9 +172,10 @@ public void onScanningDone(@NonNull RecognitionSuccessType recognitionSuccessTyp
184
172
mFirstSideScanned = false ;
185
173
handleDirectApiResult (recognitionSuccessType );
186
174
}
175
+
187
176
@ Override
188
177
public void onUnrecoverableError (@ NonNull Throwable throwable ) {
189
- handleDirectApiError (throwable .getMessage ());
178
+ handleDirectApiError (throwable .getMessage ());
190
179
}
191
180
};
192
181
@@ -209,17 +198,18 @@ public void onScanningDone(@NonNull RecognitionSuccessType recognitionSuccessTyp
209
198
} else {
210
199
handleDirectApiError ("Could not extract the information from the front side and back side is empty!" );
211
200
}
212
- } else if (!mFirstSideScanned && recognitionSuccessType != RecognitionSuccessType .UNSUCCESSFUL ){
201
+ } else if (!mFirstSideScanned && recognitionSuccessType != RecognitionSuccessType .UNSUCCESSFUL ) {
213
202
//singleside recognizer used
214
203
handleDirectApiResult (recognitionSuccessType );
215
204
} else {
216
205
mFirstSideScanned = false ;
217
206
handleDirectApiError ("Could not extract the information with DirectAPI!" );
218
207
}
219
208
}
209
+
220
210
@ Override
221
211
public void onUnrecoverableError (@ NonNull Throwable throwable ) {
222
- handleDirectApiError (throwable .getMessage ());
212
+ handleDirectApiError (throwable .getMessage ());
223
213
}
224
214
};
225
215
@@ -232,7 +222,7 @@ public void onUnrecoverableError(@NonNull Throwable throwable) {
232
222
}
233
223
}
234
224
235
- private void setupRecognizerRunner (JSONObject jsonRecognizerCollection , FirstSideRecognitionCallback mFirstSideRecognitionCallback ) {
225
+ private void setupRecognizerRunner (JSONObject jsonRecognizerCollection , FirstSideRecognitionCallback mFirstSideRecognitionCallback ) {
236
226
if (mRecognizerRunner != null ) {
237
227
mRecognizerRunner .terminate ();
238
228
}
@@ -247,45 +237,47 @@ private void setupRecognizerRunner(JSONObject jsonRecognizerCollection, FirstSid
247
237
metadataCallbacks .setFirstSideRecognitionCallback (mFirstSideRecognitionCallback );
248
238
mRecognizerRunner .setMetadataCallbacks (metadataCallbacks );
249
239
mRecognizerRunner .initialize (context , mRecognizerBundle , throwable -> handleDirectApiError ("Failed to initialize recognizer with DirectAPI: " + throwable .getMessage ()));
250
- }
251
-
252
- private void processImage (String base64Image , ScanResultListener scanResultListener ) {
253
- Bitmap image = base64ToBitmap (base64Image );
254
- if (image != null ) {
255
- mRecognizerRunner .recognizeBitmap (
256
- base64ToBitmap (base64Image ),
257
- Orientation .ORIENTATION_LANDSCAPE_RIGHT ,
258
- scanResultListener
259
- );
260
- } else {
261
- handleDirectApiError ("Could not decode the Base64 image!" );
262
- }
263
- }
264
-
265
- private void handleDirectApiResult (RecognitionSuccessType recognitionSuccessType ) {
266
- if (recognitionSuccessType != RecognitionSuccessType .UNSUCCESSFUL ) {
267
- if (pendingResult == null ) {
268
- return ;
269
- }
270
- JSONArray resultList = RecognizerSerializers .INSTANCE .serializeRecognizerResults (mRecognizerBundle .getRecognizers ());
271
- pendingResult .success (resultList .toString ());
272
- }
273
- pendingResult = null ;
274
- }
275
- private Bitmap base64ToBitmap (String base64String ) {
276
- byte [] decodedBytes = android .util .Base64 .decode (base64String , Base64 .DEFAULT );
277
- return BitmapFactory .decodeByteArray (decodedBytes , 0 , decodedBytes .length );
278
- }
279
-
280
- private void handleDirectApiError (String errorMessage ) {
281
- pendingResult .error ("" , errorMessage , null );
282
- if (mRecognizerRunner != null ) {
283
- mRecognizerRunner .resetRecognitionState (true );
284
- }
285
- }
240
+ }
241
+
242
+ private void processImage (String base64Image , ScanResultListener scanResultListener ) {
243
+ Bitmap image = base64ToBitmap (base64Image );
244
+ if (image != null ) {
245
+ mRecognizerRunner .recognizeBitmap (
246
+ base64ToBitmap (base64Image ),
247
+ Orientation .ORIENTATION_LANDSCAPE_RIGHT ,
248
+ scanResultListener
249
+ );
250
+ } else {
251
+ handleDirectApiError ("Could not decode the Base64 image!" );
252
+ }
253
+ }
254
+
255
+ private void handleDirectApiResult (RecognitionSuccessType recognitionSuccessType ) {
256
+ if (recognitionSuccessType != RecognitionSuccessType .UNSUCCESSFUL ) {
257
+ if (pendingResult == null ) {
258
+ return ;
259
+ }
260
+ JSONArray resultList = RecognizerSerializers .INSTANCE .serializeRecognizerResults (mRecognizerBundle .getRecognizers ());
261
+ pendingResult .success (resultList .toString ());
262
+ }
263
+ pendingResult = null ;
264
+ }
265
+
266
+ private Bitmap base64ToBitmap (String base64String ) {
267
+ byte [] decodedBytes = android .util .Base64 .decode (base64String , Base64 .DEFAULT );
268
+ return BitmapFactory .decodeByteArray (decodedBytes , 0 , decodedBytes .length );
269
+ }
270
+
271
+ private void handleDirectApiError (String errorMessage ) {
272
+ pendingResult .error ("" , errorMessage , null );
273
+ if (mRecognizerRunner != null ) {
274
+ mRecognizerRunner .resetRecognitionState (true );
275
+ }
276
+ }
286
277
287
278
@ Override
288
- public void onDetachedFromActivity () {}
279
+ public void onDetachedFromActivity () {
280
+ }
289
281
290
282
@ Override
291
283
public void onReattachedToActivityForConfigChanges (ActivityPluginBinding binding ) {
@@ -299,15 +291,16 @@ public void onAttachedToActivity(ActivityPluginBinding binding) {
299
291
}
300
292
301
293
@ Override
302
- public void onDetachedFromActivityForConfigChanges () {}
294
+ public void onDetachedFromActivityForConfigChanges () {
295
+ }
303
296
304
297
@ Override
305
298
public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
306
- this .context = null ;
307
- this .activity = null ;
299
+ this .context = null ;
300
+ this .activity = null ;
308
301
309
- this .channel .setMethodCallHandler (null );
310
- this .channel = null ;
302
+ this .channel .setMethodCallHandler (null );
303
+ this .channel = null ;
311
304
}
312
305
313
306
@@ -316,9 +309,9 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
316
309
if (pendingResult == null ) {
317
310
return true ;
318
311
}
319
-
312
+
320
313
if (resultCode == Activity .RESULT_OK ) {
321
- if (requestCode == SCAN_REQ_CODE && mRecognizerBundle != null ) {
314
+ if (requestCode == SCAN_REQ_CODE && mRecognizerBundle != null ) {
322
315
mRecognizerBundle .loadFromIntent (data );
323
316
JSONArray resultList = RecognizerSerializers .INSTANCE .serializeRecognizerResults (mRecognizerBundle .getRecognizers ());
324
317
0 commit comments