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:
parent
0ba3236ce0
commit
5855c4905e
@ -26,13 +26,36 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
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("+width+", "+height+")");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
|
|
||||||
@ -52,28 +75,22 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
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) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
if (mCamera.isOpened()) {
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
} else {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
Log.e(TAG, "Failed to open native camera");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
if (mCamera != null) {
|
releaseCamera();
|
||||||
synchronized (this) {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.puzzle15;
|
package org.opencv.samples.puzzle15;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -18,6 +20,31 @@ public class puzzle15Activity extends Activity {
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -45,12 +45,12 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
super.surfaceChanged(_holder, format, width, height);
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mat before usage
|
// initialize Mat before usage
|
||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
}
|
}
|
||||||
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void shuffle(int[] array) {
|
public static void shuffle(int[] array) {
|
||||||
@ -185,6 +185,8 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean onTouch(View v, MotionEvent event) {
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
if(mRgba==null) return false;
|
||||||
|
|
||||||
int cols = mRgba.cols();
|
int cols = mRgba.cols();
|
||||||
int rows = mRgba.rows();
|
int rows = mRgba.rows();
|
||||||
float xoffset = (getWidth() - cols) / 2;
|
float xoffset = (getWidth() - cols) / 2;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.colorblobdetect;
|
package org.opencv.samples.colorblobdetect;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
@ -15,6 +17,31 @@ public class ColorBlobDetectionActivity extends Activity {
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -56,12 +56,13 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
super.surfaceChanged(_holder, format, width, height);
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mat before usage
|
// initialize Mat before usage
|
||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean onTouch(View v, MotionEvent event)
|
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());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
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) {
|
synchronized (this) {
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
|
|
||||||
@ -52,28 +75,22 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
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) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
if (mCamera.isOpened()) {
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
} else {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
Log.e(TAG, "Failed to open native camera");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
if (mCamera != null) {
|
releaseCamera();
|
||||||
synchronized (this) {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.fd;
|
package org.opencv.samples.fd;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -15,19 +17,47 @@ public class FdActivity extends Activity {
|
|||||||
private MenuItem mItemFace30;
|
private MenuItem mItemFace30;
|
||||||
private MenuItem mItemFace20;
|
private MenuItem mItemFace20;
|
||||||
|
|
||||||
|
private FdView mView;
|
||||||
|
|
||||||
public static float minFaceSize = 0.5f;
|
public static float minFaceSize = 0.5f;
|
||||||
|
|
||||||
public FdActivity() {
|
public FdActivity() {
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(new FdView(this));
|
mView = new FdView(this);
|
||||||
|
setContentView(mView);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -62,14 +62,14 @@ class FdView extends SampleCvViewBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
super.surfaceChanged(_holder, format, width, height);
|
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mGray = new Mat();
|
mGray = new Mat();
|
||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -28,13 +28,36 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
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("+width+", "+height+")");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
|
|
||||||
@ -54,28 +77,22 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
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) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
if (mCamera.isOpened()) {
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
} else {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
Log.e(TAG, "Failed to open native camera");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
if (mCamera != null) {
|
releaseCamera();
|
||||||
synchronized (this) {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.imagemanipulations;
|
package org.opencv.samples.imagemanipulations;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -8,7 +10,8 @@ import android.view.MenuItem;
|
|||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
|
|
||||||
public class ImageManipulationsActivity extends Activity {
|
public class ImageManipulationsActivity extends Activity {
|
||||||
private static final String TAG = "Sample::Activity";
|
|
||||||
|
private static final String TAG = "Sample-ImageManipulations::Activity";
|
||||||
|
|
||||||
public static final int VIEW_MODE_RGBA = 0;
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
public static final int VIEW_MODE_HIST = 1;
|
public static final int VIEW_MODE_HIST = 1;
|
||||||
@ -30,17 +33,45 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
|
|
||||||
public static int viewMode = VIEW_MODE_RGBA;
|
public static int viewMode = VIEW_MODE_RGBA;
|
||||||
|
|
||||||
|
private ImageManipulationsView mView;
|
||||||
|
|
||||||
public ImageManipulationsActivity() {
|
public ImageManipulationsActivity() {
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(new ImageManipulationsView(this));
|
mView = new ImageManipulationsView(this);
|
||||||
|
setContentView(mView);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -55,9 +55,7 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
super.surfaceChanged(_holder, format, width, height);
|
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mGray = new Mat();
|
mGray = new Mat();
|
||||||
@ -83,6 +81,8 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
mP1 = new Point();
|
mP1 = new Point();
|
||||||
mP2 = new Point();
|
mP2 = new Point();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateAuxiliaryMats() {
|
private void CreateAuxiliaryMats() {
|
||||||
|
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "Sample-ImageManipulations::SurfaceView";
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private VideoCapture mCamera;
|
private VideoCapture mCamera;
|
||||||
@ -28,13 +28,36 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
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) {
|
synchronized (this) {
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
|
|
||||||
@ -54,28 +77,22 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
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) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
if (mCamera.isOpened()) {
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
} else {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
Log.e(TAG, "Failed to open native camera");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
if (mCamera != null) {
|
releaseCamera();
|
||||||
synchronized (this) {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
@ -88,8 +105,10 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera == null)
|
if (mCamera == null) {
|
||||||
|
Log.i(TAG, "mCamera == null");
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (!mCamera.grab()) {
|
if (!mCamera.grab()) {
|
||||||
Log.e(TAG, "mCamera.grab() failed");
|
Log.e(TAG, "mCamera.grab() failed");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Tutorial 1 Basic - 0. Android Camera</string>
|
<string name="app_name">Tutorial 0 (Basic) - Android Camera</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.tutorial0;
|
package org.opencv.samples.tutorial0;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -19,6 +21,31 @@ public class Sample0Base extends Activity {
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -63,6 +63,7 @@ class Sample0View extends SampleViewBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStared(int previewWidth, int previewHeight) {
|
protected void onPreviewStared(int previewWidth, int previewHeight) {
|
||||||
|
Log.i(TAG, "onPreviewStared("+previewWidth+", "+previewHeight+")");
|
||||||
/* Create a bitmap that will be used through to calculate the image to */
|
/* Create a bitmap that will be used through to calculate the image to */
|
||||||
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||||
mRGBA = new int[previewWidth * previewHeight];
|
mRGBA = new int[previewWidth * previewHeight];
|
||||||
@ -70,12 +71,19 @@ class Sample0View extends SampleViewBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStopped() {
|
protected void onPreviewStopped() {
|
||||||
|
Log.i(TAG, "onPreviewStopped");
|
||||||
|
if(mBitmap != null) {
|
||||||
mBitmap.recycle();
|
mBitmap.recycle();
|
||||||
mBitmap = null;
|
mBitmap = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mRGBA != null) {
|
||||||
mRGBA = null;
|
mRGBA = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setViewMode(int viewMode) {
|
public void setViewMode(int viewMode) {
|
||||||
|
Log.i(TAG, "setViewMode("+viewMode+")");
|
||||||
mViewMode = viewMode;
|
mViewMode = viewMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -50,8 +50,44 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "openCamera");
|
||||||
|
releaseCamera();
|
||||||
|
mCamera = Camera.open();
|
||||||
|
if(mCamera == null) {
|
||||||
|
Log.e(TAG, "Can't open camera!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||||
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
|
synchronized (SampleViewBase.this) {
|
||||||
|
System.arraycopy(data, 0, mFrame, 0, data.length);
|
||||||
|
SampleViewBase.this.notify();
|
||||||
|
}
|
||||||
|
camera.addCallbackBuffer(mBuffer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void releaseCamera() {
|
||||||
|
Log.i(TAG, "releaseCamera");
|
||||||
|
mThreadRun = false;
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera != null) {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
mCamera.setPreviewCallback(null);
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onPreviewStopped();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupCamera(int width, int height) {
|
||||||
|
Log.i(TAG, "setupCamera");
|
||||||
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
@ -85,7 +121,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
||||||
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
||||||
mBuffer = new byte[size];
|
mBuffer = new byte[size];
|
||||||
/* The buffer where the current frame will be coppied */
|
/* The buffer where the current frame will be copied */
|
||||||
mFrame = new byte [size];
|
mFrame = new byte [size];
|
||||||
mCamera.addCallbackBuffer(mBuffer);
|
mCamera.addCallbackBuffer(mBuffer);
|
||||||
|
|
||||||
@ -102,36 +138,21 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
Log.i(TAG, "surfaceChanged");
|
||||||
|
setupCamera(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = Camera.open();
|
|
||||||
|
|
||||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
|
||||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
|
||||||
synchronized (SampleViewBase.this) {
|
|
||||||
System.arraycopy(data, 0, mFrame, 0, data.length);
|
|
||||||
SampleViewBase.this.notify();
|
|
||||||
}
|
|
||||||
camera.addCallbackBuffer(mBuffer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
mThreadRun = false;
|
releaseCamera();
|
||||||
if (mCamera != null) {
|
|
||||||
synchronized (this) {
|
|
||||||
mCamera.stopPreview();
|
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onPreviewStopped();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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() */
|
||||||
@ -175,5 +196,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finishing processing thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Tutorial 1 Basic - 1. Add OpenCV</string>
|
<string name="app_name">Tutorial 1 (Basic) - Add OpenCV</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.tutorial1;
|
package org.opencv.samples.tutorial1;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -19,6 +21,31 @@ public class Sample1Java extends Activity {
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -2,16 +2,15 @@ package org.opencv.samples.tutorial1;
|
|||||||
|
|
||||||
import org.opencv.android.Utils;
|
import org.opencv.android.Utils;
|
||||||
import org.opencv.core.Core;
|
import org.opencv.core.Core;
|
||||||
|
import org.opencv.core.CvType;
|
||||||
import org.opencv.core.Mat;
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.core.Point;
|
import org.opencv.core.Point;
|
||||||
import org.opencv.core.Scalar;
|
import org.opencv.core.Scalar;
|
||||||
import org.opencv.core.CvType;
|
|
||||||
import org.opencv.imgproc.Imgproc;
|
import org.opencv.imgproc.Imgproc;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.SurfaceHolder;
|
|
||||||
|
|
||||||
class Sample1View extends SampleViewBase {
|
class Sample1View extends SampleViewBase {
|
||||||
|
|
||||||
@ -94,7 +93,7 @@ class Sample1View extends SampleViewBase {
|
|||||||
try {
|
try {
|
||||||
Utils.matToBitmap(mRgba, bmp);
|
Utils.matToBitmap(mRgba, bmp);
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
Log.e("org.opencv.samples.tutorial1", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
bmp = null;
|
bmp = null;
|
||||||
}
|
}
|
||||||
|
@ -49,10 +49,45 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.setPreviewDisplay(null);
|
mCamera.setPreviewDisplay(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "openCamera");
|
||||||
if (mCamera != null) {
|
releaseCamera();
|
||||||
|
mCamera = Camera.open();
|
||||||
|
if(mCamera == null) {
|
||||||
|
Log.e(TAG, "Can't open camera!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||||
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
|
synchronized (SampleViewBase.this) {
|
||||||
|
System.arraycopy(data, 0, mFrame, 0, data.length);
|
||||||
|
SampleViewBase.this.notify();
|
||||||
|
}
|
||||||
|
camera.addCallbackBuffer(mBuffer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void releaseCamera() {
|
||||||
|
Log.i(TAG, "releaseCamera");
|
||||||
|
mThreadRun = false;
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera != null) {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
mCamera.setPreviewCallback(null);
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onPreviewStopped();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupCamera(int width, int height) {
|
||||||
|
Log.i(TAG, "setupCamera");
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera != null) {
|
||||||
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;
|
||||||
@ -85,7 +120,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
||||||
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
||||||
mBuffer = new byte[size];
|
mBuffer = new byte[size];
|
||||||
/* The buffer where the current frame will be coppied */
|
/* The buffer where the current frame will be copied */
|
||||||
mFrame = new byte [size];
|
mFrame = new byte [size];
|
||||||
mCamera.addCallbackBuffer(mBuffer);
|
mCamera.addCallbackBuffer(mBuffer);
|
||||||
|
|
||||||
@ -102,36 +137,21 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
Log.i(TAG, "surfaceChanged");
|
||||||
|
setupCamera(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = Camera.open();
|
|
||||||
|
|
||||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
|
||||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
|
||||||
synchronized (SampleViewBase.this) {
|
|
||||||
System.arraycopy(data, 0, mFrame, 0, data.length);
|
|
||||||
SampleViewBase.this.notify();
|
|
||||||
}
|
|
||||||
camera.addCallbackBuffer(mBuffer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
mThreadRun = false;
|
releaseCamera();
|
||||||
if (mCamera != null) {
|
|
||||||
synchronized (this) {
|
|
||||||
mCamera.stopPreview();
|
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onPreviewStopped();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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() */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Tutorial 1 Basic - 2. Use OpenCV Camera</string>
|
<string name="app_name">Tutorial 2 (Basic) - Use OpenCV Camera</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.tutorial2;
|
package org.opencv.samples.tutorial2;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -20,17 +22,45 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
|
|
||||||
public static int viewMode = VIEW_MODE_RGBA;
|
public static int viewMode = VIEW_MODE_RGBA;
|
||||||
|
|
||||||
|
private Sample2View mView;
|
||||||
|
|
||||||
public Sample2NativeCamera() {
|
public Sample2NativeCamera() {
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(new Sample2View(this));
|
mView = new Sample2View(this);
|
||||||
|
setContentView(mView);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
package org.opencv.samples.tutorial2;
|
package org.opencv.samples.tutorial2;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.opencv.android.Utils;
|
import org.opencv.android.Utils;
|
||||||
import org.opencv.core.Core;
|
import org.opencv.core.Core;
|
||||||
import org.opencv.core.Mat;
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.core.MatOfPoint;
|
|
||||||
import org.opencv.core.Point;
|
import org.opencv.core.Point;
|
||||||
import org.opencv.core.Scalar;
|
import org.opencv.core.Scalar;
|
||||||
import org.opencv.highgui.Highgui;
|
import org.opencv.highgui.Highgui;
|
||||||
@ -22,36 +18,25 @@ class Sample2View extends SampleCvViewBase {
|
|||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private Mat mGray;
|
private Mat mGray;
|
||||||
private Mat mIntermediateMat;
|
private Mat mIntermediateMat;
|
||||||
private Mat mIntermediateMat2;
|
|
||||||
private Mat mEmpty;
|
|
||||||
private Scalar lo, hi;
|
|
||||||
private Scalar bl, wh;
|
|
||||||
|
|
||||||
public Sample2View(Context context) {
|
public Sample2View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
super.surfaceChanged(_holder, format, width, height);
|
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mGray = new Mat();
|
mGray = new Mat();
|
||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
mIntermediateMat = new Mat();
|
mIntermediateMat = new Mat();
|
||||||
mIntermediateMat2 = new Mat();
|
|
||||||
mEmpty = new Mat();
|
|
||||||
lo = new Scalar(85, 100, 30);
|
|
||||||
hi = new Scalar(130, 255, 255);
|
|
||||||
bl = new Scalar(0, 0, 0, 255);
|
|
||||||
wh = new Scalar(255, 255, 255, 255);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Bitmap processFrame(VideoCapture capture) {
|
protected Bitmap processFrame(VideoCapture capture) {
|
||||||
/**/
|
|
||||||
switch (Sample2NativeCamera.viewMode) {
|
switch (Sample2NativeCamera.viewMode) {
|
||||||
case Sample2NativeCamera.VIEW_MODE_GRAY:
|
case Sample2NativeCamera.VIEW_MODE_GRAY:
|
||||||
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
||||||
@ -62,36 +47,11 @@ class Sample2View extends SampleCvViewBase {
|
|||||||
Core.putText(mRgba, "OpenCV + Android", new Point(10, 100), 3, 2, new Scalar(255, 0, 0, 255), 3);
|
Core.putText(mRgba, "OpenCV + Android", new Point(10, 100), 3, 2, new Scalar(255, 0, 0, 255), 3);
|
||||||
break;
|
break;
|
||||||
case Sample2NativeCamera.VIEW_MODE_CANNY:
|
case Sample2NativeCamera.VIEW_MODE_CANNY:
|
||||||
/*capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
||||||
Imgproc.Canny(mGray, mIntermediateMat, 80, 100);
|
Imgproc.Canny(mGray, mIntermediateMat, 80, 100);
|
||||||
Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
|
Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
|
||||||
*/
|
|
||||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
|
||||||
Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL);
|
|
||||||
Core.inRange(mIntermediateMat, lo, hi, mIntermediateMat2); // green
|
|
||||||
Imgproc.dilate(mIntermediateMat2, mIntermediateMat2, mEmpty);
|
|
||||||
//
|
|
||||||
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
|
||||||
Mat hierarchy = new Mat();
|
|
||||||
Imgproc.findContours(mIntermediateMat2, contours, hierarchy,Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
|
|
||||||
Log.d("processFrame", "contours.size()" + contours.size());
|
|
||||||
double maxArea = 0;
|
|
||||||
int indexMaxArea = -1;
|
|
||||||
for (int i = 0; i < contours.size(); i++) {
|
|
||||||
double s = Imgproc.contourArea(contours.get(i));
|
|
||||||
if(s > maxArea){
|
|
||||||
indexMaxArea = i;
|
|
||||||
maxArea = s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mRgba.setTo(bl);
|
|
||||||
Imgproc.drawContours(mRgba, contours, indexMaxArea, wh);
|
|
||||||
//
|
|
||||||
//Imgproc.cvtColor(mIntermediateMat2, mRgba, Imgproc.COLOR_GRAY2RGBA);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/**/
|
|
||||||
|
|
||||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
@ -99,7 +59,7 @@ class Sample2View extends SampleCvViewBase {
|
|||||||
Utils.matToBitmap(mRgba, bmp);
|
Utils.matToBitmap(mRgba, bmp);
|
||||||
return bmp;
|
return bmp;
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
Log.e("org.opencv.samples.tutorial2", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -118,9 +78,6 @@ class Sample2View extends SampleCvViewBase {
|
|||||||
if (mIntermediateMat != null)
|
if (mIntermediateMat != null)
|
||||||
mIntermediateMat.release();
|
mIntermediateMat.release();
|
||||||
|
|
||||||
if (mIntermediateMat2 != null)
|
|
||||||
mIntermediateMat2.release();
|
|
||||||
|
|
||||||
mRgba = null;
|
mRgba = null;
|
||||||
mGray = null;
|
mGray = null;
|
||||||
mIntermediateMat = null;
|
mIntermediateMat = null;
|
||||||
|
@ -26,13 +26,36 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
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) {
|
synchronized (this) {
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
|
|
||||||
@ -52,28 +75,22 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
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) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
if (mCamera.isOpened()) {
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
} else {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
Log.e(TAG, "Failed to open native camera");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
if (mCamera != null) {
|
releaseCamera();
|
||||||
synchronized (this) {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Tutorial 2 Advanced - 1. Add Native OpenCV</string>
|
<string name="app_name">Tutorial 3 (Advanced) - Add Native OpenCV</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1,23 +1,52 @@
|
|||||||
package org.opencv.samples.tutorial3;
|
package org.opencv.samples.tutorial3;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
|
|
||||||
public class Sample3Native extends Activity {
|
public class Sample3Native extends Activity {
|
||||||
private static final String TAG = "Sample::Activity";
|
private static final String TAG = "Sample::Activity";
|
||||||
|
private Sample3View mView;
|
||||||
|
|
||||||
public Sample3Native() {
|
public Sample3Native() {
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(new Sample3View(this));
|
mView = new Sample3View(this);
|
||||||
|
setContentView(mView);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package org.opencv.samples.tutorial3;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
@ -49,8 +48,44 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.setPreviewDisplay(null);
|
mCamera.setPreviewDisplay(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "openCamera");
|
||||||
|
releaseCamera();
|
||||||
|
mCamera = Camera.open();
|
||||||
|
if(mCamera == null) {
|
||||||
|
Log.e(TAG, "Can't open camera!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||||
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
|
synchronized (SampleViewBase.this) {
|
||||||
|
System.arraycopy(data, 0, mFrame, 0, data.length);
|
||||||
|
SampleViewBase.this.notify();
|
||||||
|
}
|
||||||
|
camera.addCallbackBuffer(mBuffer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void releaseCamera() {
|
||||||
|
Log.i(TAG, "releaseCamera");
|
||||||
|
mThreadRun = false;
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera != null) {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
mCamera.setPreviewCallback(null);
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onPreviewStopped();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupCamera(int width, int height) {
|
||||||
|
Log.i(TAG, "setupCamera");
|
||||||
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
@ -84,7 +119,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
||||||
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
||||||
mBuffer = new byte[size];
|
mBuffer = new byte[size];
|
||||||
/* The buffer where the current frame will be coppied */
|
/* The buffer where the current frame will be copied */
|
||||||
mFrame = new byte [size];
|
mFrame = new byte [size];
|
||||||
mCamera.addCallbackBuffer(mBuffer);
|
mCamera.addCallbackBuffer(mBuffer);
|
||||||
|
|
||||||
@ -101,38 +136,24 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
Log.i(TAG, "surfaceChanged");
|
||||||
|
setupCamera(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = Camera.open();
|
|
||||||
|
|
||||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
|
||||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
|
||||||
synchronized (SampleViewBase.this) {
|
|
||||||
System.arraycopy(data, 0, mFrame, 0, data.length);
|
|
||||||
SampleViewBase.this.notify();
|
|
||||||
}
|
|
||||||
camera.addCallbackBuffer(mBuffer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
mThreadRun = false;
|
releaseCamera();
|
||||||
if (mCamera != null) {
|
|
||||||
synchronized (this) {
|
|
||||||
mCamera.stopPreview();
|
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onPreviewStopped();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* 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() */
|
||||||
protected abstract Bitmap processFrame(byte[] data);
|
protected abstract Bitmap processFrame(byte[] data);
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Tutorial 2 Advanced - 2. Mix Java+Native OpenCV</string>
|
<string name="app_name">Tutorial 4 (Advanced) - Mix Java+Native OpenCV</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.opencv.samples.tutorial4;
|
package org.opencv.samples.tutorial4;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@ -21,6 +23,31 @@ public class Sample4Mixed extends Activity {
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
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. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package org.opencv.samples.tutorial4;
|
package org.opencv.samples.tutorial4;
|
||||||
|
|
||||||
import org.opencv.android.Utils;
|
import org.opencv.android.Utils;
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.CvType;
|
import org.opencv.core.CvType;
|
||||||
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.imgproc.Imgproc;
|
import org.opencv.imgproc.Imgproc;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.SurfaceHolder;
|
|
||||||
|
|
||||||
class Sample4View extends SampleViewBase {
|
class Sample4View extends SampleViewBase {
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package org.opencv.samples.tutorial4;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
@ -49,8 +48,44 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.setPreviewDisplay(null);
|
mCamera.setPreviewDisplay(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "openCamera");
|
||||||
|
releaseCamera();
|
||||||
|
mCamera = Camera.open();
|
||||||
|
if(mCamera == null) {
|
||||||
|
Log.e(TAG, "Can't open camera!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||||
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
|
synchronized (SampleViewBase.this) {
|
||||||
|
System.arraycopy(data, 0, mFrame, 0, data.length);
|
||||||
|
SampleViewBase.this.notify();
|
||||||
|
}
|
||||||
|
camera.addCallbackBuffer(mBuffer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void releaseCamera() {
|
||||||
|
Log.i(TAG, "releaseCamera");
|
||||||
|
mThreadRun = false;
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera != null) {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
mCamera.setPreviewCallback(null);
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onPreviewStopped();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupCamera(int width, int height) {
|
||||||
|
Log.i(TAG, "setupCamera");
|
||||||
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
@ -84,7 +119,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
int size = params.getPreviewSize().width * params.getPreviewSize().height;
|
||||||
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
||||||
mBuffer = new byte[size];
|
mBuffer = new byte[size];
|
||||||
/* The buffer where the current frame will be coppied */
|
/* The buffer where the current frame will be copied */
|
||||||
mFrame = new byte [size];
|
mFrame = new byte [size];
|
||||||
mCamera.addCallbackBuffer(mBuffer);
|
mCamera.addCallbackBuffer(mBuffer);
|
||||||
|
|
||||||
@ -101,38 +136,25 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
Log.i(TAG, "surfaceChanged");
|
||||||
|
setupCamera(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = Camera.open();
|
|
||||||
|
|
||||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
|
||||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
|
||||||
synchronized (SampleViewBase.this) {
|
|
||||||
System.arraycopy(data, 0, mFrame, 0, data.length);
|
|
||||||
SampleViewBase.this.notify();
|
|
||||||
}
|
|
||||||
camera.addCallbackBuffer(mBuffer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
mThreadRun = false;
|
releaseCamera();
|
||||||
if (mCamera != null) {
|
|
||||||
synchronized (this) {
|
|
||||||
mCamera.stopPreview();
|
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onPreviewStopped();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* 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() */
|
||||||
protected abstract Bitmap processFrame(byte[] data);
|
protected abstract Bitmap processFrame(byte[] data);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user