Code clean up.
@ -1,8 +0,0 @@
|
|||||||
<?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="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
|
||||||
<classpathentry kind="output" path="bin/classes"/>
|
|
||||||
</classpath>
|
|
@ -1,33 +0,0 @@
|
|||||||
<?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>
|
|
||||||
</projectDescription>
|
|
@ -1,28 +0,0 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
package="org.opencv.samples.puzzle15"
|
|
||||||
android:versionCode="1"
|
|
||||||
android:versionName="1.0" >
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.CAMERA" required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera" required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera.autofocus" required="false"/>
|
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="8"/>
|
|
||||||
|
|
||||||
<application
|
|
||||||
android:icon="@drawable/ic_launcher"
|
|
||||||
android:label="@string/app_name"
|
|
||||||
android:theme="@style/AppTheme" >
|
|
||||||
<activity
|
|
||||||
android:name=".Puzzle15Activity"
|
|
||||||
android:label="@string/title_activity_puzzle15"
|
|
||||||
android:configChanges = "keyboardHidden|orientation" >
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MAIN" />
|
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
|
||||||
</application>
|
|
||||||
|
|
||||||
</manifest>
|
|
Before Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 3.9 KiB |
@ -1,10 +0,0 @@
|
|||||||
<resources>
|
|
||||||
|
|
||||||
<string name="app_name">15-puzzle</string>
|
|
||||||
<string name="hello_world">Hello world!</string>
|
|
||||||
<string name="menu_settings">Settings</string>
|
|
||||||
<string name="title_activity_puzzle15">Puzzle15Activity</string>
|
|
||||||
<string name="menu_toggle_tile_numbers">Show/hide tile numbers</string>
|
|
||||||
<string name="menu_start_new_game">Start new game</string>
|
|
||||||
|
|
||||||
</resources>
|
|
@ -1,5 +0,0 @@
|
|||||||
<resources>
|
|
||||||
|
|
||||||
<style name="AppTheme" parent="android:Theme.Light" />
|
|
||||||
|
|
||||||
</resources>
|
|
@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" path="src"/>
|
|
||||||
<classpathentry kind="src" path="gen"/>
|
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="gen"/>
|
||||||
<classpathentry kind="output" path="bin/classes"/>
|
<classpathentry kind="output" path="bin/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<projectDescription>
|
<projectDescription>
|
||||||
<name>OpenCV Sample - 15-puzzle</name>
|
<name>15-puzzle</name>
|
||||||
<comment></comment>
|
<comment></comment>
|
||||||
<projects>
|
<projects>
|
||||||
</projects>
|
</projects>
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
#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
|
|
@ -1,34 +1,28 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="org.opencv.samples.puzzle15"
|
package="org.opencv.samples.puzzle15"
|
||||||
android:versionCode="21"
|
android:versionCode="1"
|
||||||
android:versionName="2.1">
|
android:versionName="1.0" >
|
||||||
|
|
||||||
<application android:label="@string/app_name" android:icon="@drawable/icon">
|
<uses-permission android:name="android.permission.CAMERA" required="false"/>
|
||||||
<activity android:name="puzzle15Activity"
|
<uses-feature android:name="android.hardware.camera" required="false"/>
|
||||||
android:label="@string/app_name"
|
<uses-feature android:name="android.hardware.camera.autofocus" required="false"/>
|
||||||
android:screenOrientation="landscape"
|
|
||||||
android:configChanges="keyboardHidden|orientation">
|
<uses-sdk android:minSdkVersion="8"/>
|
||||||
|
|
||||||
|
<application
|
||||||
|
android:icon="@drawable/icon"
|
||||||
|
android:label="@string/app_name" >
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name=".Puzzle15Activity"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:configChanges = "keyboardHidden|orientation" >
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
<supports-screens android:resizeable="true"
|
|
||||||
android:smallScreens="true"
|
|
||||||
android:normalScreens="true"
|
|
||||||
android:largeScreens="true"
|
|
||||||
android:anyDensity="true" />
|
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="8" />
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.CAMERA"/>
|
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.camera" android:required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
|
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
@ -1,6 +0,0 @@
|
|||||||
set(sample example-15-puzzle)
|
|
||||||
|
|
||||||
add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}" LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11 ${ANDROID_SDK_TARGET})
|
|
||||||
if(TARGET ${sample})
|
|
||||||
add_dependencies(opencv_android_examples ${sample})
|
|
||||||
endif()
|
|
@ -3,10 +3,9 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" >
|
android:layout_height="match_parent" >
|
||||||
|
|
||||||
<org.opencv.framework.OpenCvNativeCameraView
|
<org.opencv.framework.OpenCvJavaCameraView
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="fill_parent"
|
android:layout_height="fill_parent"
|
||||||
android:id="@+id/puzzle_activity_surface_view"
|
android:id="@+id/puzzle_activity_surface_view" />
|
||||||
/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
@ -1,4 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">OCV 15 Puzzle</string>
|
<string name="app_name">OCV 15 Puzzle</string>
|
||||||
</resources>
|
<string name="menu_toggle_tile_numbers">Show/hide tile numbers</string>
|
||||||
|
<string name="menu_start_new_game">Start new game</string>
|
||||||
|
|
||||||
|
</resources>
|
@ -5,7 +5,7 @@ import org.opencv.android.LoaderCallbackInterface;
|
|||||||
import org.opencv.android.OpenCVLoader;
|
import org.opencv.android.OpenCVLoader;
|
||||||
import org.opencv.core.Mat;
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.framework.OpenCvCameraBridgeViewBase.CvCameraViewListener;
|
import org.opencv.framework.OpenCvCameraBridgeViewBase.CvCameraViewListener;
|
||||||
import org.opencv.framework.OpenCvNativeCameraView;
|
import org.opencv.framework.OpenCvJavaCameraView;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
@ -20,7 +20,7 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
|
|||||||
|
|
||||||
private static final String TAG = "Sample::Puzzle15::Activity";
|
private static final String TAG = "Sample::Puzzle15::Activity";
|
||||||
|
|
||||||
private OpenCvNativeCameraView mOpenCvCameraView;
|
private OpenCvJavaCameraView mOpenCvCameraView;
|
||||||
private Puzzle15Processor mPuzzle15;
|
private Puzzle15Processor mPuzzle15;
|
||||||
|
|
||||||
private int mGameWidth;
|
private int mGameWidth;
|
||||||
@ -54,7 +54,7 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener,
|
|||||||
|
|
||||||
setContentView(R.layout.activity_puzzle15);
|
setContentView(R.layout.activity_puzzle15);
|
||||||
|
|
||||||
mOpenCvCameraView = (OpenCvNativeCameraView) findViewById(R.id.puzzle_activity_surface_view);
|
mOpenCvCameraView = (OpenCvJavaCameraView) findViewById(R.id.puzzle_activity_surface_view);
|
||||||
mOpenCvCameraView.setCvCameraViewListener(this);
|
mOpenCvCameraView.setCvCameraViewListener(this);
|
||||||
mPuzzle15 = new Puzzle15Processor();
|
mPuzzle15 = new Puzzle15Processor();
|
||||||
mPuzzle15.prepareNewGame();
|
mPuzzle15.prepareNewGame();
|
@ -1,117 +0,0 @@
|
|||||||
package org.opencv.samples.puzzle15;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.opencv.core.Size;
|
|
||||||
import org.opencv.highgui.VideoCapture;
|
|
||||||
import org.opencv.highgui.Highgui;
|
|
||||||
|
|
||||||
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 SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
|
||||||
private static final String TAG = "OCVSample::BaseView";
|
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
|
||||||
private VideoCapture mCamera;
|
|
||||||
|
|
||||||
public SampleCvViewBase(Context context) {
|
|
||||||
super(context);
|
|
||||||
mHolder = getHolder();
|
|
||||||
mHolder.addCallback(this);
|
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized boolean openCamera() {
|
|
||||||
Log.i(TAG, "Opening Camera");
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
if (!mCamera.isOpened()) {
|
|
||||||
releaseCamera();
|
|
||||||
Log.e(TAG, "Can't open native camera");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void releaseCamera() {
|
|
||||||
Log.i(TAG, "Releasing Camera");
|
|
||||||
if (mCamera != null) {
|
|
||||||
mCamera.release();
|
|
||||||
mCamera = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void setupCamera(int width, int height) {
|
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
|
||||||
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
|
||||||
List<Size> sizes = 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 surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
|
||||||
Log.i(TAG, "called surfaceChanged");
|
|
||||||
setupCamera(width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
|
||||||
Log.i(TAG, "called surfaceCreated");
|
|
||||||
(new Thread(this)).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
||||||
Log.i(TAG, "called surfaceDestroyed");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
Log.i(TAG, "Started 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.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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Log.i(TAG, "Finished processing thread");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,123 +0,0 @@
|
|||||||
package org.opencv.samples.puzzle15;
|
|
||||||
|
|
||||||
import org.opencv.android.BaseLoaderCallback;
|
|
||||||
import org.opencv.android.LoaderCallbackInterface;
|
|
||||||
import org.opencv.android.OpenCVLoader;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.AlertDialog;
|
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.os.Bundle;
|
|
||||||
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 = "OCVSample::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) {
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
MarketErrorMessage.show();
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
super.onManagerConnected(status);
|
|
||||||
} break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public puzzle15Activity() {
|
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
Log.i(TAG, "called onPause");
|
|
||||||
if (null != mView)
|
|
||||||
mView.releaseCamera();
|
|
||||||
super.onPause();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onResume() {
|
|
||||||
Log.i(TAG, "called 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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Called when the activity is first created. */
|
|
||||||
@Override
|
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
|
||||||
Log.i(TAG, "called 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, "called 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, "called onOptionsItemSelected; selected item: " + item);
|
|
||||||
if (item == mItemNewGame)
|
|
||||||
mView.startNewGame();
|
|
||||||
else if (item == mItemToggleNumbers)
|
|
||||||
mView.tolggleTileNumbers();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,250 +0,0 @@
|
|||||||
package org.opencv.samples.puzzle15;
|
|
||||||
|
|
||||||
import org.opencv.android.Utils;
|
|
||||||
import org.opencv.core.Core;
|
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.Size;
|
|
||||||
import org.opencv.core.Point;
|
|
||||||
import org.opencv.core.Scalar;
|
|
||||||
import org.opencv.highgui.Highgui;
|
|
||||||
import org.opencv.highgui.VideoCapture;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.SurfaceHolder;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.View.OnTouchListener;
|
|
||||||
|
|
||||||
public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
|
||||||
private static final String TAG = "OCVSample::View";
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
|
||||||
Log.i(TAG, "called surfaceCreated");
|
|
||||||
synchronized (this) {
|
|
||||||
// initialize Mat before usage
|
|
||||||
mRgba = new Mat();
|
|
||||||
}
|
|
||||||
|
|
||||||
super.surfaceCreated(holder);
|
|
||||||
}
|
|
||||||
|
|
||||||
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, int type) {
|
|
||||||
mCells = new Mat[gridArea];
|
|
||||||
mCells15 = new Mat[gridArea];
|
|
||||||
|
|
||||||
mRgba15 = new Mat(rows, cols, 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized 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();
|
|
||||||
|
|
||||||
rows = rows - rows%4;
|
|
||||||
cols = cols - cols%4;
|
|
||||||
|
|
||||||
if (mCells == null)
|
|
||||||
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++) {
|
|
||||||
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);
|
|
||||||
try {
|
|
||||||
Utils.matToBitmap(mRgba15, bmp);
|
|
||||||
return bmp;
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
|
||||||
bmp.recycle();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
super.run();
|
|
||||||
|
|
||||||
synchronized (this) {
|
|
||||||
releaseMats();
|
|
||||||
|
|
||||||
if (mRgba != null)
|
|
||||||
mRgba.release();
|
|
||||||
mRgba = 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 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
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<classpath>
|
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
|
||||||
<classpathentry kind="src" path="src"/>
|
|
||||||
<classpathentry kind="src" path="gen"/>
|
|
||||||
<classpathentry kind="output" path="bin/classes"/>
|
|
||||||
</classpath>
|
|
@ -1,33 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<projectDescription>
|
|
||||||
<name>CameraWriter</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>
|
|
||||||
</projectDescription>
|
|
@ -1,5 +0,0 @@
|
|||||||
#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
|
|
@ -1,27 +0,0 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
package="org.opencv.test.camerawriter"
|
|
||||||
android:versionCode="1"
|
|
||||||
android:versionName="1.0" >
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.CAMERA" required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera" required="false"/>
|
|
||||||
<uses-feature android:name="android.hardware.camera.autofocus" required="false"/>
|
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="8" />
|
|
||||||
|
|
||||||
<application
|
|
||||||
android:icon="@drawable/ic_launcher"
|
|
||||||
android:label="@string/app_name"
|
|
||||||
android:theme="@style/AppTheme" >
|
|
||||||
<activity
|
|
||||||
android:name=".PreviewActivity"
|
|
||||||
android:label="@string/title_activity_camera_writer" >
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MAIN" />
|
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
|
||||||
</application>
|
|
||||||
|
|
||||||
</manifest>
|
|
@ -1,6 +0,0 @@
|
|||||||
set(sample example-camera-preview)
|
|
||||||
|
|
||||||
add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}" LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11 ${ANDROID_SDK_TARGET})
|
|
||||||
if(TARGET ${sample})
|
|
||||||
add_dependencies(opencv_android_examples ${sample})
|
|
||||||
endif()
|
|
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.9 KiB |
@ -1,13 +0,0 @@
|
|||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent" >
|
|
||||||
|
|
||||||
<view class="org.opencv.test.camerawriter.NativeCameraView"
|
|
||||||
android:id="@+id/camera_surface_view"
|
|
||||||
android:layout_width = "fill_parent"
|
|
||||||
android:layout_height="fill_parent"
|
|
||||||
/>
|
|
||||||
|
|
||||||
|
|
||||||
</RelativeLayout>
|
|
@ -1,7 +0,0 @@
|
|||||||
<resources>
|
|
||||||
|
|
||||||
<string name="app_name">CameraWriter</string>
|
|
||||||
<string name="menu_settings">Settings</string>
|
|
||||||
<string name="title_activity_camera_writer">CameraWriterActivity</string>
|
|
||||||
|
|
||||||
</resources>
|
|
@ -1,5 +0,0 @@
|
|||||||
<resources>
|
|
||||||
|
|
||||||
<style name="AppTheme" parent="android:Theme.Light" />
|
|
||||||
|
|
||||||
</resources>
|
|
@ -1,301 +0,0 @@
|
|||||||
package org.opencv.test.camerawriter;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.opencv.android.Utils;
|
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.Size;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.Canvas;
|
|
||||||
import android.util.AttributeSet;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.SurfaceHolder;
|
|
||||||
import android.view.SurfaceView;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a basic class, implementing the interaction with Camera and OpenCV library.
|
|
||||||
* The main responsibility of it - is to control when camera can be enabled, process the frame,
|
|
||||||
* call external listener to make any adjustments to the frame and then draw the resulting
|
|
||||||
* frame to the screen.
|
|
||||||
* The clients shall implement CvCameraViewListener
|
|
||||||
*/
|
|
||||||
public abstract class CameraViewBase extends SurfaceView implements SurfaceHolder.Callback {
|
|
||||||
|
|
||||||
private static final int MAX_UNSPECIFIED = -1;
|
|
||||||
|
|
||||||
protected int mFrameWidth;
|
|
||||||
protected int mFrameHeight;
|
|
||||||
|
|
||||||
protected int mMaxHeight;
|
|
||||||
protected int mMaxWidth;
|
|
||||||
|
|
||||||
private Bitmap mCacheBitmap;
|
|
||||||
|
|
||||||
|
|
||||||
public CameraViewBase(Context context, AttributeSet attrs) {
|
|
||||||
super(context,attrs);
|
|
||||||
Log.d(TAG, "call CameraViewBase constructor");
|
|
||||||
getHolder().addCallback(this);
|
|
||||||
mMaxWidth = MAX_UNSPECIFIED;
|
|
||||||
mMaxHeight = MAX_UNSPECIFIED;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface CvCameraViewListener {
|
|
||||||
/**
|
|
||||||
* This method is invoked when camera preview has started. After this method is invoked
|
|
||||||
* the frames will start to be delivered to client via the onCameraFrame() callback.
|
|
||||||
* @param width - the width of the frames that will be delivered
|
|
||||||
* @param height - the height of the frames that will be delivered
|
|
||||||
*/
|
|
||||||
public void onCameraViewStarted(int width, int height);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is invoked when camera preview has been stopped for some reason.
|
|
||||||
* No frames will be delivered via onCameraFrame() callback after this method is called.
|
|
||||||
*/
|
|
||||||
public void onCameraViewStopped();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is invoked when delivery of the frame needs to be done.
|
|
||||||
* The returned values - is a modified frame which needs to be displayed on the screen.
|
|
||||||
*/
|
|
||||||
public Mat onCameraFrame(Mat inputFrame);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static final int STOPPED = 0;
|
|
||||||
private static final int STARTED = 1;
|
|
||||||
private static final String TAG = "SampleCvBase";
|
|
||||||
|
|
||||||
private CvCameraViewListener mListener;
|
|
||||||
private int mState = STOPPED;
|
|
||||||
|
|
||||||
private boolean mEnabled;
|
|
||||||
private boolean mSurfaceExist;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private Object mSyncObject = new Object();
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
|
|
||||||
synchronized(mSyncObject) {
|
|
||||||
if (!mSurfaceExist) {
|
|
||||||
mSurfaceExist = true;
|
|
||||||
checkCurrentState();
|
|
||||||
} else {
|
|
||||||
/** Surface changed. We need to stop camera and restart with new parameters */
|
|
||||||
/* Pretend that old surface has been destroyed */
|
|
||||||
mSurfaceExist = false;
|
|
||||||
checkCurrentState();
|
|
||||||
/* Now use new surface. Say we have it now */
|
|
||||||
mSurfaceExist = true;
|
|
||||||
checkCurrentState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
|
||||||
/* Do nothing. Wait until surfaceChanged delivered */
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
||||||
synchronized(mSyncObject) {
|
|
||||||
mSurfaceExist = false;
|
|
||||||
checkCurrentState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is provided for clients, so they can enable the camera connection.
|
|
||||||
* The actual onCameraViewStarted callback will be delivered only after both this method is called and surface is available
|
|
||||||
*/
|
|
||||||
public void enableView() {
|
|
||||||
synchronized(mSyncObject) {
|
|
||||||
mEnabled = true;
|
|
||||||
checkCurrentState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is provided for clients, so they can disable camera connection and stop
|
|
||||||
* the delivery of frames even though the surface view itself is not destroyed and still stays on the scren
|
|
||||||
*/
|
|
||||||
public void disableView() {
|
|
||||||
synchronized(mSyncObject) {
|
|
||||||
mEnabled = false;
|
|
||||||
checkCurrentState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void setCvCameraViewListener(CvCameraViewListener listener) {
|
|
||||||
mListener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method sets the maximum size that camera frame is allowed to be. When selecting
|
|
||||||
* size - the biggest size which less or equal the size set will be selected.
|
|
||||||
* As an example - we set setMaxFrameSize(200,200) and we have 176x152 and 320x240 sizes. The
|
|
||||||
* preview frame will be selected with 176x152 size.
|
|
||||||
* This method is useful when need to restrict the size of preview frame for some reason (for example for video recording)
|
|
||||||
* @param maxWidth - the maximum width allowed for camera frame.
|
|
||||||
* @param maxHeight - the maximum height allowed for camera frame
|
|
||||||
*/
|
|
||||||
public void setMaxFrameSize(int maxWidth, int maxHeight) {
|
|
||||||
mMaxWidth = maxWidth;
|
|
||||||
mMaxHeight = maxHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when mSyncObject lock is held
|
|
||||||
*/
|
|
||||||
private void checkCurrentState() {
|
|
||||||
int targetState;
|
|
||||||
|
|
||||||
if (mEnabled && mSurfaceExist) {
|
|
||||||
targetState = STARTED;
|
|
||||||
} else {
|
|
||||||
targetState = STOPPED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetState != mState) {
|
|
||||||
/* The state change detected. Need to exit the current state and enter target state */
|
|
||||||
processExitState(mState);
|
|
||||||
mState = targetState;
|
|
||||||
processEnterState(mState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processEnterState(int state) {
|
|
||||||
switch(state) {
|
|
||||||
case STARTED:
|
|
||||||
onEnterStartedState();
|
|
||||||
if (mListener != null) {
|
|
||||||
mListener.onCameraViewStarted(mFrameWidth, mFrameHeight);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case STOPPED:
|
|
||||||
onEnterStoppedState();
|
|
||||||
if (mListener != null) {
|
|
||||||
mListener.onCameraViewStopped();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void processExitState(int state) {
|
|
||||||
switch(state) {
|
|
||||||
case STARTED:
|
|
||||||
onExitStartedState();
|
|
||||||
break;
|
|
||||||
case STOPPED:
|
|
||||||
onExitStoppedState();
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onEnterStoppedState() {
|
|
||||||
/* nothing to do */
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onExitStoppedState() {
|
|
||||||
/* nothing to do */
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onEnterStartedState() {
|
|
||||||
|
|
||||||
connectCamera(getWidth(), getHeight());
|
|
||||||
/* Now create cache Bitmap */
|
|
||||||
mCacheBitmap = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onExitStartedState() {
|
|
||||||
|
|
||||||
disconnectCamera();
|
|
||||||
if (mCacheBitmap != null) {
|
|
||||||
mCacheBitmap.recycle();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method shall be called by the subclasses when they have valid
|
|
||||||
* object and want it to be delivered to external client (via callback) and
|
|
||||||
* then displayed on the screen.
|
|
||||||
* @param frame - the current frame to be delivered
|
|
||||||
*/
|
|
||||||
protected void deliverAndDrawFrame(Mat frame) {
|
|
||||||
Mat modified;
|
|
||||||
|
|
||||||
synchronized(mSyncObject) {
|
|
||||||
if (mListener != null) {
|
|
||||||
modified = mListener.onCameraFrame(frame);
|
|
||||||
} else {
|
|
||||||
modified = frame;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (modified != null) {
|
|
||||||
Utils.matToBitmap(modified, mCacheBitmap);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mCacheBitmap != null) {
|
|
||||||
Canvas canvas = getHolder().lockCanvas();
|
|
||||||
if (canvas != null) {
|
|
||||||
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
|
|
||||||
canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, null);
|
|
||||||
getHolder().unlockCanvasAndPost(canvas);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is invoked shall perform concrete operation to initialize the camera.
|
|
||||||
* CONTRACT: as a result of this method variables mFrameWidth and mFrameHeight MUST be
|
|
||||||
* initialized with the size of the Camera frames that will be delivered to external processor.
|
|
||||||
* @param width - the width of this SurfaceView
|
|
||||||
* @param height - the height of this SurfaceView
|
|
||||||
*/
|
|
||||||
protected abstract void connectCamera(int width, int height);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disconnects and release the particular camera object beeing connected to this surface view.
|
|
||||||
* Called when syncObject lock is held
|
|
||||||
*/
|
|
||||||
protected abstract void disconnectCamera();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This helper method can be called by subclasses to select camera preview size.
|
|
||||||
* It goes over the list of the supported preview sizes and selects the maximum one which
|
|
||||||
* fits both values set via setMaxFrameSize() and surface frame allocated for this view
|
|
||||||
* @param supportedSizes
|
|
||||||
* @param surfaceWidth
|
|
||||||
* @param surfaceHeight
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
protected Size calculateCameraFrameSize(List<Size> supportedSizes, int surfaceWidth, int surfaceHeight) {
|
|
||||||
int calcWidth = 0;
|
|
||||||
int calcHeight = 0;
|
|
||||||
|
|
||||||
int maxAllowedWidth = (mMaxWidth != MAX_UNSPECIFIED && mMaxWidth < surfaceWidth)? mMaxWidth : surfaceWidth;
|
|
||||||
int maxAllowedHeight = (mMaxHeight != MAX_UNSPECIFIED && mMaxHeight < surfaceHeight)? mMaxHeight : surfaceHeight;
|
|
||||||
|
|
||||||
for (Size size : supportedSizes) {
|
|
||||||
if (size.width <= maxAllowedWidth && size.height <= maxAllowedHeight) {
|
|
||||||
if (size.width >= calcWidth && size.height >= calcHeight) {
|
|
||||||
calcWidth = (int) size.width;
|
|
||||||
calcHeight = (int) size.height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Size(calcWidth, calcHeight);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,110 +0,0 @@
|
|||||||
package org.opencv.test.camerawriter;
|
|
||||||
|
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.Size;
|
|
||||||
import org.opencv.highgui.Highgui;
|
|
||||||
import org.opencv.highgui.VideoCapture;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.AttributeSet;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class is an implementation of a bridge between SurfaceView and native OpenCV camera.
|
|
||||||
* Due to the big amount of work done, by the base class this child is only responsible
|
|
||||||
* for creating camera, destroying camera and delivering frames while camera is enabled
|
|
||||||
*/
|
|
||||||
public class NativeCameraView extends CameraViewBase {
|
|
||||||
|
|
||||||
public static final String TAG = "OpenCvNativeCameraView";
|
|
||||||
private boolean mStopThread;
|
|
||||||
private Thread mThread;
|
|
||||||
private VideoCapture mCamera;
|
|
||||||
|
|
||||||
|
|
||||||
public NativeCameraView(Context context, AttributeSet attrs) {
|
|
||||||
super(context, attrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void connectCamera(int width, int height) {
|
|
||||||
|
|
||||||
/* 1. We need to instantiate camera
|
|
||||||
* 2. We need to start thread which will be getting frames
|
|
||||||
*/
|
|
||||||
/* First step - initialize camera connection */
|
|
||||||
initializeCamera(getWidth(), getHeight());
|
|
||||||
|
|
||||||
/* now we can start update thread */
|
|
||||||
mThread = new Thread(new CameraWorker());
|
|
||||||
mThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void disconnectCamera() {
|
|
||||||
/* 1. We need to stop thread which updating the frames
|
|
||||||
* 2. Stop camera and release it
|
|
||||||
*/
|
|
||||||
try {
|
|
||||||
mStopThread = true;
|
|
||||||
mThread.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
mThread = null;
|
|
||||||
mStopThread = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Now release camera */
|
|
||||||
releaseCamera();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initializeCamera(int width, int height) {
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
|
||||||
//TODO: improve error handling
|
|
||||||
|
|
||||||
java.util.List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
|
||||||
|
|
||||||
/* Select the size that fits surface considering maximum size allowed */
|
|
||||||
Size frameSize = calculateCameraFrameSize(sizes, width, height);
|
|
||||||
|
|
||||||
|
|
||||||
double frameWidth = frameSize.width;
|
|
||||||
double frameHeight = frameSize.height;
|
|
||||||
|
|
||||||
|
|
||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, frameWidth);
|
|
||||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, frameHeight);
|
|
||||||
|
|
||||||
mFrameWidth = (int)frameWidth;
|
|
||||||
mFrameHeight = (int)frameHeight;
|
|
||||||
|
|
||||||
Log.i(TAG, "Selected camera frame size = (" + mFrameWidth + ", " + mFrameHeight + ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void releaseCamera() {
|
|
||||||
if (mCamera != null) {
|
|
||||||
mCamera.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class CameraWorker implements Runnable {
|
|
||||||
public void run() {
|
|
||||||
do {
|
|
||||||
if (!mCamera.grab()) {
|
|
||||||
Log.e(TAG, "Camera frame grab failed");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
|
||||||
|
|
||||||
deliverAndDrawFrame(mRgba);
|
|
||||||
|
|
||||||
} while (!mStopThread);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Mat mRgba = new Mat();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
package org.opencv.test.camerawriter;
|
|
||||||
|
|
||||||
import org.opencv.android.BaseLoaderCallback;
|
|
||||||
import org.opencv.android.LoaderCallbackInterface;
|
|
||||||
import org.opencv.android.OpenCVLoader;
|
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.test.camerawriter.CameraViewBase.CvCameraViewListener;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
public class PreviewActivity extends Activity implements CvCameraViewListener {
|
|
||||||
|
|
||||||
protected static final String TAG = "CameraWriterActivity";
|
|
||||||
|
|
||||||
private CameraViewBase mCameraView;
|
|
||||||
|
|
||||||
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
|
||||||
@Override
|
|
||||||
public void onManagerConnected(int status) {
|
|
||||||
switch (status) {
|
|
||||||
case LoaderCallbackInterface.SUCCESS:
|
|
||||||
Log.i(TAG, "OpenCV loaded successfully");
|
|
||||||
// Create and set View
|
|
||||||
mCameraView.setMaxFrameSize(640, 480);
|
|
||||||
mCameraView.enableView();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
super.onManagerConnected(status);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
setContentView(R.layout.activity_camera_writer);
|
|
||||||
|
|
||||||
mCameraView = (CameraViewBase)findViewById(R.id.camera_surface_view);
|
|
||||||
mCameraView.setCvCameraViewListener(this);
|
|
||||||
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onCameraViewStarted(int width, int height) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onCameraViewStopped() {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Mat onCameraFrame(Mat inputFrame) {
|
|
||||||
return inputFrame;
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,16 +5,11 @@ import android.app.AlertDialog;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
|
|
||||||
public class Sample0Base extends Activity {
|
public class Sample0Base extends Activity {
|
||||||
|
|
||||||
private static final String TAG = "OCVSample::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private MenuItem mItemPreviewRGBA;
|
|
||||||
private MenuItem mItemPreviewGray;
|
|
||||||
private Sample0View mView;
|
private Sample0View mView;
|
||||||
|
|
||||||
public Sample0Base() {
|
public Sample0Base() {
|
||||||
@ -55,22 +50,4 @@ public class Sample0Base extends Activity {
|
|||||||
mView = new Sample0View(this);
|
mView = new Sample0View(this);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
|
||||||
Log.i(TAG, "called onCreateOptionsMenu");
|
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
|
||||||
mItemPreviewGray = menu.add("Preview GRAY");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
|
||||||
if (item == mItemPreviewRGBA)
|
|
||||||
mView.setViewMode(Sample0View.VIEW_MODE_RGBA);
|
|
||||||
else if (item == mItemPreviewGray)
|
|
||||||
mView.setViewMode(Sample0View.VIEW_MODE_GRAY);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package org.opencv.samples.tutorial0;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
@ -43,7 +44,8 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
return mFrameHeight;
|
return mFrameHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPreview() throws IOException {
|
@TargetApi(11)
|
||||||
|
public void setPreview() throws IOException {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||||
mSf = new SurfaceTexture(10);
|
mSf = new SurfaceTexture(10);
|
||||||
mCamera.setPreviewTexture( mSf );
|
mCamera.setPreviewTexture( mSf );
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
android:largeScreens="true"
|
android:largeScreens="true"
|
||||||
android:anyDensity="true" />
|
android:anyDensity="true" />
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="11" />
|
<uses-sdk android:minSdkVersion="8" />
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.CAMERA"/>
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ import org.opencv.android.OpenCVLoader;
|
|||||||
import org.opencv.core.CvType;
|
import org.opencv.core.CvType;
|
||||||
import org.opencv.core.Mat;
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.framework.OpenCvJavaCameraView;
|
import org.opencv.framework.OpenCvJavaCameraView;
|
||||||
import org.opencv.framework.OpenCvNativeCameraView;
|
|
||||||
import org.opencv.framework.OpenCvCameraBridgeViewBase.CvCameraViewListener;
|
import org.opencv.framework.OpenCvCameraBridgeViewBase.CvCameraViewListener;
|
||||||
import org.opencv.highgui.Highgui;
|
import org.opencv.highgui.Highgui;
|
||||||
import org.opencv.imgproc.Imgproc;
|
import org.opencv.imgproc.Imgproc;
|
||||||
@ -122,8 +121,6 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener {
|
|||||||
public Mat onCameraFrame(Mat inputFrame) {
|
public Mat onCameraFrame(Mat inputFrame) {
|
||||||
final int viewMode = mViewMode;
|
final int viewMode = mViewMode;
|
||||||
|
|
||||||
Log.d(TAG, "Input format: " + inputFrame);
|
|
||||||
|
|
||||||
switch (viewMode) {
|
switch (viewMode) {
|
||||||
case VIEW_MODE_GRAY:
|
case VIEW_MODE_GRAY:
|
||||||
// input frame has gray scale format
|
// input frame has gray scale format
|
||||||
@ -140,15 +137,12 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener {
|
|||||||
break;
|
break;
|
||||||
case VIEW_MODE_FEATURES:
|
case VIEW_MODE_FEATURES:
|
||||||
// input frame has RGBA format
|
// input frame has RGBA format
|
||||||
//Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);
|
|
||||||
inputFrame.copyTo(mRgba);
|
inputFrame.copyTo(mRgba);
|
||||||
Imgproc.cvtColor(mRgba, mGrayMat, Imgproc.COLOR_RGBA2GRAY);
|
Imgproc.cvtColor(mRgba, mGrayMat, Imgproc.COLOR_RGBA2GRAY);
|
||||||
FindFeatures(mGrayMat.getNativeObjAddr(), mRgba.getNativeObjAddr());
|
FindFeatures(mGrayMat.getNativeObjAddr(), mRgba.getNativeObjAddr());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(TAG, "Output format: " + mRgba);
|
|
||||||
|
|
||||||
return mRgba;
|
return mRgba;
|
||||||
}
|
}
|
||||||
|
|
||||||
|