Android samples are updated: onPause()/onResume() release/open camera, a message is shown on camera open error, minor fixes in code and resources
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package org.opencv.samples.colorblobdetect;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Window;
|
||||
@@ -15,6 +17,31 @@ public class ColorBlobDetectionActivity extends Activity {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
mView.releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(this).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton("OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
|
@@ -56,12 +56,13 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
super.surfaceChanged(_holder, format, width, height);
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
synchronized (this) {
|
||||
// initialize Mat before usage
|
||||
mRgba = new Mat();
|
||||
}
|
||||
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
|
||||
public boolean onTouch(View v, MotionEvent event)
|
||||
|
@@ -26,13 +26,36 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
public boolean openCamera() {
|
||||
Log.i(TAG, "openCamera");
|
||||
synchronized (this) {
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
Log.e(TAG, "Failed to open native camera");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void releaseCamera() {
|
||||
Log.i(TAG, "releaseCamera");
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setupCamera(int width, int height) {
|
||||
Log.i(TAG, "setupCamera");
|
||||
synchronized (this) {
|
||||
if (mCamera != null && mCamera.isOpened()) {
|
||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
||||
int mFrameWidth = width;
|
||||
int mFrameHeight = height;
|
||||
|
||||
@@ -52,28 +75,22 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
Log.i(TAG, "surfaceChanged");
|
||||
setupCamera(width, height);
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (mCamera.isOpened()) {
|
||||
(new Thread(this)).start();
|
||||
} else {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
Log.e(TAG, "Failed to open native camera");
|
||||
}
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceDestroyed");
|
||||
if (mCamera != null) {
|
||||
synchronized (this) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
releaseCamera();
|
||||
}
|
||||
|
||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||
|
Reference in New Issue
Block a user