Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
fix(barcode): better name google variables and correct init
Browse files Browse the repository at this point in the history
  • Loading branch information
jgfidelis committed Apr 14, 2018
1 parent dba607b commit 38e96ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public void setFaceDetectionClassifications(RNCameraView view, int classificatio
}

@ReactProp(name = "googleVisionBarcodeDetectorEnabled")
public void setGoogleVisionBarcodeDetecting(RNCameraView view, boolean barcodeDetectorEnabled) {
view.setShouldDetectBarcodes(barcodeDetectorEnabled);
public void setGoogleVisionBarcodeDetecting(RNCameraView view, boolean googleBarcodeDetectorEnabled) {
view.setShouldGoogleDetectBarcodes(googleBarcodeDetectorEnabled);
}

@ReactProp(name = "googleVisionBarcodeType")
Expand Down
51 changes: 26 additions & 25 deletions android/src/main/java/org/reactnative/camera/RNCameraView.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public class RNCameraView extends CameraView implements LifecycleEventListener,
// Concurrency lock for scanners to avoid flooding the runtime
public volatile boolean barCodeScannerTaskLock = false;
public volatile boolean faceDetectorTaskLock = false;
public volatile boolean barcodeDetectorTaskLock = false;
public volatile boolean googleBarcodeDetectorTaskLock = false;
public volatile boolean textRecognizerTaskLock = false;

// Scanning-related properties
private final MultiFormatReader mMultiFormatReader = new MultiFormatReader();
private MultiFormatReader mMultiFormatReader;
private RNFaceDetector mFaceDetector;
private RNBarcodeDetector mBarcodeDetector;
private RNBarcodeDetector mGoogleBarcodeDetector;
private TextRecognizer mTextRecognizer;
private boolean mShouldDetectFaces = false;
private boolean mShouldDetectBarcodes = false;
private boolean mShouldGoogleDetectBarcodes = false;
private boolean mShouldScanBarCodes = false;
private boolean mShouldRecognizeText = false;
private int mFaceDetectorMode = RNFaceDetector.FAST_MODE;
Expand All @@ -67,7 +67,6 @@ public class RNCameraView extends CameraView implements LifecycleEventListener,

public RNCameraView(ThemedReactContext themedReactContext) {
super(themedReactContext, true);
initBarcodeReader();
mThemedReactContext = themedReactContext;
themedReactContext.addLifecycleEventListener(this);

Expand Down Expand Up @@ -137,10 +136,10 @@ public void onFramePreview(CameraView cameraView, byte[] data, int width, int he
new FaceDetectorAsyncTask(delegate, mFaceDetector, correctData, correctWidth, correctHeight, correctRotation).execute();
}

if (mShouldDetectBarcodes && !barcodeDetectorTaskLock && cameraView instanceof BarcodeDetectorAsyncTaskDelegate) {
barcodeDetectorTaskLock = true;
if (mShouldGoogleDetectBarcodes && !googleBarcodeDetectorTaskLock && cameraView instanceof BarcodeDetectorAsyncTaskDelegate) {
googleBarcodeDetectorTaskLock = true;
BarcodeDetectorAsyncTaskDelegate delegate = (BarcodeDetectorAsyncTaskDelegate) cameraView;
new BarcodeDetectorAsyncTask(delegate, mBarcodeDetector, correctData, correctWidth, correctHeight, correctRotation).execute();
new BarcodeDetectorAsyncTask(delegate, mGoogleBarcodeDetector, correctData, correctWidth, correctHeight, correctRotation).execute();
}

if (mShouldRecognizeText && !textRecognizerTaskLock && cameraView instanceof TextRecognizerAsyncTaskDelegate) {
Expand Down Expand Up @@ -250,6 +249,7 @@ public void record(ReadableMap options, final Promise promise, File cacheDirecto
* Additionally supports [codabar, code128, maxicode, rss14, rssexpanded, upc_a, upc_ean]
*/
private void initBarcodeReader() {
mMultiFormatReader = new MultiFormatReader();
EnumMap<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
EnumSet<BarcodeFormat> decodeFormats = EnumSet.noneOf(BarcodeFormat.class);

Expand All @@ -267,11 +267,11 @@ private void initBarcodeReader() {
}

public void setShouldScanBarCodes(boolean shouldScanBarCodes) {
if (shouldScanBarCodes && mBarcodeDetector == null) {
setupBarcodeDetector();
if (shouldScanBarCodes && mMultiFormatReader == null) {
initBarcodeReader();
}
this.mShouldScanBarCodes = shouldScanBarCodes;
setScanning(mShouldDetectFaces || mShouldDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
setScanning(mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
}

public void onBarCodeRead(Result barCode) {
Expand Down Expand Up @@ -325,15 +325,15 @@ public void setShouldDetectFaces(boolean shouldDetectFaces) {
setupFaceDetector();
}
this.mShouldDetectFaces = shouldDetectFaces;
setScanning(mShouldDetectFaces || mShouldDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
setScanning(mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
}

public void setShouldDetectBarcodes(boolean shouldDetectBarcodes) {
if (shouldDetectBarcodes && mBarcodeDetector == null) {
public void setShouldGoogleDetectBarcodes(boolean shouldDetectBarcodes) {
if (shouldDetectBarcodes && mGoogleBarcodeDetector == null) {
setupBarcodeDetector();
}
this.mShouldDetectBarcodes = shouldDetectBarcodes;
setScanning(mShouldDetectFaces || mShouldDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
this.mShouldGoogleDetectBarcodes = shouldDetectBarcodes;
setScanning(mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
}

public void onFacesDetected(SparseArray<Face> facesReported, int sourceWidth, int sourceHeight, int sourceRotation) {
Expand Down Expand Up @@ -364,8 +364,8 @@ public void onFaceDetectingTaskCompleted() {
* Initial setup of the barcode detector
*/
private void setupBarcodeDetector() {
mBarcodeDetector = new RNBarcodeDetector(mThemedReactContext);
mBarcodeDetector.setBarcodeType(mGoogleVisionBarCodeType);
mGoogleBarcodeDetector = new RNBarcodeDetector(mThemedReactContext);
mGoogleBarcodeDetector.setBarcodeType(mGoogleVisionBarCodeType);
}

/**
Expand All @@ -377,13 +377,13 @@ private void setupTextRecongnizer() {

public void setGoogleVisionBarcodeType(int barcodeType) {
mGoogleVisionBarCodeType = barcodeType;
if (mBarcodeDetector != null) {
mBarcodeDetector.setBarcodeType(barcodeType);
if (mGoogleBarcodeDetector != null) {
mGoogleBarcodeDetector.setBarcodeType(barcodeType);
}
}

public void onBarcodesDetected(SparseArray<Barcode> barcodesReported, int sourceWidth, int sourceHeight, int sourceRotation) {
if (!mShouldDetectBarcodes) {
if (!mShouldGoogleDetectBarcodes) {
return;
}

Expand All @@ -393,7 +393,7 @@ public void onBarcodesDetected(SparseArray<Barcode> barcodesReported, int source
}

public void onBarcodeDetectionError(RNBarcodeDetector barcodeDetector) {
if (!mShouldDetectBarcodes) {
if (!mShouldGoogleDetectBarcodes) {
return;
}

Expand All @@ -402,15 +402,15 @@ public void onBarcodeDetectionError(RNBarcodeDetector barcodeDetector) {

@Override
public void onBarcodeDetectingTaskCompleted() {
barcodeDetectorTaskLock = false;
googleBarcodeDetectorTaskLock = false;
}

public void setShouldRecognizeText(boolean shouldRecognizeText) {
if (shouldRecognizeText && mTextRecognizer == null) {
setupTextRecongnizer();
}
this.mShouldRecognizeText = shouldRecognizeText;
setScanning(mShouldDetectFaces || mShouldDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
setScanning(mShouldDetectFaces || mShouldGoogleDetectBarcodes || mShouldScanBarCodes || mShouldRecognizeText);
}

@Override
Expand Down Expand Up @@ -456,7 +456,8 @@ public void onHostPause() {
@Override
public void onHostDestroy() {
mFaceDetector.release();
mBarcodeDetector.release();
mGoogleBarcodeDetector.release();
mMultiFormatReader = null;
stop();
mThemedReactContext.removeLifecycleEventListener(this);
}
Expand Down

0 comments on commit 38e96ed

Please sign in to comment.