mirror of
https://github.com/opencv/opencv.git
synced 2024-11-30 06:10:02 +08:00
Fix Andoid samples: black screen after resume problem
This commit is contained in:
parent
d849f51023
commit
a4235948a7
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||
private static final String TAG = "Sample::SurfaceView";
|
||||
private static final String TAG = "Sample-15puzzle::SurfaceView";
|
||||
|
||||
private SurfaceHolder mHolder;
|
||||
private VideoCapture mCamera;
|
||||
@ -29,24 +29,23 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
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;
|
||||
}
|
||||
}
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
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;
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,109 +12,98 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
/** Activity class implements LoaderCallbackInterface to handle OpenCV initialization status **/
|
||||
public class puzzle15Activity extends Activity
|
||||
{
|
||||
private static final String TAG = "Sample::Activity";
|
||||
private static final String TAG = "Sample::Activity";
|
||||
|
||||
private MenuItem mItemNewGame;
|
||||
private MenuItem mItemToggleNumbers;
|
||||
private puzzle15View mView = null;
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new puzzle15View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public puzzle15Activity()
|
||||
{
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new puzzle15View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public puzzle15Activity() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if( mView!=null && !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(AlertDialog.BUTTON_POSITIVE, "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)
|
||||
{
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
Log.i(TAG, "onCreateOptionsMenu");
|
||||
|
@ -18,7 +18,7 @@ import android.view.View;
|
||||
import android.view.View.OnTouchListener;
|
||||
|
||||
public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
private Mat mRgba;
|
||||
private Mat mRgba;
|
||||
private Mat mRgba15;
|
||||
private Mat[] mCells;
|
||||
private Mat[] mCells15;
|
||||
@ -45,13 +45,13 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
synchronized (this) {
|
||||
// initialize Mat before usage
|
||||
mRgba = new Mat();
|
||||
}
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
|
||||
public static void shuffle(int[] array) {
|
||||
for (int i = array.length; i > 1; i--) {
|
||||
@ -83,11 +83,11 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
return sum % 2 == 0;
|
||||
}
|
||||
|
||||
private void createPuzzle(int cols, int rows) {
|
||||
private void createPuzzle(int cols, int rows, int type) {
|
||||
mCells = new Mat[gridArea];
|
||||
mCells15 = new Mat[gridArea];
|
||||
|
||||
mRgba15 = new Mat(rows, cols, mRgba.type());
|
||||
mRgba15 = new Mat(rows, cols, type);
|
||||
mIndexses = new int[gridArea];
|
||||
|
||||
for (int i = 0; i < gridSize; i++) {
|
||||
@ -122,7 +122,11 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
cols = cols - cols%4;
|
||||
|
||||
if (mCells == null)
|
||||
createPuzzle(cols, rows);
|
||||
createPuzzle(cols, rows, mRgba.type());
|
||||
else if(mRgba15.cols() != cols || mRgba15.rows() != rows) {
|
||||
releaseMats();
|
||||
createPuzzle(cols, rows, mRgba.type());
|
||||
}
|
||||
|
||||
// copy shuffled tiles
|
||||
for (int i = 0; i < gridArea; i++) {
|
||||
@ -141,10 +145,10 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
drawGrid(cols, rows);
|
||||
Bitmap bmp = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888);
|
||||
try {
|
||||
Utils.matToBitmap(mRgba15, bmp);
|
||||
Utils.matToBitmap(mRgba15, bmp);
|
||||
return bmp;
|
||||
} catch(Exception e) {
|
||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
bmp.recycle();
|
||||
return null;
|
||||
}
|
||||
@ -162,32 +166,38 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
super.run();
|
||||
|
||||
synchronized (this) {
|
||||
// Explicitly deallocate Mats
|
||||
if (mCells != null) {
|
||||
for (Mat m : mCells)
|
||||
m.release();
|
||||
}
|
||||
if (mCells15 != null) {
|
||||
for (Mat m : mCells15)
|
||||
m.release();
|
||||
}
|
||||
releaseMats();
|
||||
|
||||
if (mRgba != null)
|
||||
mRgba.release();
|
||||
if (mRgba15 != null)
|
||||
mRgba15.release();
|
||||
|
||||
mRgba = null;
|
||||
mRgba15 = null;
|
||||
mCells = null;
|
||||
mCells15 = null;
|
||||
mIndexses = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void releaseMats() {
|
||||
// Explicitly deallocate Mats
|
||||
if (mCells != null) {
|
||||
for (Mat m : mCells)
|
||||
m.release();
|
||||
}
|
||||
if (mCells15 != null) {
|
||||
for (Mat m : mCells15)
|
||||
m.release();
|
||||
}
|
||||
|
||||
if (mRgba15 != null)
|
||||
mRgba15.release();
|
||||
|
||||
mRgba15 = null;
|
||||
mCells = null;
|
||||
mCells15 = null;
|
||||
mIndexses = null;
|
||||
}
|
||||
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
if(mRgba==null) return false;
|
||||
|
||||
int cols = mRgba.cols();
|
||||
int cols = mRgba.cols();
|
||||
int rows = mRgba.rows();
|
||||
float xoffset = (getWidth() - cols) / 2;
|
||||
float yoffset = (getHeight() - rows) / 2;
|
||||
|
@ -10,101 +10,90 @@ import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class ColorBlobDetectionActivity extends Activity {
|
||||
|
||||
private static final String TAG = "Example/ColorBlobDetection";
|
||||
private ColorBlobDetectionView mView;
|
||||
private static final String TAG = "Sample-ColorBlobDetection::Activity";
|
||||
private ColorBlobDetectionView mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new ColorBlobDetectionView(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new ColorBlobDetectionView(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public ColorBlobDetectionActivity()
|
||||
{
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
public ColorBlobDetectionActivity() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if( (null != mView) && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
}
|
@ -23,26 +23,25 @@ import android.view.View.OnTouchListener;
|
||||
|
||||
public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchListener {
|
||||
|
||||
private Mat mRgba;
|
||||
private Mat mRgba;
|
||||
|
||||
private boolean mIsColorSelected = false;
|
||||
private Scalar mBlobColorRgba = new Scalar(255);
|
||||
private Scalar mBlobColorHsv = new Scalar(255);
|
||||
private ColorBlobDetector mDetector = new ColorBlobDetector();
|
||||
private Mat mSpectrum = new Mat();
|
||||
private static Size SPECTRUM_SIZE = new Size(200, 32);
|
||||
private boolean mIsColorSelected = false;
|
||||
private Scalar mBlobColorRgba = new Scalar(255);
|
||||
private Scalar mBlobColorHsv = new Scalar(255);
|
||||
private ColorBlobDetector mDetector = new ColorBlobDetector();
|
||||
private Mat mSpectrum = new Mat();
|
||||
private static Size SPECTRUM_SIZE = new Size(200, 32);
|
||||
|
||||
// Logcat tag
|
||||
private static final String TAG = "Example/ColorBlobDetection";
|
||||
// Logcat tag
|
||||
private static final String TAG = "Sample-ColorBlobDetection::View";
|
||||
|
||||
private static final Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);
|
||||
private static final Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);
|
||||
|
||||
|
||||
public ColorBlobDetectionView(Context context)
|
||||
{
|
||||
public ColorBlobDetectionView(Context context) {
|
||||
super(context);
|
||||
setOnTouchListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
@ -54,8 +53,7 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
|
||||
public boolean onTouch(View v, MotionEvent event)
|
||||
{
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
int cols = mRgba.cols();
|
||||
int rows = mRgba.rows();
|
||||
|
||||
@ -86,36 +84,33 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
||||
mBlobColorHsv = Core.sumElems(touchedRegionHsv);
|
||||
int pointCount = touchedRect.width*touchedRect.height;
|
||||
for (int i = 0; i < mBlobColorHsv.val.length; i++)
|
||||
{
|
||||
mBlobColorHsv.val[i] /= pointCount;
|
||||
}
|
||||
mBlobColorHsv.val[i] /= pointCount;
|
||||
|
||||
mBlobColorRgba = converScalarHsv2Rgba(mBlobColorHsv);
|
||||
|
||||
Log.i(TAG, "Touched rgba color: (" + mBlobColorRgba.val[0] + ", " + mBlobColorRgba.val[1] +
|
||||
", " + mBlobColorRgba.val[2] + ", " + mBlobColorRgba.val[3] + ")");
|
||||
", " + mBlobColorRgba.val[2] + ", " + mBlobColorRgba.val[3] + ")");
|
||||
|
||||
mDetector.setHsvColor(mBlobColorHsv);
|
||||
mDetector.setHsvColor(mBlobColorHsv);
|
||||
|
||||
Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE);
|
||||
Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE);
|
||||
|
||||
mIsColorSelected = true;
|
||||
|
||||
return false; // don't need subsequent touch events
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Bitmap processFrame(VideoCapture capture) {
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
@Override
|
||||
protected Bitmap processFrame(VideoCapture capture) {
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
if (mIsColorSelected)
|
||||
{
|
||||
mDetector.process(mRgba);
|
||||
List<MatOfPoint> contours = mDetector.getContours();
|
||||
if (mIsColorSelected) {
|
||||
mDetector.process(mRgba);
|
||||
List<MatOfPoint> contours = mDetector.getContours();
|
||||
Log.e(TAG, "Contours count: " + contours.size());
|
||||
Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);
|
||||
Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);
|
||||
|
||||
Mat colorLabel = mRgba.submat(2, 34, 2, 34);
|
||||
colorLabel.setTo(mBlobColorRgba);
|
||||
@ -125,24 +120,23 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
||||
}
|
||||
|
||||
try {
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
} catch(Exception e) {
|
||||
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
bmp.recycle();
|
||||
bmp = null;
|
||||
}
|
||||
|
||||
return bmp;
|
||||
}
|
||||
}
|
||||
|
||||
private Scalar converScalarHsv2Rgba(Scalar hsvColor)
|
||||
{
|
||||
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
|
||||
Mat pointMatRgba = new Mat();
|
||||
Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
|
||||
Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
||||
|
||||
return new Scalar(pointMatRgba.get(0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -11,66 +11,58 @@ import org.opencv.core.MatOfPoint;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class ColorBlobDetector
|
||||
{
|
||||
public void setColorRadius(Scalar radius)
|
||||
{
|
||||
mColorRadius = radius;
|
||||
}
|
||||
public class ColorBlobDetector {
|
||||
public void setColorRadius(Scalar radius) {
|
||||
mColorRadius = radius;
|
||||
}
|
||||
|
||||
public void setHsvColor(Scalar hsvColor)
|
||||
{
|
||||
double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-mColorRadius.val[0] : 0;
|
||||
double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ? hsvColor.val[0]+mColorRadius.val[0] : 255;
|
||||
public void setHsvColor(Scalar hsvColor) {
|
||||
double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-mColorRadius.val[0] : 0;
|
||||
double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ? hsvColor.val[0]+mColorRadius.val[0] : 255;
|
||||
|
||||
mLowerBound.val[0] = minH;
|
||||
mUpperBound.val[0] = maxH;
|
||||
mLowerBound.val[0] = minH;
|
||||
mUpperBound.val[0] = maxH;
|
||||
|
||||
mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
|
||||
mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];
|
||||
mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
|
||||
mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];
|
||||
|
||||
mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
|
||||
mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];
|
||||
mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
|
||||
mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];
|
||||
|
||||
mLowerBound.val[3] = 0;
|
||||
mUpperBound.val[3] = 255;
|
||||
mLowerBound.val[3] = 0;
|
||||
mUpperBound.val[3] = 255;
|
||||
|
||||
Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3);
|
||||
Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3);
|
||||
|
||||
for (int j = 0; j < maxH-minH; j++)
|
||||
{
|
||||
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
|
||||
spectrumHsv.put(0, j, tmp);
|
||||
}
|
||||
for (int j = 0; j < maxH-minH; j++) {
|
||||
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
|
||||
spectrumHsv.put(0, j, tmp);
|
||||
}
|
||||
|
||||
Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
||||
Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
||||
}
|
||||
|
||||
}
|
||||
public Mat getSpectrum() {
|
||||
return mSpectrum;
|
||||
}
|
||||
|
||||
public Mat getSpectrum()
|
||||
{
|
||||
return mSpectrum;
|
||||
}
|
||||
public void setMinContourArea(double area) {
|
||||
mMinContourArea = area;
|
||||
}
|
||||
|
||||
public void setMinContourArea(double area)
|
||||
{
|
||||
mMinContourArea = area;
|
||||
}
|
||||
public void process(Mat rgbaImage) {
|
||||
Mat pyrDownMat = new Mat();
|
||||
|
||||
public void process(Mat rgbaImage)
|
||||
{
|
||||
Mat pyrDownMat = new Mat();
|
||||
Imgproc.pyrDown(rgbaImage, pyrDownMat);
|
||||
Imgproc.pyrDown(pyrDownMat, pyrDownMat);
|
||||
|
||||
Imgproc.pyrDown(rgbaImage, pyrDownMat);
|
||||
Imgproc.pyrDown(pyrDownMat, pyrDownMat);
|
||||
Mat hsvMat = new Mat();
|
||||
Imgproc.cvtColor(pyrDownMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL);
|
||||
|
||||
Mat hsvMat = new Mat();
|
||||
Imgproc.cvtColor(pyrDownMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL);
|
||||
|
||||
Mat Mask = new Mat();
|
||||
Core.inRange(hsvMat, mLowerBound, mUpperBound, Mask);
|
||||
Mat dilatedMask = new Mat();
|
||||
Imgproc.dilate(Mask, dilatedMask, new Mat());
|
||||
Mat Mask = new Mat();
|
||||
Core.inRange(hsvMat, mLowerBound, mUpperBound, Mask);
|
||||
Mat dilatedMask = new Mat();
|
||||
Imgproc.dilate(Mask, dilatedMask, new Mat());
|
||||
|
||||
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
||||
Mat hierarchy = new Mat();
|
||||
@ -80,40 +72,36 @@ public class ColorBlobDetector
|
||||
// Find max contour area
|
||||
double maxArea = 0;
|
||||
Iterator<MatOfPoint> each = contours.iterator();
|
||||
while (each.hasNext())
|
||||
{
|
||||
MatOfPoint wrapper = each.next();
|
||||
double area = Imgproc.contourArea(wrapper);
|
||||
if (area > maxArea)
|
||||
maxArea = area;
|
||||
while (each.hasNext()) {
|
||||
MatOfPoint wrapper = each.next();
|
||||
double area = Imgproc.contourArea(wrapper);
|
||||
if (area > maxArea)
|
||||
maxArea = area;
|
||||
}
|
||||
|
||||
// Filter contours by area and resize to fit the original image size
|
||||
mContours.clear();
|
||||
each = contours.iterator();
|
||||
while (each.hasNext())
|
||||
{
|
||||
MatOfPoint contour = each.next();
|
||||
if (Imgproc.contourArea(contour) > mMinContourArea*maxArea)
|
||||
{
|
||||
Core.multiply(contour, new Scalar(4,4), contour);
|
||||
mContours.add(contour);
|
||||
}
|
||||
while (each.hasNext()) {
|
||||
MatOfPoint contour = each.next();
|
||||
if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
|
||||
Core.multiply(contour, new Scalar(4,4), contour);
|
||||
mContours.add(contour);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<MatOfPoint> getContours()
|
||||
{
|
||||
return mContours;
|
||||
}
|
||||
public List<MatOfPoint> getContours() {
|
||||
return mContours;
|
||||
}
|
||||
|
||||
// Lower and Upper bounds for range checking in HSV color space
|
||||
private Scalar mLowerBound = new Scalar(0);
|
||||
private Scalar mUpperBound = new Scalar(0);
|
||||
// Minimum contour area in percent for contours filtering
|
||||
private static double mMinContourArea = 0.1;
|
||||
// Color radius for range checking in HSV color space
|
||||
private Scalar mColorRadius = new Scalar(25,50,50,0);
|
||||
private Mat mSpectrum = new Mat();
|
||||
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
|
||||
// Lower and Upper bounds for range checking in HSV color space
|
||||
private Scalar mLowerBound = new Scalar(0);
|
||||
private Scalar mUpperBound = new Scalar(0);
|
||||
// Minimum contour area in percent for contours filtering
|
||||
private static double mMinContourArea = 0.1;
|
||||
// Color radius for range checking in HSV color space
|
||||
private Scalar mColorRadius = new Scalar(25,50,50,0);
|
||||
private Mat mSpectrum = new Mat();
|
||||
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
|
||||
}
|
||||
|
@ -29,30 +29,29 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
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;
|
||||
}
|
||||
}
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
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;
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setupCamera(int width, int height) {
|
||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
||||
synchronized (this) {
|
||||
if (mCamera != null && mCamera.isOpened()) {
|
||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||
@ -115,8 +114,8 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
if (bmp != null) {
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
if (canvas != null) {
|
||||
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
|
||||
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
bmp.recycle();
|
||||
|
@ -9,7 +9,7 @@ include ../../sdk/native/jni/OpenCV.mk
|
||||
|
||||
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
|
||||
LOCAL_C_INCLUDES += $(LOCAL_PATH)
|
||||
LOCAL_LDLIBS += -llog -ldl
|
||||
LOCAL_LDLIBS += -llog -ldl
|
||||
|
||||
LOCAL_MODULE := detection_based_tracker
|
||||
|
||||
|
@ -27,25 +27,25 @@ JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeC
|
||||
|
||||
try
|
||||
{
|
||||
DetectionBasedTracker::Parameters DetectorParams;
|
||||
if (faceSize > 0)
|
||||
DetectorParams.minObjectSize = faceSize;
|
||||
result = (jlong)new DetectionBasedTracker(stdFileName, DetectorParams);
|
||||
DetectionBasedTracker::Parameters DetectorParams;
|
||||
if (faceSize > 0)
|
||||
DetectorParams.minObjectSize = faceSize;
|
||||
result = (jlong)new DetectionBasedTracker(stdFileName, DetectorParams);
|
||||
}
|
||||
catch(cv::Exception e)
|
||||
{
|
||||
LOGD("nativeCreateObject catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
LOGD("nativeCreateObject catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOGD("nativeCreateObject catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
return 0;
|
||||
LOGD("nativeCreateObject catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -56,22 +56,22 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDe
|
||||
{
|
||||
try
|
||||
{
|
||||
((DetectionBasedTracker*)thiz)->stop();
|
||||
delete (DetectionBasedTracker*)thiz;
|
||||
((DetectionBasedTracker*)thiz)->stop();
|
||||
delete (DetectionBasedTracker*)thiz;
|
||||
}
|
||||
catch(cv::Exception e)
|
||||
{
|
||||
LOGD("nativeestroyObject catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
LOGD("nativeestroyObject catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOGD("nativeDestroyObject catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
LOGD("nativeDestroyObject catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,21 +80,21 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSt
|
||||
{
|
||||
try
|
||||
{
|
||||
((DetectionBasedTracker*)thiz)->run();
|
||||
((DetectionBasedTracker*)thiz)->run();
|
||||
}
|
||||
catch(cv::Exception e)
|
||||
{
|
||||
LOGD("nativeStart catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
LOGD("nativeStart catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOGD("nativeStart catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
LOGD("nativeStart catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,21 +103,21 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSt
|
||||
{
|
||||
try
|
||||
{
|
||||
((DetectionBasedTracker*)thiz)->stop();
|
||||
((DetectionBasedTracker*)thiz)->stop();
|
||||
}
|
||||
catch(cv::Exception e)
|
||||
{
|
||||
LOGD("nativeStop catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
LOGD("nativeStop catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOGD("nativeStop catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
LOGD("nativeStop catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,28 +126,27 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSe
|
||||
{
|
||||
try
|
||||
{
|
||||
if (faceSize > 0)
|
||||
{
|
||||
DetectionBasedTracker::Parameters DetectorParams = \
|
||||
((DetectionBasedTracker*)thiz)->getParameters();
|
||||
DetectorParams.minObjectSize = faceSize;
|
||||
((DetectionBasedTracker*)thiz)->setParameters(DetectorParams);
|
||||
}
|
||||
|
||||
if (faceSize > 0)
|
||||
{
|
||||
DetectionBasedTracker::Parameters DetectorParams = \
|
||||
((DetectionBasedTracker*)thiz)->getParameters();
|
||||
DetectorParams.minObjectSize = faceSize;
|
||||
((DetectionBasedTracker*)thiz)->setParameters(DetectorParams);
|
||||
}
|
||||
}
|
||||
catch(cv::Exception e)
|
||||
{
|
||||
LOGD("nativeStop catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
LOGD("nativeStop catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOGD("nativeSetFaceSize catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
LOGD("nativeSetFaceSize catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,23 +156,23 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDe
|
||||
{
|
||||
try
|
||||
{
|
||||
vector<Rect> RectFaces;
|
||||
((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
|
||||
((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
|
||||
vector_Rect_to_Mat(RectFaces, *((Mat*)faces));
|
||||
vector<Rect> RectFaces;
|
||||
((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
|
||||
((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
|
||||
vector_Rect_to_Mat(RectFaces, *((Mat*)faces));
|
||||
}
|
||||
catch(cv::Exception e)
|
||||
{
|
||||
LOGD("nativeCreateObject catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
LOGD("nativeCreateObject catched cv::Exception: %s", e.what());
|
||||
jclass je = jenv->FindClass("org/opencv/core/CvException");
|
||||
if(!je)
|
||||
je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOGD("nativeDetect catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
LOGD("nativeDetect catched unknown exception");
|
||||
jclass je = jenv->FindClass("java/lang/Exception");
|
||||
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
|
||||
}
|
||||
}
|
@ -5,48 +5,41 @@ import org.opencv.core.MatOfRect;
|
||||
|
||||
public class DetectionBasedTracker
|
||||
{
|
||||
public DetectionBasedTracker(String cascadeName, int minFaceSize)
|
||||
{
|
||||
mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
|
||||
}
|
||||
public DetectionBasedTracker(String cascadeName, int minFaceSize) {
|
||||
mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
nativeStart(mNativeObj);
|
||||
}
|
||||
public void start() {
|
||||
nativeStart(mNativeObj);
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
nativeStop(mNativeObj);
|
||||
}
|
||||
public void stop() {
|
||||
nativeStop(mNativeObj);
|
||||
}
|
||||
|
||||
public void setMinFaceSize(int size)
|
||||
{
|
||||
nativeSetFaceSize(mNativeObj, size);
|
||||
}
|
||||
public void setMinFaceSize(int size) {
|
||||
nativeSetFaceSize(mNativeObj, size);
|
||||
}
|
||||
|
||||
public void detect(Mat imageGray, MatOfRect faces)
|
||||
{
|
||||
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
|
||||
}
|
||||
public void detect(Mat imageGray, MatOfRect faces) {
|
||||
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
|
||||
}
|
||||
|
||||
public void release()
|
||||
{
|
||||
nativeDestroyObject(mNativeObj);
|
||||
mNativeObj = 0;
|
||||
}
|
||||
public void release() {
|
||||
nativeDestroyObject(mNativeObj);
|
||||
mNativeObj = 0;
|
||||
}
|
||||
|
||||
private long mNativeObj = 0;
|
||||
private long mNativeObj = 0;
|
||||
|
||||
private static native long nativeCreateObject(String cascadeName, int minFaceSize);
|
||||
private static native void nativeDestroyObject(long thiz);
|
||||
private static native void nativeStart(long thiz);
|
||||
private static native void nativeStop(long thiz);
|
||||
private static native void nativeSetFaceSize(long thiz, int size);
|
||||
private static native void nativeDetect(long thiz, long inputImage, long faces);
|
||||
private static native long nativeCreateObject(String cascadeName, int minFaceSize);
|
||||
private static native void nativeDestroyObject(long thiz);
|
||||
private static native void nativeStart(long thiz);
|
||||
private static native void nativeStop(long thiz);
|
||||
private static native void nativeSetFaceSize(long thiz, int size);
|
||||
private static native void nativeDetect(long thiz, long inputImage, long faces);
|
||||
|
||||
static
|
||||
{
|
||||
System.loadLibrary("detection_based_tracker");
|
||||
}
|
||||
static {
|
||||
System.loadLibrary("detection_based_tracker");
|
||||
}
|
||||
}
|
||||
|
@ -12,74 +12,72 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class FdActivity extends Activity {
|
||||
private static final String TAG = "Sample::Activity";
|
||||
private static final String TAG = "Sample-FD::Activity";
|
||||
|
||||
private MenuItem mItemFace50;
|
||||
private MenuItem mItemFace40;
|
||||
private MenuItem mItemFace30;
|
||||
private MenuItem mItemFace20;
|
||||
private MenuItem mItemType;
|
||||
|
||||
private FdView mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
|
||||
// Load native libs after OpenCV initialization
|
||||
System.loadLibrary("detection_based_tracker");
|
||||
|
||||
// Create and set View
|
||||
mView = new FdView(mAppContext);
|
||||
mView.setDetectorType(mDetectorType);
|
||||
mView.setMinFaceSize(0.2f);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
private int mDetectorType = 0;
|
||||
private String[] mDetectorName;
|
||||
private FdView mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
|
||||
// Load native libs after OpenCV initialization
|
||||
System.loadLibrary("detection_based_tracker");
|
||||
|
||||
// Create and set View
|
||||
mView = new FdView(mAppContext);
|
||||
mView.setDetectorType(mDetectorType);
|
||||
mView.setMinFaceSize(0.2f);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public FdActivity() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
@ -88,31 +86,25 @@ public class FdActivity extends Activity {
|
||||
mDetectorName[FdView.NATIVE_DETECTOR] = "Native (tracking)";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
@Override
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (mView != null)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (mView != null)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if( mView != null && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@ -120,12 +112,7 @@ public class FdActivity extends Activity {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -136,7 +123,6 @@ public class FdActivity extends Activity {
|
||||
mItemFace30 = menu.add("Face size 30%");
|
||||
mItemFace20 = menu.add("Face size 20%");
|
||||
mItemType = menu.add(mDetectorName[mDetectorType]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -146,16 +132,16 @@ public class FdActivity extends Activity {
|
||||
if (item == mItemFace50)
|
||||
mView.setMinFaceSize(0.5f);
|
||||
else if (item == mItemFace40)
|
||||
mView.setMinFaceSize(0.4f);
|
||||
mView.setMinFaceSize(0.4f);
|
||||
else if (item == mItemFace30)
|
||||
mView.setMinFaceSize(0.3f);
|
||||
mView.setMinFaceSize(0.3f);
|
||||
else if (item == mItemFace20)
|
||||
mView.setMinFaceSize(0.2f);
|
||||
mView.setMinFaceSize(0.2f);
|
||||
else if (item == mItemType)
|
||||
{
|
||||
mDetectorType = (mDetectorType + 1) % mDetectorName.length;
|
||||
item.setTitle(mDetectorName[mDetectorType]);
|
||||
mView.setDetectorType(mDetectorType);
|
||||
mDetectorType = (mDetectorType + 1) % mDetectorName.length;
|
||||
item.setTitle(mDetectorName[mDetectorType]);
|
||||
mView.setDetectorType(mDetectorType);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
class FdView extends SampleCvViewBase {
|
||||
private static final String TAG = "Sample::FdView";
|
||||
private static final String TAG = "Sample-FD::View";
|
||||
private Mat mRgba;
|
||||
private Mat mGray;
|
||||
private File mCascadeFile;
|
||||
@ -37,31 +37,25 @@ class FdView extends SampleCvViewBase {
|
||||
private int mDetectorType = JAVA_DETECTOR;
|
||||
|
||||
private float mRelativeFaceSize = 0;
|
||||
private int mAbsoluteFaceSize = 0;
|
||||
private int mAbsoluteFaceSize = 0;
|
||||
|
||||
public void setMinFaceSize(float faceSize)
|
||||
{
|
||||
mRelativeFaceSize = faceSize;
|
||||
mAbsoluteFaceSize = 0;
|
||||
public void setMinFaceSize(float faceSize) {
|
||||
mRelativeFaceSize = faceSize;
|
||||
mAbsoluteFaceSize = 0;
|
||||
}
|
||||
|
||||
public void setDetectorType(int type)
|
||||
{
|
||||
if (mDetectorType != type)
|
||||
{
|
||||
mDetectorType = type;
|
||||
public void setDetectorType(int type) {
|
||||
if (mDetectorType != type) {
|
||||
mDetectorType = type;
|
||||
|
||||
if (type == NATIVE_DETECTOR)
|
||||
{
|
||||
Log.i(TAG, "Detection Based Tracker enabled");
|
||||
mNativeDetector.start();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.i(TAG, "Cascade detector enabled");
|
||||
mNativeDetector.stop();
|
||||
}
|
||||
}
|
||||
if (type == NATIVE_DETECTOR) {
|
||||
Log.i(TAG, "Detection Based Tracker enabled");
|
||||
mNativeDetector.start();
|
||||
} else {
|
||||
Log.i(TAG, "Cascade detector enabled");
|
||||
mNativeDetector.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FdView(Context context) {
|
||||
@ -99,7 +93,7 @@ class FdView extends SampleCvViewBase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mGray = new Mat();
|
||||
@ -107,39 +101,34 @@ class FdView extends SampleCvViewBase {
|
||||
}
|
||||
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
protected Bitmap processFrame(VideoCapture capture) {
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
||||
|
||||
if (mAbsoluteFaceSize == 0)
|
||||
{
|
||||
int height = mGray.rows();
|
||||
if (Math.round(height * mRelativeFaceSize) > 0);
|
||||
{
|
||||
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
|
||||
}
|
||||
mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
|
||||
if (mAbsoluteFaceSize == 0) {
|
||||
int height = mGray.rows();
|
||||
if (Math.round(height * mRelativeFaceSize) > 0) {
|
||||
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
|
||||
}
|
||||
mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
|
||||
}
|
||||
|
||||
MatOfRect faces = new MatOfRect();
|
||||
|
||||
if (mDetectorType == JAVA_DETECTOR)
|
||||
{
|
||||
if (mJavaDetector != null)
|
||||
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE
|
||||
, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
|
||||
if (mDetectorType == JAVA_DETECTOR) {
|
||||
if (mJavaDetector != null)
|
||||
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
|
||||
new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
|
||||
}
|
||||
else if (mDetectorType == NATIVE_DETECTOR)
|
||||
{
|
||||
if (mNativeDetector != null)
|
||||
mNativeDetector.detect(mGray, faces);
|
||||
else if (mDetectorType == NATIVE_DETECTOR) {
|
||||
if (mNativeDetector != null)
|
||||
mNativeDetector.detect(mGray, faces);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.e(TAG, "Detection method is not selected!");
|
||||
else {
|
||||
Log.e(TAG, "Detection method is not selected!");
|
||||
}
|
||||
|
||||
Rect[] facesArray = faces.toArray();
|
||||
@ -149,9 +138,9 @@ class FdView extends SampleCvViewBase {
|
||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
try {
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
} catch(Exception e) {
|
||||
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
bmp.recycle();
|
||||
bmp = null;
|
||||
}
|
||||
@ -170,9 +159,9 @@ class FdView extends SampleCvViewBase {
|
||||
if (mGray != null)
|
||||
mGray.release();
|
||||
if (mCascadeFile != null)
|
||||
mCascadeFile.delete();
|
||||
mCascadeFile.delete();
|
||||
if (mNativeDetector != null)
|
||||
mNativeDetector.release();
|
||||
mNativeDetector.release();
|
||||
|
||||
mRgba = null;
|
||||
mGray = null;
|
||||
|
@ -31,24 +31,23 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
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;
|
||||
}
|
||||
}
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
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;
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,10 +12,11 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class ImageManipulationsActivity extends Activity {
|
||||
|
||||
private static final String TAG = "Sample-ImageManipulations::Activity";
|
||||
private static final String TAG = "Sample-ImageManipulations::Activity";
|
||||
|
||||
public static final int VIEW_MODE_RGBA = 0;
|
||||
public static final int VIEW_MODE_HIST = 1;
|
||||
@ -40,81 +41,75 @@ public class ImageManipulationsActivity extends Activity {
|
||||
private ImageManipulationsView mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new ImageManipulationsView(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new ImageManipulationsView(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public ImageManipulationsActivity() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if( (null != mView) && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@ -122,12 +117,7 @@ public class ImageManipulationsActivity extends Activity {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -55,7 +55,7 @@ class ImageManipulationsView extends SampleCvViewBase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mGray = new Mat();
|
||||
@ -71,11 +71,11 @@ class ImageManipulationsView extends SampleCvViewBase {
|
||||
mMat0 = new Mat();
|
||||
mColorsRGB = new Scalar[] { new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255) };
|
||||
mColorsHue = new Scalar[] {
|
||||
new Scalar(255, 0, 0, 255), new Scalar(255, 60, 0, 255), new Scalar(255, 120, 0, 255), new Scalar(255, 180, 0, 255), new Scalar(255, 240, 0, 255),
|
||||
new Scalar(215, 213, 0, 255), new Scalar(150, 255, 0, 255), new Scalar(85, 255, 0, 255), new Scalar(20, 255, 0, 255), new Scalar(0, 255, 30, 255),
|
||||
new Scalar(0, 255, 85, 255), new Scalar(0, 255, 150, 255), new Scalar(0, 255, 215, 255), new Scalar(0, 234, 255, 255), new Scalar(0, 170, 255, 255),
|
||||
new Scalar(0, 120, 255, 255), new Scalar(0, 60, 255, 255), new Scalar(0, 0, 255, 255), new Scalar(64, 0, 255, 255), new Scalar(120, 0, 255, 255),
|
||||
new Scalar(180, 0, 255, 255), new Scalar(255, 0, 255, 255), new Scalar(255, 0, 215, 255), new Scalar(255, 0, 85, 255), new Scalar(255, 0, 0, 255)
|
||||
new Scalar(255, 0, 0, 255), new Scalar(255, 60, 0, 255), new Scalar(255, 120, 0, 255), new Scalar(255, 180, 0, 255), new Scalar(255, 240, 0, 255),
|
||||
new Scalar(215, 213, 0, 255), new Scalar(150, 255, 0, 255), new Scalar(85, 255, 0, 255), new Scalar(20, 255, 0, 255), new Scalar(0, 255, 30, 255),
|
||||
new Scalar(0, 255, 85, 255), new Scalar(0, 255, 150, 255), new Scalar(0, 255, 215, 255), new Scalar(0, 234, 255, 255), new Scalar(0, 170, 255, 255),
|
||||
new Scalar(0, 120, 255, 255), new Scalar(0, 60, 255, 255), new Scalar(0, 0, 255, 255), new Scalar(64, 0, 255, 255), new Scalar(120, 0, 255, 255),
|
||||
new Scalar(180, 0, 255, 255), new Scalar(255, 0, 255, 255), new Scalar(255, 0, 215, 255), new Scalar(255, 0, 85, 255), new Scalar(255, 0, 0, 255)
|
||||
};
|
||||
mWhilte = Scalar.all(255);
|
||||
mP1 = new Point();
|
||||
@ -83,9 +83,9 @@ class ImageManipulationsView extends SampleCvViewBase {
|
||||
}
|
||||
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateAuxiliaryMats() {
|
||||
private void CreateAuxiliaryMats() {
|
||||
if (mRgba.empty())
|
||||
return;
|
||||
|
||||
@ -134,38 +134,38 @@ class ImageManipulationsView extends SampleCvViewBase {
|
||||
int offset = (int) ((mSizeRgba.width - (5*mHistSizeNum + 4*10)*thikness)/2);
|
||||
// RGB
|
||||
for(int c=0; c<3; c++) {
|
||||
Imgproc.calcHist(Arrays.asList(mRgba), mChannels[c], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (c * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsRGB[c], thikness);
|
||||
}
|
||||
Imgproc.calcHist(Arrays.asList(mRgba), mChannels[c], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (c * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsRGB[c], thikness);
|
||||
}
|
||||
}
|
||||
// Value and Hue
|
||||
Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL);
|
||||
// Value
|
||||
Imgproc.calcHist(Arrays.asList(mIntermediateMat), mChannels[2], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (3 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mWhilte, thikness);
|
||||
}
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (3 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mWhilte, thikness);
|
||||
}
|
||||
// Hue
|
||||
Imgproc.calcHist(Arrays.asList(mIntermediateMat), mChannels[0], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (4 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsHue[h], thikness);
|
||||
}
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (4 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsHue[h], thikness);
|
||||
}
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_CANNY:
|
||||
@ -231,10 +231,10 @@ class ImageManipulationsView extends SampleCvViewBase {
|
||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
try {
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
return bmp;
|
||||
} catch(Exception e) {
|
||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
bmp.recycle();
|
||||
return null;
|
||||
}
|
||||
|
@ -31,24 +31,23 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
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;
|
||||
}
|
||||
}
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
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;
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import android.view.Window;
|
||||
|
||||
public class Sample0Base extends Activity {
|
||||
|
||||
private static final String TAG = "Sample::Activity";
|
||||
private static final String TAG = "Sample::Activity";
|
||||
|
||||
private MenuItem mItemPreviewRGBA;
|
||||
private MenuItem mItemPreviewGray;
|
||||
@ -22,31 +22,31 @@ public class Sample0Base extends Activity {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
mView.releaseCamera();
|
||||
}
|
||||
super.onPause();
|
||||
mView.releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
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(AlertDialog.BUTTON_POSITIVE, "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
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
Log.i(TAG, "onCreate");
|
||||
@ -68,9 +68,9 @@ public class Sample0Base extends Activity {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
if (item == mItemPreviewRGBA)
|
||||
mView.setViewMode(Sample0View.VIEW_MODE_RGBA);
|
||||
mView.setViewMode(Sample0View.VIEW_MODE_RGBA);
|
||||
else if (item == mItemPreviewGray)
|
||||
mView.setViewMode(Sample0View.VIEW_MODE_GRAY);
|
||||
mView.setViewMode(Sample0View.VIEW_MODE_GRAY);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ import android.util.Log;
|
||||
|
||||
class Sample0View extends SampleViewBase {
|
||||
|
||||
private static final String TAG = "Sample0View";
|
||||
int mSize;
|
||||
int[] mRGBA;
|
||||
private Bitmap mBitmap;
|
||||
private static final String TAG = "Sample0View";
|
||||
int mSize;
|
||||
int[] mRGBA;
|
||||
private Bitmap mBitmap;
|
||||
private int mViewMode;
|
||||
|
||||
public static final int VIEW_MODE_RGBA = 0;
|
||||
@ -35,10 +35,10 @@ class Sample0View extends SampleViewBase {
|
||||
rgba[i] = 0xff000000 + (y << 16) + (y << 8) + y;
|
||||
}
|
||||
} else if (view_mode == VIEW_MODE_RGBA) {
|
||||
for (int i = 0; i < getFrameHeight(); i++)
|
||||
for (int i = 0; i < getFrameHeight(); i++) {
|
||||
for (int j = 0; j < getFrameWidth(); j++) {
|
||||
int index = i * getFrameWidth() + j;
|
||||
int supply_index = frameSize + (i >> 1) * getFrameWidth() + (j & ~1);
|
||||
int index = i * getFrameWidth() + j;
|
||||
int supply_index = frameSize + (i >> 1) * getFrameWidth() + (j & ~1);
|
||||
int y = (0xff & ((int) data[index]));
|
||||
int u = (0xff & ((int) data[supply_index + 0]));
|
||||
int v = (0xff & ((int) data[supply_index + 1]));
|
||||
@ -55,35 +55,36 @@ class Sample0View extends SampleViewBase {
|
||||
|
||||
rgba[i * getFrameWidth() + j] = 0xff000000 + (b << 16) + (g << 8) + r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mBitmap.setPixels(rgba, 0/* offset */, getFrameWidth() /* stride */, 0, 0, getFrameWidth(), getFrameHeight());
|
||||
return mBitmap;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||
Log.i(TAG, "onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
||||
/* Create a bitmap that will be used through to calculate the image to */
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||
Log.i(TAG, "onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
||||
/* Create a bitmap that will be used through to calculate the image to */
|
||||
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||
mRGBA = new int[previewWidth * previewHeight];
|
||||
}
|
||||
mRGBA = new int[previewWidth * previewHeight];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
Log.i(TAG, "onPreviewStopped");
|
||||
if(mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
mBitmap = null;
|
||||
}
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
Log.i(TAG, "onPreviewStopped");
|
||||
if(mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
mBitmap = null;
|
||||
}
|
||||
|
||||
if(mRGBA != null) {
|
||||
mRGBA = null;
|
||||
}
|
||||
}
|
||||
if(mRGBA != null) {
|
||||
mRGBA = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setViewMode(int viewMode) {
|
||||
Log.i(TAG, "setViewMode("+viewMode+")");
|
||||
mViewMode = viewMode;
|
||||
}
|
||||
public void setViewMode(int viewMode) {
|
||||
Log.i(TAG, "setViewMode("+viewMode+")");
|
||||
mViewMode = viewMode;
|
||||
}
|
||||
}
|
@ -46,7 +46,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
|
||||
mCamera.setPreviewTexture( new SurfaceTexture(10) );
|
||||
else
|
||||
mCamera.setPreviewDisplay(null);
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
|
||||
|
||||
@ -55,8 +55,8 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
releaseCamera();
|
||||
mCamera = Camera.open();
|
||||
if(mCamera == null) {
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
}
|
||||
|
||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||
@ -75,7 +75,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
Log.i(TAG, "releaseCamera");
|
||||
mThreadRun = false;
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
if (mCamera != null) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
@ -111,7 +111,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
List<String> FocusModes = params.getSupportedFocusModes();
|
||||
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
|
||||
{
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
}
|
||||
|
||||
mCamera.setParameters(params);
|
||||
@ -125,11 +125,11 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
mFrame = new byte [size];
|
||||
mCamera.addCallbackBuffer(mBuffer);
|
||||
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
|
||||
/* Notify that the preview is about to be started and deliver preview size */
|
||||
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);
|
||||
|
@ -12,6 +12,7 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample1Java extends Activity {
|
||||
private static final String TAG = "Sample::Activity";
|
||||
@ -22,81 +23,76 @@ public class Sample1Java extends Activity {
|
||||
private Sample1View mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new Sample1View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new Sample1View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public Sample1Java() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if( (null != mView) && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@ -104,12 +100,7 @@ public class Sample1Java extends Activity {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -125,11 +116,11 @@ public class Sample1Java extends Activity {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
if (item == mItemPreviewRGBA) {
|
||||
mView.setViewMode(Sample1View.VIEW_MODE_RGBA);
|
||||
mView.setViewMode(Sample1View.VIEW_MODE_RGBA);
|
||||
} else if (item == mItemPreviewGray) {
|
||||
mView.setViewMode(Sample1View.VIEW_MODE_GRAY);
|
||||
mView.setViewMode(Sample1View.VIEW_MODE_GRAY);
|
||||
} else if (item == mItemPreviewCanny) {
|
||||
mView.setViewMode(Sample1View.VIEW_MODE_CANNY);
|
||||
mView.setViewMode(Sample1View.VIEW_MODE_CANNY);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -22,35 +22,35 @@ class Sample1View extends SampleViewBase {
|
||||
private Mat mRgba;
|
||||
private Mat mGraySubmat;
|
||||
private Mat mIntermediateMat;
|
||||
private Bitmap mBitmap;
|
||||
private int mViewMode;
|
||||
private Bitmap mBitmap;
|
||||
private int mViewMode;
|
||||
|
||||
public Sample1View(Context context) {
|
||||
super(context);
|
||||
mViewMode = VIEW_MODE_RGBA;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||
|
||||
mRgba = new Mat();
|
||||
mIntermediateMat = new Mat();
|
||||
mRgba = new Mat();
|
||||
mIntermediateMat = new Mat();
|
||||
|
||||
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
}
|
||||
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
if(mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
}
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
if(mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
synchronized (this) {
|
||||
// Explicitly deallocate Mats
|
||||
if (mYuv != null)
|
||||
mYuv.release();
|
||||
@ -101,7 +101,7 @@ class Sample1View extends SampleViewBase {
|
||||
}
|
||||
|
||||
public void setViewMode(int viewMode) {
|
||||
mViewMode = viewMode;
|
||||
mViewMode = viewMode;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -46,16 +46,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
|
||||
mCamera.setPreviewTexture( new SurfaceTexture(10) );
|
||||
else
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
|
||||
public boolean openCamera() {
|
||||
Log.i(TAG, "openCamera");
|
||||
releaseCamera();
|
||||
mCamera = Camera.open();
|
||||
if(mCamera == null) {
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
}
|
||||
|
||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||
@ -74,7 +74,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
Log.i(TAG, "releaseCamera");
|
||||
mThreadRun = false;
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
if (mCamera != null) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
@ -110,7 +110,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
List<String> FocusModes = params.getSupportedFocusModes();
|
||||
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
|
||||
{
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
}
|
||||
|
||||
mCamera.setParameters(params);
|
||||
@ -124,11 +124,11 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
mFrame = new byte [size];
|
||||
mCamera.addCallbackBuffer(mBuffer);
|
||||
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
|
||||
/* Notify that the preview is about to be started and deliver preview size */
|
||||
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);
|
||||
|
@ -12,6 +12,7 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample2NativeCamera extends Activity {
|
||||
private static final String TAG = "Sample::Activity";
|
||||
@ -26,84 +27,78 @@ public class Sample2NativeCamera extends Activity {
|
||||
|
||||
public static int viewMode = VIEW_MODE_RGBA;
|
||||
|
||||
private Sample2View mView;
|
||||
private Sample2View mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new Sample2View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new Sample2View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public Sample2NativeCamera() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if((null != mView) && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@ -111,11 +106,7 @@ public class Sample2NativeCamera extends Activity {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,16 +50,16 @@ class Sample2View extends SampleCvViewBase {
|
||||
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
||||
Imgproc.Canny(mGray, mIntermediateMat, 80, 100);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
try {
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
return bmp;
|
||||
} catch(Exception e) {
|
||||
Log.e("org.opencv.samples.tutorial2", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
Log.e("org.opencv.samples.tutorial2", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
bmp.recycle();
|
||||
return null;
|
||||
}
|
||||
|
@ -29,24 +29,23 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
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;
|
||||
}
|
||||
}
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
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;
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -115,8 +114,8 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
if (bmp != null) {
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
if (canvas != null) {
|
||||
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth()-bmp.getWidth()) / 2, (canvas.getHeight()-bmp.getHeight()) / 2, null);
|
||||
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth()-bmp.getWidth()) / 2, (canvas.getHeight()-bmp.getHeight()) / 2, null);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
bmp.recycle();
|
||||
|
@ -10,91 +10,86 @@ import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample3Native extends Activity {
|
||||
private static final String TAG = "Sample::Activity";
|
||||
private Sample3View mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
|
||||
// Load native library after(!) OpenCV initialization
|
||||
System.loadLibrary("native_sample");
|
||||
// Load native library after(!) OpenCV initialization
|
||||
System.loadLibrary("native_sample");
|
||||
|
||||
// Create and set View
|
||||
mView = new Sample3View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
// Create and set View
|
||||
mView = new Sample3View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public Sample3Native() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if((null != mView) && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@ -102,10 +97,6 @@ public class Sample3Native extends Activity {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
}
|
||||
|
@ -5,29 +5,29 @@ import android.graphics.Bitmap;
|
||||
|
||||
class Sample3View extends SampleViewBase {
|
||||
|
||||
private int mFrameSize;
|
||||
private Bitmap mBitmap;
|
||||
private int[] mRGBA;
|
||||
private int mFrameSize;
|
||||
private Bitmap mBitmap;
|
||||
private int[] mRGBA;
|
||||
|
||||
public Sample3View(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
|
||||
mFrameSize = previewWidtd * previewHeight;
|
||||
mRGBA = new int[mFrameSize];
|
||||
mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
|
||||
mFrameSize = previewWidtd * previewHeight;
|
||||
mRGBA = new int[mFrameSize];
|
||||
mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
if(mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
mBitmap = null;
|
||||
}
|
||||
mRGBA = null;
|
||||
}
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
if(mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
mBitmap = null;
|
||||
}
|
||||
mRGBA = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Bitmap processFrame(byte[] data) {
|
||||
|
@ -45,16 +45,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
|
||||
mCamera.setPreviewTexture( new SurfaceTexture(10) );
|
||||
else
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
|
||||
public boolean openCamera() {
|
||||
Log.i(TAG, "openCamera");
|
||||
releaseCamera();
|
||||
mCamera = Camera.open();
|
||||
if(mCamera == null) {
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
}
|
||||
|
||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||
@ -73,7 +73,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
Log.i(TAG, "releaseCamera");
|
||||
mThreadRun = false;
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
if (mCamera != null) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
@ -109,7 +109,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
List<String> FocusModes = params.getSupportedFocusModes();
|
||||
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
|
||||
{
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
}
|
||||
|
||||
mCamera.setParameters(params);
|
||||
@ -123,11 +123,11 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
mFrame = new byte [size];
|
||||
mCamera.addCallbackBuffer(mBuffer);
|
||||
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
|
||||
/* Notify that the preview is about to be started and deliver preview size */
|
||||
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);
|
||||
|
@ -12,6 +12,7 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class Sample4Mixed extends Activity {
|
||||
private static final String TAG = "Sample::Activity";
|
||||
@ -23,100 +24,88 @@ public class Sample4Mixed extends Activity {
|
||||
private Sample4View mView;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
|
||||
// Load native library after(!) OpenCV initialization
|
||||
System.loadLibrary("mixed_sample");
|
||||
// Load native library after(!) OpenCV initialization
|
||||
System.loadLibrary("mixed_sample");
|
||||
|
||||
// Create and set View
|
||||
mView = new Sample4View(mAppContext);
|
||||
setContentView(mView);
|
||||
// Create and set View
|
||||
mView = new Sample4View(mAppContext);
|
||||
setContentView(mView);
|
||||
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mAppContext.finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
super.onManagerConnected(status);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public Sample4Mixed() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
super.onPause();
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
}
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
super.onResume();
|
||||
if((null != mView) && !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(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
}
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Log.i(TAG, "onCreate");
|
||||
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
@ -131,13 +120,13 @@ public class Sample4Mixed extends Activity {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
if (item == mItemPreviewRGBA) {
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_RGBA);
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_RGBA);
|
||||
} else if (item == mItemPreviewGray) {
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_GRAY);
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_GRAY);
|
||||
} else if (item == mItemPreviewCanny) {
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_CANNY);
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_CANNY);
|
||||
} else if (item == mItemPreviewFeatures) {
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_FEATURES);
|
||||
mView.setViewMode(Sample4View.VIEW_MODE_FEATURES);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -22,14 +22,14 @@ class Sample4View extends SampleViewBase {
|
||||
private Mat mIntermediateMat;
|
||||
|
||||
private int mViewMode;
|
||||
private Bitmap mBitmap;
|
||||
private Bitmap mBitmap;
|
||||
|
||||
public Sample4View(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
|
||||
@Override
|
||||
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
|
||||
// initialize Mats before usage
|
||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||
@ -38,15 +38,15 @@ class Sample4View extends SampleViewBase {
|
||||
mIntermediateMat = new Mat();
|
||||
|
||||
mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
@Override
|
||||
protected void onPreviewStopped() {
|
||||
|
||||
if (mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
mBitmap = null;
|
||||
}
|
||||
if (mBitmap != null) {
|
||||
mBitmap.recycle();
|
||||
mBitmap = null;
|
||||
}
|
||||
|
||||
// Explicitly deallocate Mats
|
||||
if (mYuv != null)
|
||||
@ -63,7 +63,7 @@ class Sample4View extends SampleViewBase {
|
||||
mGraySubmat = null;
|
||||
mIntermediateMat = null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@ -105,6 +105,6 @@ class Sample4View extends SampleViewBase {
|
||||
public native void FindFeatures(long matAddrGr, long matAddrRgba);
|
||||
|
||||
public void setViewMode(int viewMode) {
|
||||
mViewMode = viewMode;
|
||||
mViewMode = viewMode;
|
||||
}
|
||||
}
|
||||
|
@ -45,16 +45,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
|
||||
mCamera.setPreviewTexture( new SurfaceTexture(10) );
|
||||
else
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
mCamera.setPreviewDisplay(null);
|
||||
}
|
||||
|
||||
public boolean openCamera() {
|
||||
Log.i(TAG, "openCamera");
|
||||
releaseCamera();
|
||||
mCamera = Camera.open();
|
||||
if(mCamera == null) {
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
Log.e(TAG, "Can't open camera!");
|
||||
return false;
|
||||
}
|
||||
|
||||
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
|
||||
@ -73,7 +73,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
Log.i(TAG, "releaseCamera");
|
||||
mThreadRun = false;
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
if (mCamera != null) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
@ -109,7 +109,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
List<String> FocusModes = params.getSupportedFocusModes();
|
||||
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
|
||||
{
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
||||
}
|
||||
|
||||
mCamera.setParameters(params);
|
||||
@ -123,11 +123,11 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
mFrame = new byte [size];
|
||||
mCamera.addCallbackBuffer(mBuffer);
|
||||
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
try {
|
||||
setPreview();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
|
||||
}
|
||||
|
||||
/* Notify that the preview is about to be started and deliver preview size */
|
||||
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);
|
||||
|
Loading…
Reference in New Issue
Block a user