17
17
import com .facebook .react .uimanager .ThemedReactContext ;
18
18
import com .google .android .cameraview .CameraView ;
19
19
import com .google .android .gms .vision .face .Face ;
20
+ import com .google .android .gms .vision .text .Text ;
21
+ import com .google .android .gms .vision .text .TextBlock ;
22
+ import com .google .android .gms .vision .text .TextRecognizer ;
20
23
import com .google .zxing .BarcodeFormat ;
21
24
import com .google .zxing .DecodeHintType ;
22
25
import com .google .zxing .MultiFormatReader ;
27
30
import org .reactnative .camera .tasks .FaceDetectorAsyncTask ;
28
31
import org .reactnative .camera .tasks .FaceDetectorAsyncTaskDelegate ;
29
32
import org .reactnative .camera .tasks .ResolveTakenPictureAsyncTask ;
33
+ import org .reactnative .camera .tasks .TextRecognizerAsyncTask ;
34
+ import org .reactnative .camera .tasks .TextRecognizerAsyncTaskDelegate ;
30
35
import org .reactnative .camera .utils .ImageDimensions ;
31
36
import org .reactnative .camera .utils .RNFileUtils ;
32
37
import org .reactnative .facedetector .RNFaceDetector ;
41
46
import java .util .concurrent .ConcurrentHashMap ;
42
47
import java .util .concurrent .ConcurrentLinkedQueue ;
43
48
44
- public class RNCameraView extends CameraView implements LifecycleEventListener , BarCodeScannerAsyncTaskDelegate , FaceDetectorAsyncTaskDelegate {
49
+ public class RNCameraView extends CameraView implements LifecycleEventListener , BarCodeScannerAsyncTaskDelegate , FaceDetectorAsyncTaskDelegate ,
50
+ TextRecognizerAsyncTaskDelegate {
45
51
private ThemedReactContext mThemedReactContext ;
46
52
private Queue <Promise > mPictureTakenPromises = new ConcurrentLinkedQueue <>();
47
53
private Map <Promise , ReadableMap > mPictureTakenOptions = new ConcurrentHashMap <>();
@@ -55,12 +61,15 @@ public class RNCameraView extends CameraView implements LifecycleEventListener,
55
61
// Concurrency lock for scanners to avoid flooding the runtime
56
62
public volatile boolean barCodeScannerTaskLock = false ;
57
63
public volatile boolean faceDetectorTaskLock = false ;
64
+ public volatile boolean textRecognizerTaskLock = false ;
58
65
59
66
// Scanning-related properties
60
67
private final MultiFormatReader mMultiFormatReader = new MultiFormatReader ();
61
68
private final RNFaceDetector mFaceDetector ;
69
+ private final TextRecognizer mTextRecognizer ;
62
70
private boolean mShouldDetectFaces = false ;
63
71
private boolean mShouldScanBarCodes = false ;
72
+ private boolean mShouldRecognizeText = false ;
64
73
private int mFaceDetectorMode = RNFaceDetector .FAST_MODE ;
65
74
private int mFaceDetectionLandmarks = RNFaceDetector .NO_LANDMARKS ;
66
75
private int mFaceDetectionClassifications = RNFaceDetector .NO_CLASSIFICATIONS ;
@@ -71,6 +80,7 @@ public RNCameraView(ThemedReactContext themedReactContext) {
71
80
mThemedReactContext = themedReactContext ;
72
81
mFaceDetector = new RNFaceDetector (themedReactContext );
73
82
setupFaceDetector ();
83
+ mTextRecognizer = new TextRecognizer .Builder (themedReactContext ).build ();
74
84
themedReactContext .addLifecycleEventListener (this );
75
85
76
86
addCallback (new Callback () {
@@ -121,6 +131,12 @@ public void onFramePreview(CameraView cameraView, byte[] data, int width, int he
121
131
FaceDetectorAsyncTaskDelegate delegate = (FaceDetectorAsyncTaskDelegate ) cameraView ;
122
132
new FaceDetectorAsyncTask (delegate , mFaceDetector , data , width , height , correctRotation ).execute ();
123
133
}
134
+
135
+ if (mShouldRecognizeText && !textRecognizerTaskLock && cameraView instanceof TextRecognizerAsyncTaskDelegate ) {
136
+ textRecognizerTaskLock = true ;
137
+ TextRecognizerAsyncTaskDelegate delegate = (TextRecognizerAsyncTaskDelegate ) cameraView ;
138
+ new TextRecognizerAsyncTask (delegate , mTextRecognizer , data , width , height , correctRotation ).execute ();
139
+ }
124
140
}
125
141
});
126
142
}
@@ -145,7 +161,7 @@ public void requestLayout() {
145
161
@ Override
146
162
public void onViewAdded (View child ) {
147
163
if (this .getView () == child || this .getView () == null ) return ;
148
- // remove and readd view to make sure it is in the back.
164
+ // remove and read view to make sure it is in the back.
149
165
// @TODO figure out why there was a z order issue in the first place and fix accordingly.
150
166
this .removeView (this .getView ());
151
167
this .addView (this .getView (), 0 );
@@ -210,7 +226,7 @@ private void initBarcodeReader() {
210
226
211
227
public void setShouldScanBarCodes (boolean shouldScanBarCodes ) {
212
228
this .mShouldScanBarCodes = shouldScanBarCodes ;
213
- setScanning (mShouldDetectFaces || mShouldScanBarCodes );
229
+ setScanning (mShouldDetectFaces || mShouldScanBarCodes || mShouldRecognizeText );
214
230
}
215
231
216
232
public void onBarCodeRead (Result barCode ) {
@@ -260,7 +276,7 @@ public void setFaceDetectionMode(int mode) {
260
276
261
277
public void setShouldDetectFaces (boolean shouldDetectFaces ) {
262
278
this .mShouldDetectFaces = shouldDetectFaces ;
263
- setScanning (mShouldDetectFaces || mShouldScanBarCodes );
279
+ setScanning (mShouldDetectFaces || mShouldScanBarCodes || mShouldRecognizeText );
264
280
}
265
281
266
282
public void onFacesDetected (SparseArray <Face > facesReported , int sourceWidth , int sourceHeight , int sourceRotation ) {
@@ -287,6 +303,28 @@ public void onFaceDetectingTaskCompleted() {
287
303
faceDetectorTaskLock = false ;
288
304
}
289
305
306
+ public void setShouldRecognizeText (boolean shouldRecognizeText ) {
307
+ this .mShouldRecognizeText = shouldRecognizeText ;
308
+ setScanning (mShouldDetectFaces || mShouldScanBarCodes || mShouldRecognizeText );
309
+ }
310
+
311
+ @ Override
312
+ public void onTextRecognized (SparseArray <TextBlock > textBlocks , int sourceWidth , int sourceHeight , int sourceRotation ) {
313
+ if (!mShouldRecognizeText ) {
314
+ return ;
315
+ }
316
+
317
+ SparseArray <TextBlock > textBlocksDetected = textBlocks == null ? new SparseArray <TextBlock >() : textBlocks ;
318
+ ImageDimensions dimensions = new ImageDimensions (sourceWidth , sourceHeight , sourceRotation , getFacing ());
319
+
320
+ RNCameraViewHelper .emitTextRecognizedEvent (this , textBlocksDetected , dimensions );
321
+ }
322
+
323
+ @ Override
324
+ public void onTextRecognizerTaskCompleted () {
325
+ textRecognizerTaskLock = false ;
326
+ }
327
+
290
328
@ Override
291
329
public void onHostResume () {
292
330
if (hasCameraPermissions ()) {
0 commit comments