From d015a50d514b4839ede597d65934252c789415b6 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Fri, 27 Apr 2012 13:37:45 +0000 Subject: [PATCH] Base classes for sample added. --- samples/android/color-blob-detection/.classpath | 1 - samples/android/color-blob-detection/.project | 3 +- .../color-blob-detection/AndroidManifest.xml | 20 ++-- .../color-blob-detection/project.properties | 1 + .../color-blob-detection/res/values/strings.xml | 4 +- .../ColorBlobDetectionActivity.java | 27 +++++ .../colorblobdetect/ColorBlobDetectionView.java | 87 ++++++++++++++++ .../samples/colorblobdetect/SampleCvViewBase.java | 110 +++++++++++++++++++++ 8 files changed, 242 insertions(+), 11 deletions(-) create mode 100644 samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java create mode 100644 samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionView.java create mode 100644 samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/SampleCvViewBase.java diff --git a/samples/android/color-blob-detection/.classpath b/samples/android/color-blob-detection/.classpath index e3c66ae..a4763d1 100644 --- a/samples/android/color-blob-detection/.classpath +++ b/samples/android/color-blob-detection/.classpath @@ -2,7 +2,6 @@ - diff --git a/samples/android/color-blob-detection/.project b/samples/android/color-blob-detection/.project index 72db74d..b0f36b7 100644 --- a/samples/android/color-blob-detection/.project +++ b/samples/android/color-blob-detection/.project @@ -1,9 +1,8 @@ - ColorBlobDetectionTest + ColorBlobDetection - ColorBlobDetection diff --git a/samples/android/color-blob-detection/AndroidManifest.xml b/samples/android/color-blob-detection/AndroidManifest.xml index fb92a86..848225f 100644 --- a/samples/android/color-blob-detection/AndroidManifest.xml +++ b/samples/android/color-blob-detection/AndroidManifest.xml @@ -1,19 +1,27 @@ - - - + + + + + + + + + + + \ No newline at end of file diff --git a/samples/android/color-blob-detection/project.properties b/samples/android/color-blob-detection/project.properties index 85aac54..33a5ee0 100644 --- a/samples/android/color-blob-detection/project.properties +++ b/samples/android/color-blob-detection/project.properties @@ -12,3 +12,4 @@ # Project target. target=android-8 +android.library.reference.1=../../../android/build diff --git a/samples/android/color-blob-detection/res/values/strings.xml b/samples/android/color-blob-detection/res/values/strings.xml index 01fb28b..df79f45 100644 --- a/samples/android/color-blob-detection/res/values/strings.xml +++ b/samples/android/color-blob-detection/res/values/strings.xml @@ -1,7 +1,7 @@ - Hello World! - ColorBlobDetectionTest + Hello World, ColorBlobDetectionActivity! + ColorBlobDetection \ No newline at end of file diff --git a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java new file mode 100644 index 0000000..b02f831 --- /dev/null +++ b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java @@ -0,0 +1,27 @@ +package org.opencv.samples.colorblobdetect; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.Window; + +public class ColorBlobDetectionActivity extends Activity { + + private static final String TAG = "Example/CollorBlobDetection"; + private ColorBlobDetectionView mView; + + public ColorBlobDetectionActivity() + { + 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 ColorBlobDetectionView(this); + setContentView(mView); + } +} \ No newline at end of file diff --git a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionView.java b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionView.java new file mode 100644 index 0000000..74b79cd --- /dev/null +++ b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionView.java @@ -0,0 +1,87 @@ +package org.opencv.samples.colorblobdetect; + +import org.opencv.android.Utils; +import org.opencv.core.Mat; +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 ColorBlobDetectionView extends SampleCvViewBase implements + OnTouchListener { + + private Mat mRgba; + private static final String TAG = "Example/CollorBlobDetection"; + + public ColorBlobDetectionView(Context context) + { + super(context); + setOnTouchListener(this); + } + + @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(); + } + } + + @Override + public boolean onTouch(View v, MotionEvent event) + { + // TODO Auto-generated method stub + int cols = mRgba.cols(); + int rows = mRgba.rows(); + int xoffset = (getWidth() - cols) / 2; + int yoffset = (getHeight() - rows) / 2; + + int x = (int)event.getX() - xoffset; + int y = (int)event.getY() - yoffset; + + double TouchedColor[] = mRgba.get(x,y); + + Log.i(TAG, "Touch coordinates: (" + x + ", " + y + ")"); + Log.i(TAG, "Touched rgba color: (" + TouchedColor[0] + ", " + TouchedColor[1] + + ", " + TouchedColor[2] + ", " + TouchedColor[0] + ",)"); + + return false; // don't need subsequent touch events + } + + @Override + protected Bitmap processFrame(VideoCapture capture) { + // TODO Auto-generated method stub + capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); + + Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); + try { + Utils.matToBitmap(mRgba, bmp); + } catch(Exception e) { + Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage()); + bmp.recycle(); + bmp = null; + } + + return bmp; + } + + @Override + public void run() { + super.run(); + + synchronized (this) { + // Explicitly deallocate Mats + if (mRgba != null) + mRgba.release(); + + mRgba = null; + } + } +} diff --git a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/SampleCvViewBase.java b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/SampleCvViewBase.java new file mode 100644 index 0000000..eab92f5 --- /dev/null +++ b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/SampleCvViewBase.java @@ -0,0 +1,110 @@ +package org.opencv.samples.colorblobdetect; + +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 = "Sample::SurfaceView"; + + 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 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 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"); + } +} \ No newline at end of file -- 2.7.4