mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 08:13:04 +04:00
Fix Andoid samples: black screen after resume problem
This commit is contained in:
@@ -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,28 +29,27 @@ 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+")");
|
||||
synchronized (this) {
|
||||
@@ -77,7 +76,7 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
Log.i(TAG, "surfaceChanged");
|
||||
setupCamera(width, height);
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
@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++) {
|
||||
@@ -117,12 +117,16 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
int cols = mRgba.cols();
|
||||
int rows = mRgba.rows();
|
||||
|
||||
rows = rows - rows%4;
|
||||
cols = cols - cols%4;
|
||||
|
||||
|
||||
rows = rows - rows%4;
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user