11import { Component , NgZone } from "@angular/core" ;
2+ import { RouterExtensions } from "nativescript-angular" ;
23import { ImageSource } from "tns-core-modules/image-source" ;
3-
4- import { BarcodeFormat , MLKitScanBarcodesResult } from "nativescript-plugin-firebase/mlkit/barcodescanning" ;
5- import { MLKitRecognizeTextLocalResult , MLKitRecognizeTextCloudResult } from "nativescript-plugin-firebase/mlkit/textrecognition" ;
6- import { MLKitLandmarkRecognitionCloudResult } from "nativescript-plugin-firebase/mlkit/landmarkrecognition" ;
7- import { MLKitDetectFacesResult } from "nativescript-plugin-firebase/mlkit/facedetection" ;
84import { action } from "tns-core-modules/ui/dialogs" ;
95import { ImageAsset } from "tns-core-modules/image-asset" ;
6+ import { isIOS } from "tns-core-modules/platform" ;
107import * as ImagePicker from "nativescript-imagepicker" ;
118import * as Camera from "nativescript-camera" ;
12- import { RouterExtensions } from "nativescript-angular" ;
13- import { isIOS } from "tns-core-modules/platform" ;
14- import { MLKitImageLabelingResult } from "nativescript-plugin-firebase/mlkit/imagelabeling" ;
9+ import { BarcodeFormat , MLKitScanBarcodesOnDeviceResult } from "nativescript-plugin-firebase/mlkit/barcodescanning" ;
10+ import { MLKitLandmarkRecognitionCloudResult } from "nativescript-plugin-firebase/mlkit/landmarkrecognition" ;
11+ import { MLKitDetectFacesOnDeviceResult } from "nativescript-plugin-firebase/mlkit/facedetection" ;
12+ import {
13+ MLKitRecognizeTextCloudResult ,
14+ MLKitRecognizeTextOnDeviceResult
15+ } from "nativescript-plugin-firebase/mlkit/textrecognition" ;
16+ import {
17+ MLKitImageLabelingCloudResult ,
18+ MLKitImageLabelingOnDeviceResult
19+ } from "nativescript-plugin-firebase/mlkit/imagelabeling" ;
1520
1621const firebase = require ( "nativescript-plugin-firebase" ) ;
1722
@@ -25,15 +30,16 @@ export class MLKitComponent {
2530 pickedImage : ImageSource ;
2631
2732 private mlkitFeatures : Array < string > = [
28- "Text recognition (local )" ,
33+ "Text recognition (on device )" ,
2934 "Text recognition (cloud)" ,
30- "Barcode scanning" ,
31- "Face detection" ,
32- "Image labeling" ,
35+ "Barcode scanning (on device)" ,
36+ "Face detection (on device)" ,
37+ "Image labeling (on device)" ,
38+ "Image labeling (cloud)" ,
3339 "Landmark recognition (cloud)"
3440 ] ;
3541
36- private mlkitLocalFeatures : Array < string > = [
42+ private mlkitOnDeviceFeatures : Array < string > = [
3743 "Text recognition" ,
3844 "Barcode scanning" ,
3945 "Face detection" ,
@@ -46,9 +52,9 @@ export class MLKitComponent {
4652
4753 fromCameraFeed ( ) : void {
4854 action (
49- "Test which ML Kit feature? No cloud processing will be used. " ,
55+ "Test which on-device ML Kit feature?" ,
5056 "Cancel" ,
51- this . mlkitLocalFeatures
57+ this . mlkitOnDeviceFeatures
5258 ) . then ( ( pickedItem : string ) => {
5359 let to ;
5460 if ( pickedItem === "Text recognition" ) {
@@ -83,7 +89,7 @@ export class MLKitComponent {
8389 width : 800 ,
8490 height : 800 ,
8591 keepAspectRatio : true ,
86- saveToGallery : true ,
92+ saveToGallery : false ,
8793 cameraFacing : "rear"
8894 } ) . then ( imageAsset => {
8995 new ImageSource ( ) . fromAsset ( imageAsset ) . then ( imageSource => {
@@ -147,34 +153,34 @@ export class MLKitComponent {
147153 this . mlkitFeatures
148154 ) . then ( ( pickedItem : string ) => {
149155 let pickedItemIndex = this . mlkitFeatures . indexOf ( pickedItem ) ;
150- if ( pickedItem === "Text recognition (local )" ) {
151- this . recognizeTextLocal ( imageSource ) ;
156+ if ( pickedItem === "Text recognition (on device )" ) {
157+ this . recognizeTextOnDevice ( imageSource ) ;
152158 } else if ( pickedItem === "Text recognition (cloud)" ) {
153159 this . recognizeTextCloud ( imageSource ) ;
154- } else if ( pickedItem === "Barcode scanning" ) {
155- this . scanBarcode ( imageSource ) ;
156- } else if ( pickedItem === "Face detection" ) {
157- this . detectFaces ( imageSource ) ;
158- } else if ( pickedItem === "Image labeling" ) {
159- this . labelImage ( imageSource ) ;
160+ } else if ( pickedItem === "Barcode scanning (on device)" ) {
161+ this . scanBarcodeOnDevice ( imageSource ) ;
162+ } else if ( pickedItem === "Face detection (on device)" ) {
163+ this . detectFacesOnDevice ( imageSource ) ;
164+ } else if ( pickedItem === "Image labeling (on device)" ) {
165+ this . labelImageOnDevice ( imageSource ) ;
166+ } else if ( pickedItem === "Image labeling (cloud)" ) {
167+ this . labelImageCloud ( imageSource ) ;
160168 } else if ( pickedItem === "Landmark recognition (cloud)" ) {
161169 this . recognizeLandmarkCloud ( imageSource ) ;
162170 }
163171 } ) ;
164172 }
165173
166- private recognizeTextLocal ( imageSource : ImageSource ) : void {
167- firebase . mlkit . textrecognition . recognizeTextLocal ( {
174+ private recognizeTextOnDevice ( imageSource : ImageSource ) : void {
175+ firebase . mlkit . textrecognition . recognizeTextOnDevice ( {
168176 image : imageSource
169- } ) . then (
170- ( result : MLKitRecognizeTextLocalResult ) => {
171- alert ( {
172- title : `Result` ,
173- message : result . features . map ( feature => feature . text ) . join ( "" ) ,
174- okButtonText : "OK"
175- } ) ;
176- } )
177- . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
177+ } ) . then ( ( result : MLKitRecognizeTextOnDeviceResult ) => {
178+ alert ( {
179+ title : `Result` ,
180+ message : result . features . map ( feature => feature . text ) . join ( "" ) ,
181+ okButtonText : "OK"
182+ } ) ;
183+ } ) . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
178184 }
179185
180186 private recognizeTextCloud ( imageSource : ImageSource ) : void {
@@ -207,12 +213,13 @@ export class MLKitComponent {
207213 . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
208214 }
209215
210- private scanBarcode ( imageSource : ImageSource ) : void {
211- firebase . mlkit . barcodescanning . scanBarcodes ( {
216+ private scanBarcodeOnDevice ( imageSource : ImageSource ) : void {
217+ console . log ( ">>> imageSource.rotationAngle: " + imageSource . rotationAngle ) ;
218+ firebase . mlkit . barcodescanning . scanBarcodesOnDevice ( {
212219 image : imageSource ,
213220 formats : [ BarcodeFormat . QR_CODE , BarcodeFormat . EAN_13 ]
214221 } ) . then (
215- ( result : MLKitScanBarcodesResult ) => {
222+ ( result : MLKitScanBarcodesOnDeviceResult ) => {
216223 alert ( {
217224 title : `Result` ,
218225 message : JSON . stringify ( result . barcodes ) ,
@@ -222,11 +229,11 @@ export class MLKitComponent {
222229 . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
223230 }
224231
225- private detectFaces ( imageSource : ImageSource ) : void {
226- firebase . mlkit . facedetection . detectFaces ( {
232+ private detectFacesOnDevice ( imageSource : ImageSource ) : void {
233+ firebase . mlkit . facedetection . detectFacesOnDevice ( {
227234 image : imageSource
228235 } ) . then (
229- ( result : MLKitDetectFacesResult ) => {
236+ ( result : MLKitDetectFacesOnDeviceResult ) => {
230237 alert ( {
231238 title : `Result` ,
232239 message : JSON . stringify ( result . faces ) ,
@@ -236,12 +243,27 @@ export class MLKitComponent {
236243 . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
237244 }
238245
239- private labelImage ( imageSource : ImageSource ) : void {
240- firebase . mlkit . imagelabeling . labelImage ( {
246+ private labelImageOnDevice ( imageSource : ImageSource ) : void {
247+ firebase . mlkit . imagelabeling . labelImageOnDevice ( {
248+ image : imageSource ,
249+ confidenceThreshold : 0.3
250+ } ) . then (
251+ ( result : MLKitImageLabelingOnDeviceResult ) => {
252+ alert ( {
253+ title : `Result` ,
254+ message : JSON . stringify ( result . labels ) ,
255+ okButtonText : "OK"
256+ } ) ;
257+ } )
258+ . catch ( errorMessage => console . log ( "ML Kit error: " + errorMessage ) ) ;
259+ }
260+
261+ private labelImageCloud ( imageSource : ImageSource ) : void {
262+ firebase . mlkit . imagelabeling . labelImageCloud ( {
241263 image : imageSource ,
242264 confidenceThreshold : 0.3
243265 } ) . then (
244- ( result : MLKitImageLabelingResult ) => {
266+ ( result : MLKitImageLabelingCloudResult ) => {
245267 alert ( {
246268 title : `Result` ,
247269 message : JSON . stringify ( result . labels ) ,
0 commit comments