Fixed java camera release in Android tutorial-0 sample

(cherry picked from commit cd59cf3ab5)

Signed-off-by: Andrey Kamaev <andrey.kamaev@itseez.com>
This commit is contained in:
Andrey Kamaev 2012-08-28 17:23:33 +04:00
parent 05630b336b
commit ca0609dde3
2 changed files with 22 additions and 6 deletions

View File

@ -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;

View File

@ -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 = 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<Camera.Size> 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();