OnResume/OnPause handlers added to start/stop camera.
OpenCV initialization moved to onResume event to support package installation waiting.
This commit is contained in:
parent
cc3430dbcc
commit
09901fae2b
@ -18,39 +18,34 @@ import android.view.View;
|
|||||||
|
|
||||||
public class Puzzle15Activity extends Activity implements CvCameraViewListener, View.OnTouchListener {
|
public class Puzzle15Activity extends Activity implements CvCameraViewListener, View.OnTouchListener {
|
||||||
|
|
||||||
private static final String TAG = "Sample::Puzzle15::Activity";
|
private static final String TAG = "Sample::Puzzle15::Activity";
|
||||||
|
|
||||||
private OpenCvNativeCameraView mOpenCvCameraView;
|
private OpenCvNativeCameraView mOpenCvCameraView;
|
||||||
private Puzzle15Processor mPuzzle15;
|
private Puzzle15Processor mPuzzle15;
|
||||||
|
|
||||||
private int mGameWidth;
|
private int mGameWidth;
|
||||||
private int mGameHeight;
|
private int mGameHeight;
|
||||||
|
|
||||||
|
|
||||||
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
|
||||||
@Override
|
|
||||||
public void onManagerConnected(int status) {
|
|
||||||
switch (status) {
|
|
||||||
case LoaderCallbackInterface.SUCCESS:
|
|
||||||
{
|
|
||||||
Log.i(TAG, "OpenCV loaded successfully");
|
|
||||||
|
|
||||||
mPuzzle15 = new Puzzle15Processor();
|
|
||||||
mPuzzle15.prepareNewGame();
|
|
||||||
/* Now enable camera view to start receiving frames */
|
|
||||||
mOpenCvCameraView.setOnTouchListener(Puzzle15Activity.this);
|
|
||||||
mOpenCvCameraView.enableView();
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
super.onManagerConnected(status);
|
|
||||||
} break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onManagerConnected(int status) {
|
||||||
|
switch (status) {
|
||||||
|
case LoaderCallbackInterface.SUCCESS:
|
||||||
|
{
|
||||||
|
Log.i(TAG, "OpenCV loaded successfully");
|
||||||
|
|
||||||
|
/* Now enable camera view to start receiving frames */
|
||||||
|
mOpenCvCameraView.setOnTouchListener(Puzzle15Activity.this);
|
||||||
|
mOpenCvCameraView.enableView();
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
super.onManagerConnected(status);
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
@ -61,12 +56,27 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
mOpenCvCameraView.disableView();
|
mOpenCvCameraView.disableView();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -79,49 +89,46 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
|
|||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "Menu Item selected " + item);
|
||||||
if (item.getItemId() == R.id.menu_start_new_game) {
|
if (item.getItemId() == R.id.menu_start_new_game) {
|
||||||
/* We need to start new game */
|
/* We need to start new game */
|
||||||
mPuzzle15.prepareNewGame();
|
mPuzzle15.prepareNewGame();
|
||||||
} else if (item.getItemId() == R.id.menu_toggle_tile_numbers) {
|
} else if (item.getItemId() == R.id.menu_toggle_tile_numbers) {
|
||||||
/* We need to enable or disable drawing of the tile numbers */
|
/* We need to enable or disable drawing of the tile numbers */
|
||||||
mPuzzle15.toggleTileNumbers();
|
mPuzzle15.toggleTileNumbers();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCameraViewStarted(int width, int height) {
|
||||||
|
mGameWidth = width;
|
||||||
|
mGameHeight = height;
|
||||||
|
mPuzzle15.prepareGameSize(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCameraViewStarted(int width, int height) {
|
public void onCameraViewStopped() {
|
||||||
mGameWidth = width;
|
}
|
||||||
mGameHeight = height;
|
|
||||||
mPuzzle15.prepareGameSize(width, height);
|
@Override
|
||||||
}
|
public Mat onCameraFrame(Mat inputFrame) {
|
||||||
|
return mPuzzle15.puzzleFrame(inputFrame);
|
||||||
@Override
|
}
|
||||||
public void onCameraViewStopped() {
|
|
||||||
}
|
@Override
|
||||||
|
public boolean onTouch(View view, MotionEvent event) {
|
||||||
@Override
|
int xpos, ypos;
|
||||||
public Mat onCameraFrame(Mat inputFrame) {
|
|
||||||
return mPuzzle15.puzzleFrame(inputFrame);
|
xpos = (view.getWidth() - mGameWidth) / 2;
|
||||||
}
|
xpos = (int)event.getX() - xpos;
|
||||||
|
|
||||||
@Override
|
ypos = (view.getHeight() - mGameHeight) / 2;
|
||||||
public boolean onTouch(View view, MotionEvent event) {
|
ypos = (int)event.getY() - ypos;
|
||||||
int xpos, ypos;
|
|
||||||
|
if (xpos >=0 && xpos <= mGameWidth && ypos >=0 && ypos <= mGameHeight) {
|
||||||
xpos = (view.getWidth() - mGameWidth) / 2;
|
/* click is inside the picture. Deliver this event to processor */
|
||||||
xpos = (int)event.getX() - xpos;
|
mPuzzle15.deliverTouchEvent(xpos, ypos);
|
||||||
|
|
||||||
ypos = (view.getHeight() - mGameHeight) / 2;
|
|
||||||
ypos = (int)event.getY() - ypos;
|
|
||||||
|
|
||||||
if (xpos >=0 && xpos <= mGameWidth && ypos >=0 && ypos <= mGameHeight) {
|
|
||||||
/* click is inside the picture. Deliver this event to processor */
|
|
||||||
mPuzzle15.deliverTouchEvent(xpos, ypos);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,18 +19,17 @@ public class Puzzle15Processor {
|
|||||||
private static final int GRID_SIZE = 4;
|
private static final int GRID_SIZE = 4;
|
||||||
private static final int GRID_AREA = GRID_SIZE * GRID_SIZE;
|
private static final int GRID_AREA = GRID_SIZE * GRID_SIZE;
|
||||||
private static final int GRID_EMPTY_INDEX = GRID_AREA - 1;
|
private static final int GRID_EMPTY_INDEX = GRID_AREA - 1;
|
||||||
private static final String TAG = "Puzzle15Processor";
|
private static final String TAG = "Puzzle15Processor";
|
||||||
|
|
||||||
private int[] mIndexes;
|
private int[] mIndexes;
|
||||||
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;
|
||||||
private Mat[] mCells15;
|
private Mat[] mCells15;
|
||||||
private boolean mShowTileNumbers = true;
|
private boolean mShowTileNumbers = true;
|
||||||
|
|
||||||
public Puzzle15Processor() {
|
public Puzzle15Processor() {
|
||||||
mTextWidths = new int[GRID_AREA];
|
mTextWidths = new int[GRID_AREA];
|
||||||
@ -38,13 +37,8 @@ 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 */
|
||||||
@ -67,34 +61,33 @@ 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
|
||||||
*/
|
*/
|
||||||
public synchronized Mat puzzleFrame(Mat inputPicture) {
|
public synchronized Mat puzzleFrame(Mat inputPicture) {
|
||||||
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;
|
||||||
mCells[k] = inputPicture.submat(i * inputPicture.rows() / GRID_SIZE, (i + 1) * inputPicture.rows() / GRID_SIZE, j * inputPicture.cols()/ GRID_SIZE, (j + 1) * inputPicture.cols() / GRID_SIZE);
|
mCells[k] = inputPicture.submat(i * inputPicture.rows() / GRID_SIZE, (i + 1) * inputPicture.rows() / GRID_SIZE, j * inputPicture.cols()/ GRID_SIZE, (j + 1) * inputPicture.cols() / GRID_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rows = rows - rows%4;
|
rows = rows - rows%4;
|
||||||
cols = cols - cols%4;
|
cols = cols - cols%4;
|
||||||
@ -115,23 +108,22 @@ public class Puzzle15Processor {
|
|||||||
|
|
||||||
drawGrid(cols, rows, mRgba15);
|
drawGrid(cols, rows, mRgba15);
|
||||||
|
|
||||||
return mRgba15;
|
return mRgba15;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void toggleTileNumbers() {
|
public void toggleTileNumbers() {
|
||||||
mShowTileNumbers = !mShowTileNumbers;
|
mShowTileNumbers = !mShowTileNumbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deliverTouchEvent(int x, int y) {
|
public void deliverTouchEvent(int x, int y) {
|
||||||
int rows = mRgba15.rows();
|
int rows = mRgba15.rows();
|
||||||
int cols = mRgba15.cols();
|
int cols = mRgba15.cols();
|
||||||
|
|
||||||
int row = (int) Math.floor(y * GRID_SIZE / rows);
|
int row = (int) Math.floor(y * GRID_SIZE / rows);
|
||||||
int col = (int) Math.floor(x * GRID_SIZE / cols);
|
int col = (int) Math.floor(x * GRID_SIZE / cols);
|
||||||
|
|
||||||
if (row < 0 || row >= GRID_SIZE || col < 0 || col >= GRID_SIZE) {
|
if (row < 0 || row >= GRID_SIZE || col < 0 || col >= GRID_SIZE) {
|
||||||
Log.e(TAG, "It is not expected to get touch event outside of picture");
|
Log.e(TAG, "It is not expected to get touch event outside of picture");
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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 {
|
|||||||
}
|
}
|
||||||
return sum % 2 == 0;
|
return sum % 2 == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user