Fix unbindFromSurface bug

pull/392/head
Mattia Iavarone 7 years ago
parent 1e7a4af4da
commit 4adf08107e
  1. 21
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  2. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  3. 4
      demo/src/main/AndroidManifest.xml

@ -66,7 +66,10 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
}); });
} }
// Preview surface is now available. If camera is open, set up. /**
* Preview surface is now available. If camera is open, set up.
* At this point we are sure that mPreview is not null.
*/
@Override @Override
public void onSurfaceAvailable() { public void onSurfaceAvailable() {
LOG.i("onSurfaceAvailable:", "Size is", mPreview.getOutputSurfaceSize()); LOG.i("onSurfaceAvailable:", "Size is", mPreview.getOutputSurfaceSize());
@ -80,8 +83,11 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
}); });
} }
// Preview surface did change its size. Compute a new preview size. /**
// This requires stopping and restarting the preview. * Preview surface did change its size. Compute a new preview size.
* This requires stopping and restarting the preview.
* At this point we are sure that mPreview is not null.
*/
@Override @Override
public void onSurfaceChanged() { public void onSurfaceChanged() {
LOG.i("onSurfaceChanged, size is", mPreview.getOutputSurfaceSize()); LOG.i("onSurfaceChanged, size is", mPreview.getOutputSurfaceSize());
@ -119,8 +125,11 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
return isCameraAvailable() && mPreview != null && mPreview.hasSurface() && !mIsBound; return isCameraAvailable() && mPreview != null && mPreview.hasSurface() && !mIsBound;
} }
// The act of binding an "open" camera to a "ready" preview. /**
// These can happen at different times but we want to end up here. * The act of binding an "open" camera to a "ready" preview.
* These can happen at different times but we want to end up here.
* At this point we are sure that mPreview is not null.
*/
@WorkerThread @WorkerThread
private void bindToSurface() { private void bindToSurface() {
LOG.i("bindToSurface:", "Started"); LOG.i("bindToSurface:", "Started");
@ -275,7 +284,7 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
} }
if (mCamera != null) { if (mCamera != null) {
stopPreview(); stopPreview();
unbindFromSurface(); if (mIsBound) unbindFromSurface();
destroyCamera(); destroyCamera();
} }
mCameraOptions = null; mCameraOptions = null;

@ -601,8 +601,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void open() { public void open() {
if (!isEnabled()) return; if (!isEnabled()) return;
if (mCameraPreview == null) return; // Don't want to start without a preview if (mCameraPreview != null) mCameraPreview.onResume();
mCameraPreview.onResume();
if (checkPermissions(getAudio())) { if (checkPermissions(getAudio())) {
// Update display orientation for current CameraController // Update display orientation for current CameraController
mOrientationHelper.enable(getContext()); mOrientationHelper.enable(getContext());

@ -15,9 +15,9 @@
<activity <activity
android:name=".CameraActivity" android:name=".CameraActivity"
android:theme="@style/Theme.MainActivity" android:theme="@style/Theme.MainActivity"
android:hardwareAccelerated="true"
android:configChanges="orientation|screenLayout|keyboardHidden" android:configChanges="orientation|screenLayout|keyboardHidden"
android:screenOrientation="portrait"> android:screenOrientation="portrait"
android:hardwareAccelerated="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />

Loading…
Cancel
Save