3030import org .reactnative .camera .tasks .*;
3131import org .reactnative .camera .utils .RNFileUtils ;
3232import org .reactnative .facedetector .RNFaceDetector ;
33+ import org .reactnative .imagelabeler .RNImageLabeler ;
3334
3435import java .io .ByteArrayOutputStream ;
3536import java .io .File ;
3940import java .util .concurrent .ConcurrentLinkedQueue ;
4041
4142public class RNCameraView extends CameraView implements LifecycleEventListener , BarCodeScannerAsyncTaskDelegate , FaceDetectorAsyncTaskDelegate ,
42- BarcodeDetectorAsyncTaskDelegate , TextRecognizerAsyncTaskDelegate , PictureSavedDelegate {
43+ BarcodeDetectorAsyncTaskDelegate , TextRecognizerAsyncTaskDelegate , ImageLabelerAsyncTaskDelegate , PictureSavedDelegate {
4344 private ThemedReactContext mThemedReactContext ;
4445 private Queue <Promise > mPictureTakenPromises = new ConcurrentLinkedQueue <>();
4546 private Map <Promise , ReadableMap > mPictureTakenOptions = new ConcurrentHashMap <>();
@@ -64,16 +65,19 @@ public class RNCameraView extends CameraView implements LifecycleEventListener,
6465 public volatile boolean faceDetectorTaskLock = false ;
6566 public volatile boolean googleBarcodeDetectorTaskLock = false ;
6667 public volatile boolean textRecognizerTaskLock = false ;
68+ public volatile boolean imageLabelerTaskLock = false ;
6769
6870 // Scanning-related properties
6971 private MultiFormatReader mMultiFormatReader ;
7072 private RNFaceDetector mFaceDetector ;
7173 private RNBarcodeDetector mGoogleBarcodeDetector ;
74+ private RNImageLabeler mImageLabeler ;
7275 private boolean mShouldDetectFaces = false ;
7376 private boolean mShouldGoogleDetectBarcodes = false ;
7477 private boolean mShouldScanBarCodes = false ;
7578 private boolean mShouldRecognizeText = false ;
7679 private boolean mShouldDetectTouches = false ;
80+ private boolean mShouldDetectLabels = false ;
7781 private int mFaceDetectorMode = RNFaceDetector .FAST_MODE ;
7882 private int mFaceDetectionLandmarks = RNFaceDetector .NO_LANDMARKS ;
7983 private int mFaceDetectionClassifications = RNFaceDetector .NO_CLASSIFICATIONS ;
@@ -166,7 +170,9 @@ public void onFramePreview(CameraView cameraView, byte[] data, int width, int he
166170 boolean willCallFaceTask = mShouldDetectFaces && !faceDetectorTaskLock && cameraView instanceof FaceDetectorAsyncTaskDelegate ;
167171 boolean willCallGoogleBarcodeTask = mShouldGoogleDetectBarcodes && !googleBarcodeDetectorTaskLock && cameraView instanceof BarcodeDetectorAsyncTaskDelegate ;
168172 boolean willCallTextTask = mShouldRecognizeText && !textRecognizerTaskLock && cameraView instanceof TextRecognizerAsyncTaskDelegate ;
169- if (!willCallBarCodeTask && !willCallFaceTask && !willCallGoogleBarcodeTask && !willCallTextTask ) {
173+ boolean willCallLabelTask = mShouldDetectLabels && !imageLabelerTaskLock && cameraView instanceof ImageLabelerAsyncTaskDelegate ;
174+
175+ if (!willCallBarCodeTask && !willCallFaceTask && !willCallGoogleBarcodeTask && !willCallTextTask && !willCallLabelTask ) {
170176 return ;
171177 }
172178
@@ -211,6 +217,12 @@ correctRotation, getResources().getDisplayMetrics().density, getFacing(),
211217 TextRecognizerAsyncTaskDelegate delegate = (TextRecognizerAsyncTaskDelegate ) cameraView ;
212218 new TextRecognizerAsyncTask (delegate , mThemedReactContext , data , width , height , correctRotation , getResources ().getDisplayMetrics ().density , getFacing (), getWidth (), getHeight (), mPaddingX , mPaddingY ).execute ();
213219 }
220+
221+ if (willCallLabelTask ) {
222+ imageLabelerTaskLock = true ;
223+ ImageLabelerAsyncTaskDelegate delegate = (ImageLabelerAsyncTaskDelegate ) cameraView ;
224+ new ImageLabelerAsyncTask (delegate , mImageLabeler , data , width , height , correctRotation , getResources ().getDisplayMetrics ().density , getFacing (), getWidth (), getHeight (), mPaddingX , mPaddingY ).execute ();
225+ }
214226 }
215227 });
216228 }
@@ -362,7 +374,7 @@ public void setShouldScanBarCodes(boolean shouldScanBarCodes) {
362374 initBarcodeReader ();
363375 }
364376 this .mShouldScanBarCodes = shouldScanBarCodes ;
365- setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText );
377+ setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText || mShouldDetectLabels );
366378 }
367379
368380 public void onBarCodeRead (Result barCode , int width , int height , byte [] imageData ) {
@@ -483,7 +495,7 @@ public void setShouldDetectFaces(boolean shouldDetectFaces) {
483495 setupFaceDetector ();
484496 }
485497 this .mShouldDetectFaces = shouldDetectFaces ;
486- setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText );
498+ setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText || mShouldDetectLabels );
487499 }
488500
489501 public void onFacesDetected (WritableArray data ) {
@@ -520,7 +532,7 @@ public void setShouldGoogleDetectBarcodes(boolean shouldDetectBarcodes) {
520532 setupBarcodeDetector ();
521533 }
522534 this .mShouldGoogleDetectBarcodes = shouldDetectBarcodes ;
523- setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText );
535+ setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText || mShouldDetectLabels );
524536 }
525537
526538 public void setGoogleVisionBarcodeType (int barcodeType ) {
@@ -571,14 +583,49 @@ public void onBarcodeDetectingTaskCompleted() {
571583 googleBarcodeDetectorTaskLock = false ;
572584 }
573585
586+ /**
587+ * Initial setup of the image labeler
588+ */
589+ private void setupImageLabeler () {
590+ mImageLabeler = new RNImageLabeler (mThemedReactContext );
591+ }
592+
593+ public void setShouldDetectLabels (boolean shouldDetectLabels ) {
594+ if (shouldDetectLabels && mImageLabeler == null ) {
595+ setupImageLabeler ();
596+ }
597+ this .mShouldDetectLabels = shouldDetectLabels ;
598+ setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText || mShouldDetectLabels );
599+ }
600+
601+ public void onLabelsDetected (WritableArray labelsDetected ) {
602+ if (!mShouldDetectLabels ) {
603+ return ;
604+ }
605+ RNCameraViewHelper .emitLabelsDetectedEvent (this , labelsDetected );
606+ }
607+
608+ public void onImageLabelingError (RNImageLabeler imageLabeler ) {
609+ if (!mShouldDetectLabels ) {
610+ return ;
611+ }
612+
613+ RNCameraViewHelper .emitImageLabelingErrorEvent (this , imageLabeler );
614+ }
615+
616+ @ Override
617+ public void onImageLabelingTaskCompleted () {
618+ imageLabelerTaskLock = false ;
619+ }
620+
574621 /**
575622 *
576623 * Text recognition
577624 */
578625
579626 public void setShouldRecognizeText (boolean shouldRecognizeText ) {
580627 this .mShouldRecognizeText = shouldRecognizeText ;
581- setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText );
628+ setScanning (mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText || mShouldDetectLabels );
582629 }
583630
584631 public void onTextRecognized (WritableArray serializedData ) {
0 commit comments