@@ -54,71 +54,85 @@ private void handleDetection(MethodCall call, final MethodChannel.Result result)
5454 DigitalInkRecognitionModel model = getModel (tag , result );
5555 if (model == null )
5656 return ;
57- if (!genericModelManager .isModelDownloaded (model )) {
58- result .error ("Model Error" , "Model has not been downloaded yet " , null );
59- return ;
60- }
61-
62- String id = call .argument ("id" );
63- com .google .mlkit .vision .digitalink .DigitalInkRecognizer recognizer = instances .get (id );
64- if (recognizer == null ) {
65- recognizer = DigitalInkRecognition .getClient (DigitalInkRecognizerOptions .builder (model ).build ());
66- instances .put (id , recognizer );
67- }
6857
69- Map <String , Object > inkMap = call .argument ("ink" );
70- List <Map <String , Object >> strokeList = (List <Map <String , Object >>) inkMap .get ("strokes" );
71- Ink .Builder inkBuilder = Ink .builder ();
72- for (final Map <String , Object > strokeMap : strokeList ) {
73- Ink .Stroke .Builder strokeBuilder = Ink .Stroke .builder ();
74- List <Map <String , Object >> pointsList = (List <Map <String , Object >>) strokeMap .get ("points" );
75- for (final Map <String , Object > point : pointsList ) {
76- float x = (float ) (double ) point .get ("x" );
77- float y = (float ) (double ) point .get ("y" );
78- Object t0 = point .get ("t" );
79- long t ;
80- if (t0 instanceof Integer ) {
81- t = (int ) t0 ;
82- } else {
83- t = (long ) t0 ;
58+ genericModelManager .isModelDownloaded (
59+ model ,
60+ new GenericModelManager .CheckModelIsDownloadedCallback () {
61+ @ Override
62+ public void onModelDownloaded (Boolean isDownloaded ) {
63+ if (!isDownloaded ) {
64+ result .error ("Model Error" , "Model has not been downloaded yet " , null );
65+ return ;
66+ }
67+
68+ String id = call .argument ("id" );
69+ com .google .mlkit .vision .digitalink .DigitalInkRecognizer recognizer = instances .get (id );
70+ if (recognizer == null ) {
71+ recognizer = DigitalInkRecognition .getClient (DigitalInkRecognizerOptions .builder (model ).build ());
72+ instances .put (id , recognizer );
73+ }
74+
75+ Map <String , Object > inkMap = call .argument ("ink" );
76+ List <Map <String , Object >> strokeList = (List <Map <String , Object >>) inkMap .get ("strokes" );
77+ Ink .Builder inkBuilder = Ink .builder ();
78+ for (final Map <String , Object > strokeMap : strokeList ) {
79+ Ink .Stroke .Builder strokeBuilder = Ink .Stroke .builder ();
80+ List <Map <String , Object >> pointsList = (List <Map <String , Object >>) strokeMap .get ("points" );
81+ for (final Map <String , Object > point : pointsList ) {
82+ float x = (float ) (double ) point .get ("x" );
83+ float y = (float ) (double ) point .get ("y" );
84+ Object t0 = point .get ("t" );
85+ long t ;
86+ if (t0 instanceof Integer ) {
87+ t = (int ) t0 ;
88+ } else {
89+ t = (long ) t0 ;
90+ }
91+ Ink .Point strokePoint = Ink .Point .create (x , y , t );
92+ strokeBuilder .addPoint (strokePoint );
93+ }
94+ inkBuilder .addStroke (strokeBuilder .build ());
95+ }
96+ Ink ink = inkBuilder .build ();
97+
98+ RecognitionContext context = null ;
99+ Map <String , Object > contextMap = call .argument ("context" );
100+ if (contextMap != null ) {
101+ RecognitionContext .Builder builder = RecognitionContext .builder ();
102+ String preContext = (String ) contextMap .get ("preContext" );
103+ if (preContext != null ) {
104+ builder .setPreContext (preContext );
105+ } else {
106+ builder .setPreContext ("" );
107+ }
108+
109+ Map <String , Object > writingAreaMap = (Map <String , Object >) contextMap .get ("writingArea" );
110+ if (writingAreaMap != null ) {
111+ float width = (float ) (double ) writingAreaMap .get ("width" );
112+ float height = (float ) (double ) writingAreaMap .get ("height" );
113+ builder .setWritingArea (new WritingArea (width , height ));
114+ }
115+
116+ context = builder .build ();
117+ }
118+
119+ if (context != null ) {
120+ recognizer .recognize (ink , context )
121+ .addOnSuccessListener (recognitionResult -> process (recognitionResult , result ))
122+ .addOnFailureListener (e -> result .error ("recognition Error" , e .toString (), null ));
123+ } else {
124+ recognizer .recognize (ink )
125+ .addOnSuccessListener (recognitionResult -> process (recognitionResult , result ))
126+ .addOnFailureListener (e -> result .error ("recognition Error" , e .toString (), null ));
127+ }
128+ }
129+
130+ @ Override
131+ public void onError (Exception e ) {
132+ result .error ("error" , e .toString (), null );
133+ }
84134 }
85- Ink .Point strokePoint = Ink .Point .create (x , y , t );
86- strokeBuilder .addPoint (strokePoint );
87- }
88- inkBuilder .addStroke (strokeBuilder .build ());
89- }
90- Ink ink = inkBuilder .build ();
91-
92- RecognitionContext context = null ;
93- Map <String , Object > contextMap = call .argument ("context" );
94- if (contextMap != null ) {
95- RecognitionContext .Builder builder = RecognitionContext .builder ();
96- String preContext = (String ) contextMap .get ("preContext" );
97- if (preContext != null ) {
98- builder .setPreContext (preContext );
99- } else {
100- builder .setPreContext ("" );
101- }
102-
103- Map <String , Object > writingAreaMap = (Map <String , Object >) contextMap .get ("writingArea" );
104- if (writingAreaMap != null ) {
105- float width = (float ) (double ) writingAreaMap .get ("width" );
106- float height = (float ) (double ) writingAreaMap .get ("height" );
107- builder .setWritingArea (new WritingArea (width , height ));
108- }
109-
110- context = builder .build ();
111- }
112-
113- if (context != null ) {
114- recognizer .recognize (ink , context )
115- .addOnSuccessListener (recognitionResult -> process (recognitionResult , result ))
116- .addOnFailureListener (e -> result .error ("recognition Error" , e .toString (), null ));
117- } else {
118- recognizer .recognize (ink )
119- .addOnSuccessListener (recognitionResult -> process (recognitionResult , result ))
120- .addOnFailureListener (e -> result .error ("recognition Error" , e .toString (), null ));
121- }
135+ );
122136 }
123137
124138 private void process (RecognitionResult recognitionResult , final MethodChannel .Result result ) {
0 commit comments