mirror of https://github.com/opencv/opencv.git
* Added manual port for getTextSize; * Fixed bugs in "native camera" sample; * Added 15-puzzle sample.pull/13383/head
parent
ae5dd1d748
commit
be2c4ddbdd
13 changed files with 560 additions and 31 deletions
@ -0,0 +1,8 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<classpath> |
||||
<classpathentry kind="src" path="src"/> |
||||
<classpathentry kind="src" path="gen"/> |
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> |
||||
<classpathentry kind="src" path="OpenCV-2.3.1_src"/> |
||||
<classpathentry kind="output" path="bin"/> |
||||
</classpath> |
@ -0,0 +1,40 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<projectDescription> |
||||
<name>15-puzzle</name> |
||||
<comment></comment> |
||||
<projects> |
||||
</projects> |
||||
<buildSpec> |
||||
<buildCommand> |
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>org.eclipse.jdt.core.javabuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
</buildSpec> |
||||
<natures> |
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature> |
||||
<nature>org.eclipse.jdt.core.javanature</nature> |
||||
</natures> |
||||
<linkedResources> |
||||
<link> |
||||
<name>OpenCV-2.3.1_src</name> |
||||
<type>2</type> |
||||
<locationURI>_android_OpenCV_2_3_1_df28900a/src</locationURI> |
||||
</link> |
||||
</linkedResources> |
||||
</projectDescription> |
@ -0,0 +1,5 @@ |
||||
#Wed Jun 29 04:36:40 MSD 2011 |
||||
eclipse.preferences.version=1 |
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 |
||||
org.eclipse.jdt.core.compiler.compliance=1.5 |
||||
org.eclipse.jdt.core.compiler.source=1.5 |
@ -0,0 +1,24 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
||||
package="org.opencv.samples.puzzle15" |
||||
android:versionCode="1" |
||||
android:versionName="1.0"> |
||||
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name"> |
||||
<activity android:name=".puzzle15Activity" |
||||
android:label="@string/app_name" |
||||
android:screenOrientation="landscape" |
||||
android:configChanges="keyboardHidden|orientation"> |
||||
<intent-filter> |
||||
<action android:name="android.intent.action.MAIN" /> |
||||
<category android:name="android.intent.category.LAUNCHER" /> |
||||
</intent-filter> |
||||
</activity> |
||||
</application> |
||||
|
||||
<uses-sdk android:minSdkVersion="8" /> |
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/> |
||||
<uses-feature android:name="android.hardware.camera" /> |
||||
<uses-feature android:name="android.hardware.camera.autofocus" /> |
||||
</manifest> |
@ -0,0 +1,3 @@ |
||||
android.library.reference.1=../../../android/build |
||||
# Project target. |
||||
target=android-8 |
After Width: | Height: | Size: 5.6 KiB |
@ -0,0 +1,4 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<string name="app_name">15-puzzle</string> |
||||
</resources> |
@ -0,0 +1,108 @@ |
||||
package org.opencv.samples.puzzle15; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.opencv.*; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Bitmap; |
||||
import android.graphics.Canvas; |
||||
import android.util.Log; |
||||
import android.view.SurfaceHolder; |
||||
import android.view.SurfaceView; |
||||
|
||||
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable { |
||||
private static final String TAG = "Sample::SurfaceView"; |
||||
|
||||
private SurfaceHolder mHolder; |
||||
private VideoCapture mCamera; |
||||
|
||||
public SampleViewBase(Context context) { |
||||
super(context); |
||||
mHolder = getHolder(); |
||||
mHolder.addCallback(this); |
||||
Log.i(TAG, "Instantiated new " + this.getClass()); |
||||
} |
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { |
||||
Log.i(TAG, "surfaceCreated"); |
||||
synchronized (this) { |
||||
if (mCamera != null && mCamera.isOpened()) { |
||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()"); |
||||
List<Size> sizes = mCamera.getSupportedPreviewSizes(); |
||||
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()"); |
||||
int mFrameWidth = width; |
||||
int mFrameHeight = height; |
||||
|
||||
// selecting optimal camera preview size
|
||||
{ |
||||
double minDiff = Double.MAX_VALUE; |
||||
for (Size size : sizes) { |
||||
if (Math.abs(size.height - height) < minDiff) { |
||||
mFrameWidth = (int) size.width; |
||||
mFrameHeight = (int) size.height; |
||||
minDiff = Math.abs(size.height - height); |
||||
} |
||||
} |
||||
} |
||||
|
||||
mCamera.set(highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth); |
||||
mCamera.set(highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) { |
||||
Log.i(TAG, "surfaceCreated"); |
||||
mCamera = new VideoCapture(highgui.CV_CAP_ANDROID); |
||||
if (mCamera.isOpened()) { |
||||
(new Thread(this)).start(); |
||||
} else { |
||||
mCamera.release(); |
||||
mCamera = null; |
||||
Log.e(TAG, "Failed to open native camera"); |
||||
} |
||||
} |
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) { |
||||
Log.i(TAG, "surfaceDestroyed"); |
||||
if (mCamera != null) { |
||||
synchronized (this) { |
||||
mCamera.release(); |
||||
mCamera = null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected abstract Bitmap processFrame(VideoCapture capture); |
||||
|
||||
public void run() { |
||||
Log.i(TAG, "Starting processing thread"); |
||||
while (true) { |
||||
Bitmap bmp = null; |
||||
|
||||
synchronized (this) { |
||||
if (mCamera == null) |
||||
break; |
||||
|
||||
if (!mCamera.grab()) { |
||||
Log.e(TAG, "mCamera.grab() failed"); |
||||
break; |
||||
} |
||||
|
||||
bmp = processFrame(mCamera); |
||||
} |
||||
|
||||
if (bmp != null) { |
||||
Canvas canvas = mHolder.lockCanvas(); |
||||
if (canvas != null) { |
||||
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null); |
||||
mHolder.unlockCanvasAndPost(canvas); |
||||
} |
||||
bmp.recycle(); |
||||
} |
||||
} |
||||
|
||||
Log.i(TAG, "Finishing processing thread"); |
||||
} |
||||
} |
@ -0,0 +1,50 @@ |
||||
package org.opencv.samples.puzzle15; |
||||
|
||||
import android.app.Activity; |
||||
import android.os.Bundle; |
||||
import android.util.Log; |
||||
import android.view.Menu; |
||||
import android.view.MenuItem; |
||||
import android.view.Window; |
||||
|
||||
public class puzzle15Activity extends Activity { |
||||
private static final String TAG = "Sample::Activity"; |
||||
|
||||
private MenuItem mItemNewGame; |
||||
private MenuItem mItemToggleNumbers; |
||||
private puzzle15View mView; |
||||
|
||||
public puzzle15Activity() { |
||||
Log.i(TAG, "Instantiated new " + this.getClass()); |
||||
} |
||||
|
||||
/** 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); |
||||
mView = new puzzle15View(this); |
||||
setContentView(mView); |
||||
} |
||||
|
||||
@Override |
||||
public boolean onCreateOptionsMenu(Menu menu) { |
||||
Log.i(TAG, "onCreateOptionsMenu"); |
||||
mItemNewGame = menu.add("Start new game"); |
||||
mItemToggleNumbers = menu.add("Show/hide tile numbers"); |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean onOptionsItemSelected(MenuItem item) { |
||||
Log.i(TAG, "Menu Item selected " + item); |
||||
if (item == mItemNewGame) { |
||||
synchronized (mView) { |
||||
mView.startNewGame(); |
||||
} |
||||
} else if (item == mItemToggleNumbers) |
||||
mView.tolggleTileNumbers(); |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,221 @@ |
||||
package org.opencv.samples.puzzle15; |
||||
|
||||
import org.opencv.*; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Bitmap; |
||||
import android.view.MotionEvent; |
||||
import android.view.SurfaceHolder; |
||||
import android.view.View; |
||||
import android.view.View.OnTouchListener; |
||||
|
||||
public class puzzle15View extends SampleViewBase implements OnTouchListener { |
||||
private Mat mRgba; |
||||
private Mat mRgba15; |
||||
private Mat[] mCells; |
||||
private Mat[] mCells15; |
||||
private int[] mIndexses; |
||||
private int[] mTextWidths; |
||||
private int[] mTextHeights; |
||||
private boolean mShowTileNumbers = true; |
||||
|
||||
int gridSize = 4; |
||||
int gridArea = gridSize * gridSize; |
||||
int gridEmptyIdx = gridArea - 1; |
||||
|
||||
public puzzle15View(Context context) { |
||||
super(context); |
||||
setOnTouchListener(this); |
||||
|
||||
mTextWidths = new int[gridArea]; |
||||
mTextHeights = new int[gridArea]; |
||||
for (int i = 0; i < gridArea; 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; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { |
||||
super.surfaceChanged(_holder, format, width, height); |
||||
synchronized (this) { |
||||
// initialize Mat before usage
|
||||
mRgba = new Mat(); |
||||
} |
||||
} |
||||
|
||||
public static void shuffle(int[] array) { |
||||
for (int i = array.length; i > 1; i--) { |
||||
int temp = array[i - 1]; |
||||
int randIx = (int) (Math.random() * i); |
||||
array[i - 1] = array[randIx]; |
||||
array[randIx] = temp; |
||||
} |
||||
} |
||||
|
||||
public boolean isPuzzleSolvable() { |
||||
if (gridSize != 4) |
||||
return true; |
||||
|
||||
int sum = 0; |
||||
for (int i = 0; i < gridArea; i++) { |
||||
if (mIndexses[i] == gridEmptyIdx) |
||||
sum += (i / gridSize) + 1; |
||||
else { |
||||
int smaller = 0; |
||||
for (int j = i + 1; j < gridArea; j++) { |
||||
if (mIndexses[j] < mIndexses[i]) |
||||
smaller++; |
||||
} |
||||
sum += smaller; |
||||
} |
||||
} |
||||
|
||||
return sum % 2 == 0; |
||||
} |
||||
|
||||
private void createPuzzle(int cols, int rows) { |
||||
mCells = new Mat[gridArea]; |
||||
mCells15 = new Mat[gridArea]; |
||||
|
||||
mRgba15 = new Mat(rows, cols, mRgba.type()); |
||||
mIndexses = new int[gridArea]; |
||||
|
||||
for (int i = 0; i < gridSize; i++) { |
||||
for (int j = 0; j < gridSize; j++) { |
||||
int k = i * gridSize + j; |
||||
mIndexses[k] = k; |
||||
mCells[k] = mRgba.submat(i * rows / gridSize, (i + 1) * rows / gridSize, j * cols / gridSize, (j + 1) * cols / gridSize); |
||||
mCells15[k] = mRgba15.submat(i * rows / gridSize, (i + 1) * rows / gridSize, j * cols / gridSize, (j + 1) * cols / gridSize); |
||||
} |
||||
} |
||||
|
||||
startNewGame(); |
||||
} |
||||
|
||||
public void startNewGame() { |
||||
do { |
||||
shuffle(mIndexses); |
||||
} while (!isPuzzleSolvable()); |
||||
} |
||||
|
||||
public void tolggleTileNumbers() { |
||||
mShowTileNumbers = !mShowTileNumbers; |
||||
} |
||||
|
||||
@Override |
||||
protected Bitmap processFrame(VideoCapture capture) { |
||||
capture.retrieve(mRgba, highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); |
||||
int cols = mRgba.cols(); |
||||
int rows = mRgba.rows(); |
||||
|
||||
if (mCells == null) |
||||
createPuzzle(cols, rows); |
||||
|
||||
// copy shuffled tiles
|
||||
for (int i = 0; i < gridArea; i++) { |
||||
int idx = mIndexses[i]; |
||||
if (idx == gridEmptyIdx) |
||||
mCells15[i].setTo(new Scalar(0x33, 0x33, 0x33, 0xFF)); |
||||
else { |
||||
mCells[idx].copyTo(mCells15[i]); |
||||
if (mShowTileNumbers) { |
||||
core.putText(mCells15[i], Integer.toString(1 + idx), new Point((cols / gridSize - mTextWidths[idx]) / 2, |
||||
(rows / gridSize + mTextHeights[idx]) / 2), 3/* CV_FONT_HERSHEY_COMPLEX */, 1, new Scalar(255, 0, 0, 255), 2); |
||||
} |
||||
} |
||||
} |
||||
|
||||
drawGrid(cols, rows); |
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(cols, rows, Bitmap.Config.ARGB_8888); |
||||
if (android.MatToBitmap(mRgba15, bmp)) |
||||
return bmp; |
||||
|
||||
bmp.recycle(); |
||||
return null; |
||||
} |
||||
|
||||
private void drawGrid(int cols, int rows) { |
||||
for (int i = 1; i < gridSize; i++) { |
||||
core.line(mRgba15, new Point(0, i * rows / gridSize), new Point(cols, i * rows / gridSize), new Scalar(0, 255, 0, 255), 3); |
||||
core.line(mRgba15, new Point(i * cols / gridSize, 0), new Point(i * cols / gridSize, rows), new Scalar(0, 255, 0, 255), 3); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
super.run(); |
||||
|
||||
synchronized (this) { |
||||
// Explicitly deallocate Mats
|
||||
if (mCells != null) { |
||||
for (Mat m : mCells) |
||||
m.dispose(); |
||||
} |
||||
if (mCells15 != null) { |
||||
for (Mat m : mCells15) |
||||
m.dispose(); |
||||
} |
||||
if (mRgba != null) |
||||
mRgba.dispose(); |
||||
if (mRgba15 != null) |
||||
mRgba15.dispose(); |
||||
|
||||
mRgba = null; |
||||
mRgba15 = null; |
||||
mCells = null; |
||||
mCells15 = null; |
||||
mIndexses = null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean onTouch(View v, MotionEvent event) { |
||||
int cols = mRgba.cols(); |
||||
int rows = mRgba.rows(); |
||||
float xoffset = (getWidth() - cols) / 2; |
||||
float yoffset = (getHeight() - rows) / 2; |
||||
|
||||
float x = event.getX() - xoffset; |
||||
float y = event.getY() - yoffset; |
||||
|
||||
int row = (int) Math.floor(y * gridSize / rows); |
||||
int col = (int) Math.floor(x * gridSize / cols); |
||||
|
||||
if (row < 0 || row >= gridSize || col < 0 || col >= gridSize) |
||||
return false; |
||||
|
||||
int idx = row * gridSize + col; |
||||
int idxtoswap = -1; |
||||
|
||||
// left
|
||||
if (idxtoswap < 0 && col > 0) |
||||
if (mIndexses[idx - 1] == gridEmptyIdx) |
||||
idxtoswap = idx - 1; |
||||
// right
|
||||
if (idxtoswap < 0 && col < gridSize - 1) |
||||
if (mIndexses[idx + 1] == gridEmptyIdx) |
||||
idxtoswap = idx + 1; |
||||
// top
|
||||
if (idxtoswap < 0 && row > 0) |
||||
if (mIndexses[idx - gridSize] == gridEmptyIdx) |
||||
idxtoswap = idx - gridSize; |
||||
// bottom
|
||||
if (idxtoswap < 0 && row < gridSize - 1) |
||||
if (mIndexses[idx + gridSize] == gridEmptyIdx) |
||||
idxtoswap = idx + gridSize; |
||||
|
||||
// swap
|
||||
if (idxtoswap >= 0) { |
||||
synchronized (this) { |
||||
int touched = mIndexses[idx]; |
||||
mIndexses[idx] = mIndexses[idxtoswap]; |
||||
mIndexses[idxtoswap] = touched; |
||||
} |
||||
} |
||||
|
||||
return false;// don't need subsequent touch events
|
||||
} |
||||
} |
Loading…
Reference in new issue