@@ -2,7 +2,7 @@ import { Component, NgZone } from "@angular/core";
22import { ImageSource } from "tns-core-modules/image-source" ;
33
44import { BarcodeFormat , MLKitScanBarcodesResult } from "nativescript-plugin-firebase/mlkit/barcodescanning" ;
5- import { MLKitRecognizeTextResult } from "nativescript-plugin-firebase/mlkit/textrecognition" ;
5+ import { MLKitRecognizeTextLocalResult , MLKitRecognizeTextCloudResult } from "nativescript-plugin-firebase/mlkit/textrecognition" ;
66import { MLKitLandmarkRecognitionResult } from "nativescript-plugin-firebase/mlkit/landmarkrecognition" ;
77import { MLKitDetectFacesResult } from "nativescript-plugin-firebase/mlkit/facedetection" ;
88import { action } from "tns-core-modules/ui/dialogs" ;
@@ -24,24 +24,31 @@ export class MLKitComponent {
2424
2525 pickedImage : ImageSource ;
2626
27- // TODO once more ML plugin features support cloud, add those as (cloud) options to this list
2827 private mlkitFeatures : Array < string > = [
29- "Text recognition" ,
28+ "Text recognition (local)" ,
29+ "Text recognition (cloud)" ,
3030 "Barcode scanning" ,
3131 "Face detection" ,
3232 "Image labeling" ,
3333 "Landmark recognition (cloud)"
3434 ] ;
3535
36+ private mlkitLocalFeatures : Array < string > = [
37+ "Text recognition" ,
38+ "Barcode scanning" ,
39+ "Face detection" ,
40+ "Image labeling"
41+ ] ;
42+
3643 constructor ( private routerExtensions : RouterExtensions ,
3744 private zone : NgZone ) {
3845 }
3946
4047 fromCameraFeed ( ) : void {
4148 action (
42- "Test which ML Kit feature?" ,
49+ "Test which ML Kit feature? No cloud processing will be used. " ,
4350 "Cancel" ,
44- this . mlkitFeatures
51+ this . mlkitLocalFeatures
4552 ) . then ( ( pickedItem : string ) => {
4653 let to ;
4754 if ( pickedItem === "Text recognition" ) {
@@ -52,14 +59,8 @@ export class MLKitComponent {
5259 to = "/tabs/mlkit/facedetection" ;
5360 } else if ( pickedItem === "Image labeling" ) {
5461 to = "/tabs/mlkit/imagelabeling" ;
55- } else if ( pickedItem === "Landmark recognition (cloud)" ) {
56- alert ( {
57- title : `Not available` ,
58- message : `Landmark recognition is currently cloud-only, so that would be a bit too taxing on your dataplan.` ,
59- okButtonText : "Gotcha!"
60- } ) ;
61- return ;
6262 }
63+
6364 if ( to !== undefined ) {
6465 this . routerExtensions . navigate ( [ to ] ,
6566 {
@@ -135,15 +136,21 @@ export class MLKitComponent {
135136 } ) ;
136137 }
137138
138- selectMLKitFeature ( imageSource : ImageSource ) : void {
139+ reusePickedImage ( ) : void {
140+ this . selectMLKitFeature ( this . pickedImage ) ;
141+ }
142+
143+ private selectMLKitFeature ( imageSource : ImageSource ) : void {
139144 action (
140145 "Use which ML Kit feature?" ,
141146 "Cancel" ,
142147 this . mlkitFeatures
143148 ) . then ( ( pickedItem : string ) => {
144149 let pickedItemIndex = this . mlkitFeatures . indexOf ( pickedItem ) ;
145- if ( pickedItem === "Text recognition" ) {
146- this . recognizeText ( imageSource ) ;
150+ if ( pickedItem === "Text recognition (local)" ) {
151+ this . recognizeTextLocal ( imageSource ) ;
152+ } else if ( pickedItem === "Text recognition (cloud)" ) {
153+ this . recognizeTextCloud ( imageSource ) ;
147154 } else if ( pickedItem === "Barcode scanning" ) {
148155 this . scanBarcode ( imageSource ) ;
149156 } else if ( pickedItem === "Face detection" ) {
@@ -156,11 +163,11 @@ export class MLKitComponent {
156163 } ) ;
157164 }
158165
159- private recognizeText ( imageSource : ImageSource ) : void {
160- firebase . mlkit . textrecognition . recognizeText ( {
166+ private recognizeTextLocal ( imageSource : ImageSource ) : void {
167+ firebase . mlkit . textrecognition . recognizeTextLocal ( {
161168 image : imageSource
162169 } ) . then (
163- ( result : MLKitRecognizeTextResult ) => {
170+ ( result : MLKitRecognizeTextLocalResult ) => {
164171 alert ( {
165172 title : `Result` ,
166173 message : result . features . map ( feature => feature . text ) . join ( "" ) ,
@@ -170,6 +177,22 @@ export class MLKitComponent {
170177 . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
171178 }
172179
180+ private recognizeTextCloud ( imageSource : ImageSource ) : void {
181+ firebase . mlkit . textrecognition . recognizeTextCloud ( {
182+ image : imageSource ,
183+ modelType : "latest" ,
184+ maxResults : 15
185+ } ) . then (
186+ ( result : MLKitRecognizeTextCloudResult ) => {
187+ alert ( {
188+ title : `Result` ,
189+ message : result . text ,
190+ okButtonText : "OK"
191+ } ) ;
192+ } )
193+ . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
194+ }
195+
173196 private recognizeLandmark ( imageSource : ImageSource ) : void {
174197 firebase . mlkit . landmarkrecognition . recognizeLandmark ( {
175198 image : imageSource
0 commit comments