Merge branch '331-viewfinder' into 'master'

Reinitialise viewfinder when camera view is shown

Thanks to @str4d for finding the cause of the bug. Closes #331.

See merge request !165
This commit is contained in:
akwizgran
2016-05-03 14:38:37 +00:00

View File

@@ -44,20 +44,19 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
public CameraView(Context context) { public CameraView(Context context) {
super(context); super(context);
initialize();
} }
public CameraView(Context context, AttributeSet attrs) { public CameraView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
initialize();
} }
public CameraView(Context context, AttributeSet attrs, int defStyleAttr) { public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
initialize();
} }
private void initialize() { @Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setKeepScreenOn(true); setKeepScreenOn(true);
SurfaceHolder holder = getHolder(); SurfaceHolder holder = getHolder();
if (Build.VERSION.SDK_INT < 11) if (Build.VERSION.SDK_INT < 11)
@@ -65,6 +64,13 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
holder.addCallback(this); holder.addCallback(this);
} }
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
setKeepScreenOn(false);
getHolder().removeCallback(this);
}
public void start(Camera camera, PreviewConsumer previewConsumer, public void start(Camera camera, PreviewConsumer previewConsumer,
int rotationDegrees) { int rotationDegrees) {
this.camera = camera; this.camera = camera;
@@ -93,9 +99,7 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
camera.startPreview(); camera.startPreview();
if (autoFocus) camera.autoFocus(this); if (autoFocus) camera.autoFocus(this);
previewConsumer.start(camera); previewConsumer.start(camera);
} catch (IOException e) { } catch (IOException | RuntimeException e) {
LOG.log(WARNING, "Error starting camera preview", e);
} catch (RuntimeException e) {
LOG.log(WARNING, "Error starting camera preview", e); LOG.log(WARNING, "Error starting camera preview", e);
} }
} }
@@ -246,7 +250,6 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
public void surfaceDestroyed(SurfaceHolder holder) { public void surfaceDestroyed(SurfaceHolder holder) {
LOG.info("Surface destroyed"); LOG.info("Surface destroyed");
surfaceExists = false; surfaceExists = false;
holder.removeCallback(this);
} }
public void onAutoFocus(boolean success, final Camera camera) { public void onAutoFocus(boolean success, final Camera camera) {