Skip to content

Commit

Permalink
Fix black screen bug by make CameraPreview a singleton
Browse files Browse the repository at this point in the history
Fix "Method called after release()" caused by calling stopCameraPreview twice
  • Loading branch information
jiaminglu committed Oct 25, 2016
1 parent dc460ba commit 06e5bc0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions android/src/main/java/com/eguma/barcodescanner/CameraManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 06e5bc0

Please sign in to comment.