11import { ImageSource } from "tns-core-modules/image-source" ;
22import { MLKitOptions , } from "../" ;
3- import { MLKitRecognizeTextOnDeviceOptions , MLKitRecognizeTextOnDeviceResult } from "./" ;
3+ import { MLKitRecognizeTextOnDeviceOptions , MLKitRecognizeTextResult } from "./" ;
44import { MLKitTextRecognition as MLKitTextRecognitionBase } from "./textrecognition-common" ;
55import {
66 MLKitRecognizeTextCloudOptions ,
7- MLKitRecognizeTextCloudResult ,
8- MLKitRecognizeTextResultBlock ,
9- MLKitRecognizeTextResultLine ,
7+ MLKitRecognizeTextResultBounds ,
108 MLKitRecognizeTextResultElement ,
11- MLKitRecognizeTextResultBounds
9+ MLKitRecognizeTextResultLine
1210} from "./index" ;
1311
1412declare const com : any ;
1513
1614export class MLKitTextRecognition extends MLKitTextRecognitionBase {
1715
18- protected createDetector ( ) : any {
19- return com . google . firebase . ml . vision . FirebaseVision . getInstance ( ) . getVisionTextDetector ( ) ;
16+ protected createDetector ( ) : any /* FirebaseVisionTextRecognizer */ {
17+ return com . google . firebase . ml . vision . FirebaseVision . getInstance ( ) . getOnDeviceTextRecognizer ( ) ;
2018 }
2119
2220 protected createSuccessListener ( ) : any {
2321 return new com . google . android . gms . tasks . OnSuccessListener ( {
24- onSuccess : textBlocks => {
25- if ( textBlocks . getBlocks ( ) . size ( ) > 0 ) {
22+ onSuccess : firebaseVisionText => {
23+ if ( firebaseVisionText . getTextBlocks ( ) . size ( ) > 0 ) {
2624 this . notify ( {
2725 eventName : MLKitTextRecognition . scanResultEvent ,
2826 object : this ,
29- value : getOnDeviceResult ( textBlocks . getBlocks ( ) )
27+ value : getResult ( firebaseVisionText )
3028 } ) ;
3129 }
3230 }
@@ -48,17 +46,29 @@ function boundingBoxToBounds(rect: any): MLKitRecognizeTextResultBounds {
4846}
4947
5048// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
51- function getOnDeviceResult ( blocks : any ) : MLKitRecognizeTextOnDeviceResult {
52- const blks : MLKitRecognizeTextResultBlock [ ] = [ ] ;
49+ function getResult ( firebaseVisionText : any ) : MLKitRecognizeTextResult {
50+ if ( firebaseVisionText === null ) {
51+ return { } ;
52+ }
53+
54+ const result = < MLKitRecognizeTextResult > { // TODO rename the return type
55+ text : firebaseVisionText . getText ( ) ,
56+ blocks : [ ] ,
57+ android : firebaseVisionText
58+ } ;
5359
54- for ( let i = 0 ; i < blocks . size ( ) ; i ++ ) {
55- const block = blocks . get ( i ) ;
56- const lines = block . getLines ( ) ;
60+ for ( let i = 0 ; i < firebaseVisionText . getTextBlocks ( ) . size ( ) ; i ++ ) {
61+ const textBlock = firebaseVisionText . getTextBlocks ( ) . get ( i ) ;
62+ // const blockText: string = textBlock.getText();
63+ // const blockConfidence: number = textBlock.getConfidence();
64+ const lines = textBlock . getLines ( ) ;
5765
58- const lns : MLKitRecognizeTextResultLine [ ] = [ ] ;
66+ const lns : MLKitRecognizeTextResultLine [ ] = [ ] ;
5967
6068 for ( let j = 0 ; j < lines . size ( ) ; j ++ ) {
6169 const line = lines . get ( j ) ;
70+ // const lineText = line.getText();
71+ // const lineConfidence = line.getConfidence();
6272 const elements = line . getElements ( ) ;
6373
6474 const elms : MLKitRecognizeTextResultElement [ ] = [ ] ;
@@ -78,36 +88,34 @@ function getOnDeviceResult(blocks: any): MLKitRecognizeTextOnDeviceResult {
7888 } ) ;
7989 }
8090
81- blks . push ( {
82- text : block . getText ( ) ,
83- bounds : boundingBoxToBounds ( block . getBoundingBox ( ) ) ,
91+ result . blocks . push ( {
92+ text : textBlock . getText ( ) ,
93+ bounds : boundingBoxToBounds ( textBlock . getBoundingBox ( ) ) ,
8494 lines : lns
8595 } ) ;
8696 }
8797
88- return {
89- blocks : blks
90- } ;
98+ return result ;
9199}
92100
93- export function recognizeTextOnDevice ( options : MLKitRecognizeTextOnDeviceOptions ) : Promise < MLKitRecognizeTextOnDeviceResult > {
101+ export function recognizeTextOnDevice ( options : MLKitRecognizeTextOnDeviceOptions ) : Promise < MLKitRecognizeTextResult > {
94102 return new Promise ( ( resolve , reject ) => {
95103 try {
96- const firebaseVisionTextDetector = com . google . firebase . ml . vision . FirebaseVision . getInstance ( ) . getVisionTextDetector ( ) ;
104+ const firebaseVisionTextRecognizer = com . google . firebase . ml . vision . FirebaseVision . getInstance ( ) . getOnDeviceTextRecognizer ( ) ;
97105
98106 const onSuccessListener = new com . google . android . gms . tasks . OnSuccessListener ( {
99- onSuccess : textBlocks => {
100- resolve ( getOnDeviceResult ( textBlocks . getBlocks ( ) ) ) ;
101- firebaseVisionTextDetector . close ( ) ;
107+ onSuccess : firebaseVisionText => {
108+ resolve ( getResult ( firebaseVisionText ) ) ;
109+ firebaseVisionTextRecognizer . close ( ) ;
102110 }
103111 } ) ;
104112
105113 const onFailureListener = new com . google . android . gms . tasks . OnFailureListener ( {
106114 onFailure : exception => reject ( exception . getMessage ( ) )
107115 } ) ;
108116
109- firebaseVisionTextDetector
110- . detectInImage ( getImage ( options ) )
117+ firebaseVisionTextRecognizer
118+ . processImage ( getImage ( options ) )
111119 . addOnSuccessListener ( onSuccessListener )
112120 . addOnFailureListener ( onFailureListener ) ;
113121
@@ -118,32 +126,31 @@ export function recognizeTextOnDevice(options: MLKitRecognizeTextOnDeviceOptions
118126 } ) ;
119127}
120128
121- export function recognizeTextCloud ( options : MLKitRecognizeTextCloudOptions ) : Promise < MLKitRecognizeTextCloudResult > {
129+ export function recognizeTextCloud ( options : MLKitRecognizeTextCloudOptions ) : Promise < MLKitRecognizeTextResult > {
122130 return new Promise ( ( resolve , reject ) => {
123131 try {
124- const cloudDetectorOptions =
132+ const firebaseVisionCloudTextRecognizerOptions =
125133 new com . google . firebase . ml . vision . cloud . FirebaseVisionCloudDetectorOptions . Builder ( )
126- . setModelType ( options . modelType === "latest" ? com . google . firebase . ml . vision . cloud . FirebaseVisionCloudDetectorOptions . LATEST_MODEL : com . google . firebase . ml . vision . cloud . FirebaseVisionCloudDetectorOptions . STABLE_MODEL )
127- . setMaxResults ( options . maxResults || 10 )
134+ // TODO see 'setLanguageHints' at https://firebase.google.com/docs/ml-kit/android/recognize-text
135+ // .setModelType(options.modelType === "latest" ? com.google.firebase.ml.vision.cloud.FirebaseVisionCloudDetectorOptions.LATEST_MODEL : com.google.firebase.ml.vision.cloud.FirebaseVisionCloudDetectorOptions.STABLE_MODEL)
136+ // .setMaxResults(options.maxResults || 10)
128137 . build ( ) ;
129138
130- const firebaseVisionCloudTextDetector = com . google . firebase . ml . vision . FirebaseVision . getInstance ( ) . getVisionCloudTextDetector ( cloudDetectorOptions ) ;
139+ const firebaseVisionCloudTextRecognizer = com . google . firebase . ml . vision . FirebaseVision . getInstance ( ) . getCloudTextRecognizer ( firebaseVisionCloudTextRecognizerOptions ) ;
131140
132141 const onSuccessListener = new com . google . android . gms . tasks . OnSuccessListener ( {
133- onSuccess : firebaseVisionCloudText => {
134- resolve ( {
135- text : firebaseVisionCloudText ? firebaseVisionCloudText . getText ( ) : null
136- } ) ;
137- firebaseVisionCloudTextDetector . close ( ) ;
142+ onSuccess : firebaseVisionText => {
143+ resolve ( getResult ( firebaseVisionText ) ) ;
144+ firebaseVisionCloudTextRecognizer . close ( ) ;
138145 }
139146 } ) ;
140147
141148 const onFailureListener = new com . google . android . gms . tasks . OnFailureListener ( {
142149 onFailure : exception => reject ( exception . getMessage ( ) )
143150 } ) ;
144151
145- firebaseVisionCloudTextDetector
146- . detectInImage ( getImage ( options ) )
152+ firebaseVisionCloudTextRecognizer
153+ . processImage ( getImage ( options ) )
147154 . addOnSuccessListener ( onSuccessListener )
148155 . addOnFailureListener ( onFailureListener ) ;
149156
0 commit comments