Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. import android.util.Log;
  2. import android.widget.FrameLayout;
  3. import android.widget.FrameLayout.LayoutParams;
  4. import com.ensighten.Ensighten;
  5. import com.facebook.react.bridge.Arguments;
  6. import com.facebook.react.bridge.ReactContext;
  7. import com.facebook.react.bridge.WritableMap;
  8. import com.facebook.react.uimanager.events.RCTEventEmitter;
  9. import ds.danskespil.lotto.CustomModules.BarcodeScannerModule;
  10. import ds.danskespil.lotto.CustomModules.BarcodeScannerModule$IScan;
  11. import ds.danskespil.lotto.CustomViews.BarcodeScanner.CameraUtility.CameraSource;
  12. import ds.danskespil.lotto.CustomViews.BarcodeScanner.CameraUtility.CameraSourcePreview;
  13. import ds.shape.dk.pam.utils.StringUtils;
  14. import java.io.IOException;
  15.  
  16. public class BarcodeScanner extends FrameLayout {
  17.     public boolean isStarted;
  18.     private String lastNonTorchMode = null;
  19.     private CameraSourcePreview mPreview;
  20.  
  21.     class C19501 implements BarcodeScannerModule$IScan {
  22.         C19501() {
  23.         }
  24.  
  25.         public void onScan(String barcode) {
  26.             Ensighten.evaluateEvent(this, "onScan", new Object[]{barcode});
  27.             WritableMap event = Arguments.createMap();
  28.             event.putString("barcode", barcode);
  29.             ((RCTEventEmitter) ((ReactContext) BarcodeScanner.this.getContext()).getJSModule(RCTEventEmitter.class)).receiveEvent(BarcodeScanner.this.getId(), "topChange", event);
  30.         }
  31.     }
  32.  
  33.     public BarcodeScanner(ReactContext context) {
  34.         super(context);
  35.         this.mPreview = new CameraSourcePreview(context);
  36.         this.mPreview.setLayoutParams(new LayoutParams(-1, -1));
  37.         addView(this.mPreview);
  38.     }
  39.  
  40.     protected void startCamera() {
  41.         Ensighten.evaluateEvent(this, "startCamera", null);
  42.         if (BarcodeScannerModule.getSingleton().getCameraSource() != null) {
  43.             BarcodeScannerModule.getSingleton().stopScanner();
  44.         }
  45.         BarcodeScannerModule.getSingleton().createCameraSource(true, false, new C19501(), getMeasuredWidth(), getMeasuredHeight());
  46.         startCameraSource();
  47.     }
  48.  
  49.     private void stopCamera() {
  50.         Ensighten.evaluateEvent(this, "stopCamera", null);
  51.         if (this.mPreview != null) {
  52.             this.mPreview.stop();
  53.         }
  54.     }
  55.  
  56.     public void toggleFlash() {
  57.         Ensighten.evaluateEvent(this, "toggleFlash", null);
  58.         CameraSource cameraSource = BarcodeScannerModule.getSingleton().getCameraSource();
  59.         if (cameraSource != null) {
  60.             String flashMode = cameraSource.getFlashMode();
  61.             if (!StringUtils.isNullOrEmpty(flashMode)) {
  62.                 if (this.lastNonTorchMode == null && !flashMode.equals("torch")) {
  63.                     this.lastNonTorchMode = flashMode;
  64.                 }
  65.                 if (!flashMode.equals("torch")) {
  66.                     cameraSource.setFlashMode("torch");
  67.                 } else if (this.lastNonTorchMode == null) {
  68.                     cameraSource.setFlashMode("off");
  69.                 } else {
  70.                     cameraSource.setFlashMode(this.lastNonTorchMode);
  71.                 }
  72.             }
  73.         }
  74.     }
  75.  
  76.     public void turnOffFlash() {
  77.         Ensighten.evaluateEvent(this, "turnOffFlash", null);
  78.         CameraSource cameraSource = BarcodeScannerModule.getSingleton().getCameraSource();
  79.         if (cameraSource != null) {
  80.             String flashMode = cameraSource.getFlashMode();
  81.             if (!StringUtils.isNullOrEmpty(flashMode)) {
  82.                 if (this.lastNonTorchMode == null && !flashMode.equals("torch")) {
  83.                     this.lastNonTorchMode = flashMode;
  84.                 }
  85.                 if (!flashMode.equals("torch")) {
  86.                     return;
  87.                 }
  88.                 if (this.lastNonTorchMode == null) {
  89.                     cameraSource.setFlashMode("off");
  90.                 } else {
  91.                     cameraSource.setFlashMode(this.lastNonTorchMode);
  92.                 }
  93.             }
  94.         }
  95.     }
  96.  
  97.     private void startCameraSource() throws SecurityException {
  98.         Ensighten.evaluateEvent(this, "startCameraSource", null);
  99.         CameraSource cameraSource = BarcodeScannerModule.getSingleton().getCameraSource();
  100.         if (cameraSource != null) {
  101.             try {
  102.                 this.mPreview.start(cameraSource);
  103.             } catch (IOException e) {
  104.                 Log.e("ContentValues", "Unable to start camera source.", e);
  105.                 cameraSource.release();
  106.                 BarcodeScannerModule.getSingleton().clearCameraSource();
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement