1818import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
1919import io .flutter .plugin .common .MethodChannel .Result ;
2020import io .flutter .plugin .common .PluginRegistry ;
21- import io .flutter .plugin .common .PluginRegistry .Registrar ;
2221
2322import com .microblink .blinkid .MicroblinkSDK ;
24- import com .microblink .blinkid .entities .recognizers .Recognizer ;
2523import com .microblink .blinkid .entities .recognizers .RecognizerBundle ;
2624import com .microblink .blinkid .intent .IntentDataTransferMode ;
2725import com .microblink .blinkid .uisettings .UISettings ;
2826import com .microblink .blinkid .uisettings .ActivityRunner ;
2927import com .microblink .blinkid .locale .LanguageUtils ;
30- import com .microblink .blinkid .directApi .DirectApiErrorListener ;
3128import com .microblink .blinkid .directApi .RecognizerRunner ;
32- import com .microblink .blinkid .entities .recognizers .RecognizerBundle ;
3329import com .microblink .blinkid .hardware .orientation .Orientation ;
3430import com .microblink .blinkid .metadata .MetadataCallbacks ;
3531import com .microblink .blinkid .metadata .recognition .FirstSideRecognitionCallback ;
4036import com .microblink .blinkid .flutter .recognizers .RecognizerSerializers ;
4137import com .microblink .blinkid .flutter .overlays .OverlaySettingsSerializers ;
4238
43- import org .json .JSONException ;
4439import org .json .JSONObject ;
4540import org .json .JSONArray ;
4641
@@ -71,14 +66,6 @@ public class BlinkIDFlutterPlugin implements FlutterPlugin, MethodCallHandler, P
7166
7267 private Result pendingResult ;
7368
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-
8269 @ Override
8370 public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
8471 setupPlugin (
@@ -88,43 +75,43 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
8875 }
8976
9077 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+ }
9481
95- this .channel = new MethodChannel (messenger , CHANNEL );
96- this .channel .setMethodCallHandler (this );
82+ this .channel = new MethodChannel (messenger , CHANNEL );
83+ this .channel .setMethodCallHandler (this );
9784 }
9885
9986 @ Override
10087 public void onMethodCall (@ NonNull MethodCall call , @ NonNull Result result ) {
10188
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 ();
113114 }
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- }
128115 }
129116
130117
@@ -164,7 +151,8 @@ private void setLanguage(JSONObject jsonOverlaySettings) {
164151 LanguageUtils .setLanguageAndCountry (jsonOverlaySettings .getString ("language" ),
165152 jsonOverlaySettings .getString ("country" ),
166153 context );
167- } catch (Exception e ) {}
154+ } catch (Exception e ) {
155+ }
168156 }
169157
170158 private void startScanning (int requestCode , UISettings uiSettings ) {
@@ -184,9 +172,10 @@ public void onScanningDone(@NonNull RecognitionSuccessType recognitionSuccessTyp
184172 mFirstSideScanned = false ;
185173 handleDirectApiResult (recognitionSuccessType );
186174 }
175+
187176 @ Override
188177 public void onUnrecoverableError (@ NonNull Throwable throwable ) {
189- handleDirectApiError (throwable .getMessage ());
178+ handleDirectApiError (throwable .getMessage ());
190179 }
191180 };
192181
@@ -209,17 +198,18 @@ public void onScanningDone(@NonNull RecognitionSuccessType recognitionSuccessTyp
209198 } else {
210199 handleDirectApiError ("Could not extract the information from the front side and back side is empty!" );
211200 }
212- } else if (!mFirstSideScanned && recognitionSuccessType != RecognitionSuccessType .UNSUCCESSFUL ){
201+ } else if (!mFirstSideScanned && recognitionSuccessType != RecognitionSuccessType .UNSUCCESSFUL ) {
213202 //singleside recognizer used
214203 handleDirectApiResult (recognitionSuccessType );
215204 } else {
216205 mFirstSideScanned = false ;
217206 handleDirectApiError ("Could not extract the information with DirectAPI!" );
218207 }
219208 }
209+
220210 @ Override
221211 public void onUnrecoverableError (@ NonNull Throwable throwable ) {
222- handleDirectApiError (throwable .getMessage ());
212+ handleDirectApiError (throwable .getMessage ());
223213 }
224214 };
225215
@@ -232,7 +222,7 @@ public void onUnrecoverableError(@NonNull Throwable throwable) {
232222 }
233223 }
234224
235- private void setupRecognizerRunner (JSONObject jsonRecognizerCollection , FirstSideRecognitionCallback mFirstSideRecognitionCallback ) {
225+ private void setupRecognizerRunner (JSONObject jsonRecognizerCollection , FirstSideRecognitionCallback mFirstSideRecognitionCallback ) {
236226 if (mRecognizerRunner != null ) {
237227 mRecognizerRunner .terminate ();
238228 }
@@ -247,45 +237,47 @@ private void setupRecognizerRunner(JSONObject jsonRecognizerCollection, FirstSid
247237 metadataCallbacks .setFirstSideRecognitionCallback (mFirstSideRecognitionCallback );
248238 mRecognizerRunner .setMetadataCallbacks (metadataCallbacks );
249239 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+ }
286277
287278 @ Override
288- public void onDetachedFromActivity () {}
279+ public void onDetachedFromActivity () {
280+ }
289281
290282 @ Override
291283 public void onReattachedToActivityForConfigChanges (ActivityPluginBinding binding ) {
@@ -299,15 +291,16 @@ public void onAttachedToActivity(ActivityPluginBinding binding) {
299291 }
300292
301293 @ Override
302- public void onDetachedFromActivityForConfigChanges () {}
294+ public void onDetachedFromActivityForConfigChanges () {
295+ }
303296
304297 @ Override
305298 public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
306- this .context = null ;
307- this .activity = null ;
299+ this .context = null ;
300+ this .activity = null ;
308301
309- this .channel .setMethodCallHandler (null );
310- this .channel = null ;
302+ this .channel .setMethodCallHandler (null );
303+ this .channel = null ;
311304 }
312305
313306
@@ -316,9 +309,9 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
316309 if (pendingResult == null ) {
317310 return true ;
318311 }
319-
312+
320313 if (resultCode == Activity .RESULT_OK ) {
321- if (requestCode == SCAN_REQ_CODE && mRecognizerBundle != null ) {
314+ if (requestCode == SCAN_REQ_CODE && mRecognizerBundle != null ) {
322315 mRecognizerBundle .loadFromIntent (data );
323316 JSONArray resultList = RecognizerSerializers .INSTANCE .serializeRecognizerResults (mRecognizerBundle .getRecognizers ());
324317
0 commit comments