From ca0609dde30dcbf8131249e0da475995d9aa8302 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Tue, 28 Aug 2012 17:23:33 +0400 Subject: [PATCH] Fixed java camera release in Android tutorial-0 sample (cherry picked from commit cd59cf3ab5c37d1db53394f5143cd87f9047547f) Signed-off-by: Andrey Kamaev --- .../org/opencv/samples/tutorial0/Sample0View.java | 2 +- .../opencv/samples/tutorial0/SampleViewBase.java | 26 +++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/Sample0View.java b/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/Sample0View.java index 769c34c..a027818 100644 --- a/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/Sample0View.java +++ b/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/Sample0View.java @@ -6,7 +6,7 @@ import android.util.Log; class Sample0View extends SampleViewBase { - private static final String TAG = "Sample0View"; + private static final String TAG = "Sample::View"; int mSize; int[] mRGBA; private Bitmap mBitmap; diff --git a/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java b/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java index 376c86a..c1cc158 100644 --- a/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java +++ b/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java @@ -23,7 +23,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde private int mFrameWidth; private int mFrameHeight; private byte[] mFrame; - private boolean mThreadRun; + private volatile boolean mThreadRun; private byte[] mBuffer; private SurfaceTexture mSf; @@ -55,9 +55,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde public boolean openCamera() { Log.i(TAG, "openCamera"); - mCamera = Camera.open(); + mCamera = null; + try { + mCamera = Camera.open(); + } + catch (Exception e){ + Log.e(TAG, "Camera is not available (in use or does not exist)"); + e.printStackTrace(); + } if(mCamera == null) { - Log.e(TAG, "Can't open camera!"); + Log.e(TAG, "Failed to open camera"); return false; } @@ -79,7 +86,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde synchronized (this) { if (mCamera != null) { mCamera.stopPreview(); - mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } @@ -91,6 +97,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde Log.i(TAG, "setupCamera"); synchronized (this) { if (mCamera != null) { + Log.i(TAG, "setupCamera - " + width + "x" + height); Camera.Parameters params = mCamera.getParameters(); List sizes = params.getSupportedPreviewSizes(); mFrameWidth = width; @@ -144,6 +151,14 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { Log.i(TAG, "surfaceChanged"); + // stop preview before making changes + try { + mCamera.stopPreview(); + } catch (Exception e){ + // ignore: tried to stop a non-existent preview + } + + // start preview with new settings setupCamera(width, height); } @@ -154,7 +169,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde public void surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG, "surfaceDestroyed"); - releaseCamera(); } /* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */ @@ -184,6 +198,8 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde synchronized (this) { try { this.wait(); + if (!mThreadRun) + break; bmp = processFrame(mFrame); } catch (InterruptedException e) { e.printStackTrace(); -- 2.7.4