mirror of
https://github.com/opencv/opencv.git
synced 2024-12-04 08:49:14 +08:00
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:
parent
05630b336b
commit
ca0609dde3
@ -6,7 +6,7 @@ import android.util.Log;
|
|||||||
|
|
||||||
class Sample0View extends SampleViewBase {
|
class Sample0View extends SampleViewBase {
|
||||||
|
|
||||||
private static final String TAG = "Sample0View";
|
private static final String TAG = "Sample::View";
|
||||||
int mSize;
|
int mSize;
|
||||||
int[] mRGBA;
|
int[] mRGBA;
|
||||||
private Bitmap mBitmap;
|
private Bitmap mBitmap;
|
||||||
|
@ -23,7 +23,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
private int mFrameWidth;
|
private int mFrameWidth;
|
||||||
private int mFrameHeight;
|
private int mFrameHeight;
|
||||||
private byte[] mFrame;
|
private byte[] mFrame;
|
||||||
private boolean mThreadRun;
|
private volatile boolean mThreadRun;
|
||||||
private byte[] mBuffer;
|
private byte[] mBuffer;
|
||||||
private SurfaceTexture mSf;
|
private SurfaceTexture mSf;
|
||||||
|
|
||||||
@ -55,9 +55,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public boolean openCamera() {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "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) {
|
if(mCamera == null) {
|
||||||
Log.e(TAG, "Can't open camera!");
|
Log.e(TAG, "Failed to open camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +86,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
@ -91,6 +97,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
Log.i(TAG, "setupCamera");
|
Log.i(TAG, "setupCamera");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
|
Log.i(TAG, "setupCamera - " + width + "x" + height);
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
mFrameWidth = width;
|
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) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
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);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +169,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
/* 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) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
|
if (!mThreadRun)
|
||||||
|
break;
|
||||||
bmp = processFrame(mFrame);
|
bmp = processFrame(mFrame);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
Reference in New Issue
Block a user