mirror of
https://github.com/status-im/react-native-camera.git
synced 2025-02-24 09:48:17 +00:00
Support hints for a subset of the barcode types.
This commit is contained in:
parent
918c00f003
commit
05b5fc727d
@ -18,6 +18,7 @@ public class RCTCamera {
|
||||
private final HashMap<Integer, Integer> _cameraTypeToIndex;
|
||||
private final Map<Number, Camera> _cameras;
|
||||
private boolean _barcodeScannerEnabled = false;
|
||||
private List<String> _barCodeTypes = null;
|
||||
private int _orientation = -1;
|
||||
private int _actualDeviceOrientation = 0;
|
||||
private int _adjustedDeviceOrientation = 0;
|
||||
@ -143,6 +144,14 @@ public class RCTCamera {
|
||||
_barcodeScannerEnabled = barcodeScannerEnabled;
|
||||
}
|
||||
|
||||
public List<String> getBarCodeTypes() {
|
||||
return _barCodeTypes;
|
||||
}
|
||||
|
||||
public void setBarCodeTypes(List<String> barCodeTypes) {
|
||||
_barCodeTypes = barCodeTypes;
|
||||
}
|
||||
|
||||
public int getActualDeviceOrientation() {
|
||||
return _actualDeviceOrientation;
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.view.View;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RCTCameraView extends ViewGroup {
|
||||
private final OrientationEventListener _orientationListener;
|
||||
private final Context _context;
|
||||
@ -109,6 +111,10 @@ public class RCTCameraView extends ViewGroup {
|
||||
RCTCamera.getInstance().setBarcodeScannerEnabled(barcodeScannerEnabled);
|
||||
}
|
||||
|
||||
public void setBarCodeTypes(List<String> types) {
|
||||
RCTCamera.getInstance().setBarCodeTypes(types);
|
||||
}
|
||||
|
||||
private boolean setActualDeviceOrientation(Context context) {
|
||||
int actualDeviceOrientation = getDeviceOrientation(context);
|
||||
if (_actualDeviceOrientation != actualDeviceOrientation) {
|
||||
|
@ -44,7 +44,7 @@ class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceText
|
||||
super(context);
|
||||
this.setSurfaceTextureListener(this);
|
||||
this._cameraType = type;
|
||||
this.initBarcodeReader();
|
||||
this.initBarcodeReader(RCTCamera.getInstance().getBarCodeTypes());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -169,14 +169,73 @@ class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceText
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the barcode decoder.
|
||||
* Parse barcodes as BarcodeFormat constants.
|
||||
*
|
||||
* TODO add hints for the `barCodeTypes` prop instead of the hardcoded `EAN_13`
|
||||
* Supports all iOS codes except [code138, code39mod43, itf14]
|
||||
*
|
||||
* Additionally supports [codabar, code128, maxicode, rss14, rssexpanded, upca, upceanextension]
|
||||
*/
|
||||
private void initBarcodeReader() {
|
||||
private BarcodeFormat parseBarCodeString(String c) {
|
||||
if ("aztec".equals(c)) {
|
||||
return BarcodeFormat.AZTEC;
|
||||
} else if ("ean13".equals(c)) {
|
||||
return BarcodeFormat.EAN_13;
|
||||
} else if ("ean8".equals(c)) {
|
||||
return BarcodeFormat.EAN_8;
|
||||
} else if ("ean8".equals(c)) {
|
||||
return BarcodeFormat.EAN_8;
|
||||
} else if ("qr".equals(c)) {
|
||||
return BarcodeFormat.QR_CODE;
|
||||
} else if ("ean8".equals(c)) {
|
||||
return BarcodeFormat.EAN_8;
|
||||
} else if ("pdf417".equals(c)) {
|
||||
return BarcodeFormat.PDF_417;
|
||||
} else if ("upce".equals(c)) {
|
||||
return BarcodeFormat.UPC_E;
|
||||
} else if ("datamatrix".equals(c)) {
|
||||
return BarcodeFormat.DATA_MATRIX;
|
||||
} else if ("code39".equals(c)) {
|
||||
return BarcodeFormat.CODE_39;
|
||||
} else if ("code93".equals(c)) {
|
||||
return BarcodeFormat.CODE_93;
|
||||
} else if ("interleaved2of5".equals(c)) {
|
||||
return BarcodeFormat.ITF;
|
||||
} else if ("codabar".equals(c)) {
|
||||
return BarcodeFormat.CODABAR;
|
||||
} else if ("code128".equals(c)) {
|
||||
return BarcodeFormat.CODE_128;
|
||||
} else if ("maxicode".equals(c)) {
|
||||
return BarcodeFormat.MAXICODE;
|
||||
} else if ("rss14".equals(c)) {
|
||||
return BarcodeFormat.RSS_14;
|
||||
} else if ("rssexpanded".equals(c)) {
|
||||
return BarcodeFormat.RSS_EXPANDED;
|
||||
} else if ("upca".equals(c)) {
|
||||
return BarcodeFormat.UPC_A;
|
||||
} else if ("upceanextension".equals(c)) {
|
||||
return BarcodeFormat.UPC_EAN_EXTENSION;
|
||||
} else {
|
||||
android.util.Log.v("RCTCamera", "Unsupported code.. [" + c + "]");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the barcode decoder.
|
||||
*/
|
||||
private void initBarcodeReader(List<String> barCodeTypes) {
|
||||
EnumMap<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
|
||||
EnumSet<BarcodeFormat> decodeFormats = EnumSet.noneOf(BarcodeFormat.class);
|
||||
decodeFormats.add(BarcodeFormat.EAN_13);
|
||||
|
||||
if (barCodeTypes != null) {
|
||||
for (String code : barCodeTypes) {
|
||||
BarcodeFormat format = parseBarCodeString(code);
|
||||
if (format != null) {
|
||||
decodeFormats.add(format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
|
||||
_multiFormatReader.setHints(hints);
|
||||
}
|
||||
|
@ -1,9 +1,14 @@
|
||||
package com.lwansbrough.RCTCamera;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.uimanager.*;
|
||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RCTCameraViewManager extends ViewGroupManager<RCTCameraView> {
|
||||
private static final String REACT_CLASS = "RCTCamera";
|
||||
|
||||
@ -64,6 +69,18 @@ public class RCTCameraViewManager extends ViewGroupManager<RCTCameraView> {
|
||||
|
||||
@ReactProp(name = "barcodeScannerEnabled")
|
||||
public void setBarcodeScannerEnabled(RCTCameraView view, boolean barcodeScannerEnabled) {
|
||||
view.setBarcodeScannerEnabled(barcodeScannerEnabled);
|
||||
view.setBarcodeScannerEnabled(barcodeScannerEnabled);
|
||||
}
|
||||
|
||||
@ReactProp(name = "barCodeTypes")
|
||||
public void setBarCodeTypes(RCTCameraView view, ReadableArray barCodeTypes) {
|
||||
if (barCodeTypes == null) {
|
||||
return;
|
||||
}
|
||||
List<String> result = new ArrayList<String>(barCodeTypes.size());
|
||||
for (int i = 0; i < barCodeTypes.size(); i++) {
|
||||
result.add(barCodeTypes.getString(i));
|
||||
}
|
||||
view.setBarCodeTypes(result);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user