diff --git a/android/src/main/java/com/eguma/barcodescanner/CameraManager.java b/android/src/main/java/com/eguma/barcodescanner/CameraManager.java index e63ceeb..47fa6ff 100644 --- a/android/src/main/java/com/eguma/barcodescanner/CameraManager.java +++ b/android/src/main/java/com/eguma/barcodescanner/CameraManager.java @@ -4,11 +4,19 @@ import java.util.List; public class CameraManager { + private static CameraManager mCameraManager; + private int mCameraId = -1; private Camera mCamera; - public CameraManager() { - mCamera = getCameraInstance(); + private CameraManager() { + } + + public static CameraManager getInstance() { + if (mCameraManager == null) { + mCameraManager = new CameraManager(); + } + return mCameraManager; } public Camera getCamera() { diff --git a/android/src/main/java/com/eguma/barcodescanner/CameraPreview.java b/android/src/main/java/com/eguma/barcodescanner/CameraPreview.java index 8db272d..cea7cf4 100644 --- a/android/src/main/java/com/eguma/barcodescanner/CameraPreview.java +++ b/android/src/main/java/com/eguma/barcodescanner/CameraPreview.java @@ -23,8 +23,8 @@ import com.google.zxing.common.HybridBinarizer; public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { - private CameraManager mCameraManager; - private Camera mCamera; + private static CameraManager mCameraManager; + private static Camera mCamera; private String mCameraType; private Camera.PreviewCallback mPreviewCallback; @@ -37,8 +37,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback public CameraPreview(Context context, Camera.PreviewCallback previewCallback) { super(context); - - mCameraManager = new CameraManager(); + mCameraManager = CameraManager.getInstance(); mAutoFocusHandler = new Handler(); mPreviewCallback = previewCallback; } @@ -89,6 +88,7 @@ public void stopCameraPreview() { mCamera.cancelAutoFocus(); mCamera.setPreviewCallback(null); mCamera.stopPreview(); + mCamera = null; } catch(Exception e) { Log.e(TAG, e.toString(), e); }