Commit 09901fae authored by Alexander Smorkalov's avatar Alexander Smorkalov Committed by Andrey Kamaev

OnResume/OnPause handlers added to start/stop camera.

OpenCV initialization moved to onResume event to support package installation waiting.
parent cc3430db
...@@ -26,8 +26,8 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, ...@@ -26,8 +26,8 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
private int mGameWidth; private int mGameWidth;
private int mGameHeight; private int mGameHeight;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override @Override
public void onManagerConnected(int status) { public void onManagerConnected(int status) {
switch (status) { switch (status) {
...@@ -35,8 +35,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, ...@@ -35,8 +35,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
{ {
Log.i(TAG, "OpenCV loaded successfully"); Log.i(TAG, "OpenCV loaded successfully");
mPuzzle15 = new Puzzle15Processor();
mPuzzle15.prepareNewGame();
/* Now enable camera view to start receiving frames */ /* Now enable camera view to start receiving frames */
mOpenCvCameraView.setOnTouchListener(Puzzle15Activity.this); mOpenCvCameraView.setOnTouchListener(Puzzle15Activity.this);
mOpenCvCameraView.enableView(); mOpenCvCameraView.enableView();
...@@ -49,9 +47,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, ...@@ -49,9 +47,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
} }
}; };
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -61,6 +56,21 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, ...@@ -61,6 +56,21 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
mOpenCvCameraView = (OpenCvNativeCameraView) findViewById(R.id.puzzle_activity_surface_view); mOpenCvCameraView = (OpenCvNativeCameraView) findViewById(R.id.puzzle_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this); mOpenCvCameraView.setCvCameraViewListener(this);
mPuzzle15 = new Puzzle15Processor();
mPuzzle15.prepareNewGame();
}
@Override
public void onPause()
{
mOpenCvCameraView.disableView();
super.onPause();
}
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback);
} }
...@@ -88,7 +98,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, ...@@ -88,7 +98,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
return true; return true;
} }
@Override @Override
public void onCameraViewStarted(int width, int height) { public void onCameraViewStarted(int width, int height) {
mGameWidth = width; mGameWidth = width;
...@@ -120,8 +129,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, ...@@ -120,8 +129,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
mPuzzle15.deliverTouchEvent(xpos, ypos); mPuzzle15.deliverTouchEvent(xpos, ypos);
} }
// TODO Auto-generated method stub
return false; return false;
} }
} }
...@@ -25,7 +25,6 @@ public class Puzzle15Processor { ...@@ -25,7 +25,6 @@ public class Puzzle15Processor {
private int[] mTextWidths; private int[] mTextWidths;
private int[] mTextHeights; private int[] mTextHeights;
private Mat mRgba;
private Mat mRgba15; private Mat mRgba15;
private Mat[] mCells; private Mat[] mCells;
...@@ -38,14 +37,9 @@ public class Puzzle15Processor { ...@@ -38,14 +37,9 @@ public class Puzzle15Processor {
mIndexes = new int [GRID_AREA]; mIndexes = new int [GRID_AREA];
for (int i = 0; i < GRID_AREA; i++) { for (int i = 0; i < GRID_AREA; i++)
Size s = Core.getTextSize(Integer.toString(i + 1), 3/* CV_FONT_HERSHEY_COMPLEX */, 1, 2, null);
mTextHeights[i] = (int) s.height;
mTextWidths[i] = (int) s.width;
mIndexes[i] = i; mIndexes[i] = i;
} }
}
/* this method is intended to make processor prepared for a new game */ /* this method is intended to make processor prepared for a new game */
public synchronized void prepareNewGame() { public synchronized void prepareNewGame() {
...@@ -67,14 +61,16 @@ public class Puzzle15Processor { ...@@ -67,14 +61,16 @@ public class Puzzle15Processor {
for (int i = 0; i < GRID_SIZE; i++) { for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) { for (int j = 0; j < GRID_SIZE; j++) {
int k = i * GRID_SIZE + j; int k = i * GRID_SIZE + j;
// mCells[k] = mRgba.submat(i * height / GRID_SIZE, (i + 1) * height / GRID_SIZE, j * width / GRID_SIZE, (j + 1) * width / GRID_SIZE);
mCells15[k] = mRgba15.submat(i * height / GRID_SIZE, (i + 1) * height / GRID_SIZE, j * width / GRID_SIZE, (j + 1) * width / GRID_SIZE); mCells15[k] = mRgba15.submat(i * height / GRID_SIZE, (i + 1) * height / GRID_SIZE, j * width / GRID_SIZE, (j + 1) * width / GRID_SIZE);
} }
} }
for (int i = 0; i < GRID_AREA; i++) {
Size s = Core.getTextSize(Integer.toString(i + 1), 3/* CV_FONT_HERSHEY_COMPLEX */, 1, 2, null);
mTextHeights[i] = (int) s.height;
mTextWidths[i] = (int) s.width;
}
} }
/* this method to be called from the outside. it processes the frame and shuffles /* this method to be called from the outside. it processes the frame and shuffles
* the tiles as specified by mIndexes array * the tiles as specified by mIndexes array
...@@ -83,12 +79,9 @@ public class Puzzle15Processor { ...@@ -83,12 +79,9 @@ public class Puzzle15Processor {
int rows = inputPicture.rows(); int rows = inputPicture.rows();
int cols = inputPicture.cols(); int cols = inputPicture.cols();
int type = inputPicture.type();
rows = rows - rows%4; rows = rows - rows%4;
cols = cols - cols%4; cols = cols - cols%4;
for (int i = 0; i < GRID_SIZE; i++) { for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) { for (int j = 0; j < GRID_SIZE; j++) {
int k = i * GRID_SIZE + j; int k = i * GRID_SIZE + j;
...@@ -118,7 +111,6 @@ public class Puzzle15Processor { ...@@ -118,7 +111,6 @@ public class Puzzle15Processor {
return mRgba15; return mRgba15;
} }
public void toggleTileNumbers() { public void toggleTileNumbers() {
mShowTileNumbers = !mShowTileNumbers; mShowTileNumbers = !mShowTileNumbers;
} }
...@@ -172,7 +164,6 @@ public class Puzzle15Processor { ...@@ -172,7 +164,6 @@ public class Puzzle15Processor {
} }
} }
private static void shuffle(int[] array) { private static void shuffle(int[] array) {
for (int i = array.length; i > 1; i--) { for (int i = array.length; i > 1; i--) {
int temp = array[i - 1]; int temp = array[i - 1];
...@@ -199,5 +190,4 @@ public class Puzzle15Processor { ...@@ -199,5 +190,4 @@ public class Puzzle15Processor {
} }
return sum % 2 == 0; return sum % 2 == 0;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment