From 3ef588b877664679d0e28dea81937294a193a5a0 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Thu, 31 Jan 2013 16:13:12 +0400 Subject: [PATCH 1/4] onCameraFrame callback signature changed. CvCameraFame interface added. New interface allows to get one RGBA or Gray frame from camera or both in the same time; New interface fixes data rase in samples also. --- .../java/android+CameraBridgeViewBase.java | 18 +++--- .../src/java/android+JavaCameraView.java | 62 ++++++++++++------- .../src/java/android+NativeCameraView.java | 40 +++++++----- 3 files changed, 75 insertions(+), 45 deletions(-) diff --git a/modules/java/generator/src/java/android+CameraBridgeViewBase.java b/modules/java/generator/src/java/android+CameraBridgeViewBase.java index 06288c94fc..44b97e7611 100644 --- a/modules/java/generator/src/java/android+CameraBridgeViewBase.java +++ b/modules/java/generator/src/java/android+CameraBridgeViewBase.java @@ -6,7 +6,6 @@ import org.opencv.R; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.core.Size; -import org.opencv.highgui.Highgui; import android.app.Activity; import android.app.AlertDialog; @@ -44,7 +43,6 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac protected int mFrameHeight; protected int mMaxHeight; protected int mMaxWidth; - protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; protected int mCameraIndex = -1; protected boolean mEnabled; protected FpsMeter mFpsMeter = null; @@ -91,10 +89,15 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac * The returned values - is a modified frame which needs to be displayed on the screen. * TODO: pass the parameters specifying the format of the frame (BPP, YUV or RGB and etc) */ - public Mat onCameraFrame(Mat inputFrame); + public Mat onCameraFrame(CvCameraViewFrame inputFrame); } + public interface CvCameraViewFrame { + public abstract Mat rgba(); + public abstract Mat gray(); + }; + public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { Log.d(TAG, "call surfaceChanged event"); synchronized(mSyncObject) { @@ -183,11 +186,6 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac mMaxHeight = maxHeight; } - public void SetCaptureFormat(int format) - { - mPreviewFormat = format; - } - /** * Called when mSyncObject lock is held */ @@ -276,13 +274,13 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac * then displayed on the screen. * @param frame - the current frame to be delivered */ - protected void deliverAndDrawFrame(Mat frame) { + protected void deliverAndDrawFrame(CvCameraViewFrame frame) { Mat modified; if (mListener != null) { modified = mListener.onCameraFrame(frame); } else { - modified = frame; + modified = frame.rgba(); } boolean bmpValid = true; diff --git a/modules/java/generator/src/java/android+JavaCameraView.java b/modules/java/generator/src/java/android+JavaCameraView.java index c3d8f564e1..508a650dae 100644 --- a/modules/java/generator/src/java/android+JavaCameraView.java +++ b/modules/java/generator/src/java/android+JavaCameraView.java @@ -16,7 +16,6 @@ import android.view.SurfaceHolder; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Size; -import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; /** @@ -33,7 +32,6 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb private static final int MAGIC_TEXTURE_ID = 10; private static final String TAG = "JavaCameraView"; - private Mat mBaseMat; private byte mBuffer[]; private Mat[] mFrameChain; private int mChainIdx = 0; @@ -41,7 +39,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb private boolean mStopThread; protected Camera mCamera; - + protected JavaCameraFrame mCameraFrame; private SurfaceTexture mSurfaceTexture; public static class JavaCameraSizeAccessor implements ListItemAccessor { @@ -146,14 +144,14 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb mCamera.addCallbackBuffer(mBuffer); mCamera.setPreviewCallbackWithBuffer(this); - mBaseMat = new Mat(mFrameHeight + (mFrameHeight/2), mFrameWidth, CvType.CV_8UC1); - mFrameChain = new Mat[2]; - mFrameChain[0] = new Mat(); - mFrameChain[1] = new Mat(); + mFrameChain[0] = new Mat(mFrameHeight + (mFrameHeight/2), mFrameWidth, CvType.CV_8UC1); + mFrameChain[1] = new Mat(mFrameHeight + (mFrameHeight/2), mFrameWidth, CvType.CV_8UC1); AllocateCache(); + mCameraFrame = new JavaCameraFrame(mFrameChain[mChainIdx], mFrameWidth, mFrameHeight); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mSurfaceTexture = new SurfaceTexture(MAGIC_TEXTURE_ID); getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); @@ -183,12 +181,12 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb mCamera.release(); } mCamera = null; - if (mBaseMat != null) - mBaseMat.release(); if (mFrameChain != null) { mFrameChain[0].release(); mFrameChain[1].release(); } + if (mCameraFrame != null) + mCameraFrame.release(); } } @@ -242,13 +240,45 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb Log.i(TAG, "Frame size is " + frame.length); synchronized (this) { - mBaseMat.put(0, 0, frame); + mFrameChain[1 - mChainIdx].put(0, 0, frame); this.notify(); } if (mCamera != null) mCamera.addCallbackBuffer(mBuffer); } + private class JavaCameraFrame implements CvCameraViewFrame + { + public Mat gray() { + return mYuvFrameData.submat(0, mHeight, 0, mWidth); + } + + public Mat rgba() { + Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2BGR_NV12, 4); + return mRgba; + } + + public JavaCameraFrame(Mat Yuv420sp, int width, int height) { + super(); + mWidth = width; + mHeight = height; + mYuvFrameData = Yuv420sp; + mRgba = new Mat(mHeight, mWidth, CvType.CV_8UC4); + } + + public void release() { + mRgba.release(); + } + + private JavaCameraFrame(CvCameraViewFrame obj) { + } + + private Mat mYuvFrameData; + private Mat mRgba; + private int mWidth; + private int mHeight; + }; + private class CameraWorker implements Runnable { public void run() { @@ -263,18 +293,8 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb } if (!mStopThread) { - switch (mPreviewFormat) { - case Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA: - Imgproc.cvtColor(mBaseMat, mFrameChain[mChainIdx], Imgproc.COLOR_YUV2RGBA_NV21, 4); - break; - case Highgui.CV_CAP_ANDROID_GREY_FRAME: - mFrameChain[mChainIdx] = mBaseMat.submat(0, mFrameHeight, 0, mFrameWidth); - break; - default: - Log.e(TAG, "Invalid frame format! Only RGBA and Gray Scale are supported!"); - }; if (!mFrameChain[mChainIdx].empty()) - deliverAndDrawFrame(mFrameChain[mChainIdx]); + deliverAndDrawFrame(mCameraFrame); mChainIdx = 1 - mChainIdx; } } while (!mStopThread); diff --git a/modules/java/generator/src/java/android+NativeCameraView.java b/modules/java/generator/src/java/android+NativeCameraView.java index dad3cb95f5..7e55d9b377 100644 --- a/modules/java/generator/src/java/android+NativeCameraView.java +++ b/modules/java/generator/src/java/android+NativeCameraView.java @@ -125,6 +125,31 @@ public class NativeCameraView extends CameraBridgeViewBase { } } + private class NativeCameraFrame implements CvCameraViewFrame { + + @Override + public Mat rgba() { + mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); + return mRgba; + } + + @Override + public Mat gray() { + mCamera.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); + return mGray; + } + + public NativeCameraFrame(VideoCapture capture) { + mCapture = capture; + mGray = new Mat(); + mRgba = new Mat(); + } + + private VideoCapture mCapture; + private Mat mRgba; + private Mat mGray; + }; + private class CameraWorker implements Runnable { private Mat mRgba = new Mat(); @@ -137,22 +162,9 @@ public class NativeCameraView extends CameraBridgeViewBase { break; } - switch (mPreviewFormat) { - case Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA: - { - mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); - deliverAndDrawFrame(mRgba); - } break; - case Highgui.CV_CAP_ANDROID_GREY_FRAME: - mCamera.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); - deliverAndDrawFrame(mGray); - break; - default: - Log.e(TAG, "Invalid frame format! Only RGBA and Gray Scale are supported!"); - } + deliverAndDrawFrame(new NativeCameraFrame(mCamera)); } while (!mStopThread); - } } From 6b5eac328fa2bb2207eef8a2d30eb55ea69f751f Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Fri, 1 Feb 2013 12:42:21 +0400 Subject: [PATCH 2/4] Android samples updated according onCameraFrame callback signature change. FpsMeter class removed from Image Manipulations and Face Detection examples as unused code. --- .../samples/puzzle15/Puzzle15Activity.java | 9 ++-- .../ColorBlobDetectionActivity.java | 5 +- .../opencv/samples/facedetect/FdActivity.java | 8 +-- .../opencv/samples/facedetect/FpsMeter.java | 50 ------------------- .../samples/imagemanipulations/FpsMeter.java | 50 ------------------- .../ImageManipulationsActivity.java | 7 +-- .../opencv/samples/tutorial1/Sample1Java.java | 5 +- .../tutorial2/Sample2NativeCamera.java | 15 +++--- .../samples/tutorial3/Sample3Native.java | 16 +++--- .../samples/tutorial4/Sample4Mixed.java | 29 +++++------ .../tutorial5/Sample5CameraControl.java | 5 +- 11 files changed, 49 insertions(+), 150 deletions(-) delete mode 100644 samples/android/face-detection/src/org/opencv/samples/facedetect/FpsMeter.java delete mode 100644 samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java diff --git a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java index d1961964fd..d32c46de26 100644 --- a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java +++ b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java @@ -1,6 +1,7 @@ package org.opencv.samples.puzzle15; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; @@ -109,10 +110,6 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, public void onCameraViewStopped() { } - public Mat onCameraFrame(Mat inputFrame) { - return mPuzzle15.puzzleFrame(inputFrame); - } - public boolean onTouch(View view, MotionEvent event) { int xpos, ypos; @@ -129,4 +126,8 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, return false; } + + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + return mPuzzle15.puzzleFrame(inputFrame.rgba()); + } } 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 index 13ffc685d7..c7ac9a2ae0 100644 --- 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 @@ -3,6 +3,7 @@ package org.opencv.samples.colorblobdetect; import java.util.List; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Core; @@ -160,8 +161,8 @@ public class ColorBlobDetectionActivity extends Activity implements OnTouchListe return false; // don't need subsequent touch events } - public Mat onCameraFrame(Mat inputFrame) { - inputFrame.copyTo(mRgba); + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + mRgba = inputFrame.rgba(); if (mIsColorSelected) { mDetector.process(mRgba); diff --git a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java index d59de02609..333d08bfb3 100644 --- a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java +++ b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Core; @@ -16,7 +17,6 @@ import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; -import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import android.app.Activity; @@ -157,10 +157,10 @@ public class FdActivity extends Activity implements CvCameraViewListener { mRgba.release(); } - public Mat onCameraFrame(Mat inputFrame) { + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { - inputFrame.copyTo(mRgba); - Imgproc.cvtColor(inputFrame, mGray, Imgproc.COLOR_RGBA2GRAY); + mRgba = inputFrame.rgba(); + mGray = inputFrame.gray(); if (mAbsoluteFaceSize == 0) { int height = mGray.rows(); diff --git a/samples/android/face-detection/src/org/opencv/samples/facedetect/FpsMeter.java b/samples/android/face-detection/src/org/opencv/samples/facedetect/FpsMeter.java deleted file mode 100644 index 2438bf89ac..0000000000 --- a/samples/android/face-detection/src/org/opencv/samples/facedetect/FpsMeter.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.opencv.samples.facedetect; - -import java.text.DecimalFormat; - -import org.opencv.core.Core; - -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.util.Log; - -public class FpsMeter { - private static final String TAG = "OCVSample::FpsMeter"; - int step; - int framesCouner; - double freq; - long prevFrameTime; - String strfps; - DecimalFormat twoPlaces = new DecimalFormat("0.00"); - Paint paint; - - public void init() { - step = 20; - framesCouner = 0; - freq = Core.getTickFrequency(); - prevFrameTime = Core.getTickCount(); - strfps = ""; - - paint = new Paint(); - paint.setColor(Color.BLUE); - paint.setTextSize(50); - } - - public void measure() { - framesCouner++; - if (framesCouner % step == 0) { - long time = Core.getTickCount(); - double fps = step * freq / (time - prevFrameTime); - prevFrameTime = time; - DecimalFormat twoPlaces = new DecimalFormat("0.00"); - strfps = twoPlaces.format(fps) + " FPS"; - Log.i(TAG, strfps); - } - } - - public void draw(Canvas canvas, float offsetx, float offsety) { - canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint); - } - -} diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java deleted file mode 100644 index 2f802366cd..0000000000 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/FpsMeter.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.opencv.samples.imagemanipulations; - -import java.text.DecimalFormat; - -import org.opencv.core.Core; - -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.util.Log; - -public class FpsMeter { - private static final String TAG = "OCVSample::FpsMeter"; - int step; - int framesCouner; - double freq; - long prevFrameTime; - String strfps; - DecimalFormat twoPlaces = new DecimalFormat("0.00"); - Paint paint; - - public void init() { - step = 20; - framesCouner = 0; - freq = Core.getTickFrequency(); - prevFrameTime = Core.getTickCount(); - strfps = ""; - - paint = new Paint(); - paint.setColor(Color.BLUE); - paint.setTextSize(50); - } - - public void measure() { - framesCouner++; - if (framesCouner % step == 0) { - long time = Core.getTickCount(); - double fps = step * freq / (time - prevFrameTime); - prevFrameTime = time; - DecimalFormat twoPlaces = new DecimalFormat("0.00"); - strfps = twoPlaces.format(fps) + " FPS"; - Log.i(TAG, strfps); - } - } - - public void draw(Canvas canvas, float offsetx, float offsety) { - canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint); - } - -} diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java index a66145c92a..513e9b770c 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java @@ -3,6 +3,7 @@ package org.opencv.samples.imagemanipulations; import java.util.Arrays; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Core; @@ -258,8 +259,8 @@ public class ImageManipulationsActivity extends Activity implements CvCameraView mZoomWindow = null; } - public Mat onCameraFrame(Mat inputFrame) { - inputFrame.copyTo(mRgba); + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + mRgba = inputFrame.rgba(); switch (ImageManipulationsActivity.viewMode) { case ImageManipulationsActivity.VIEW_MODE_RGBA: @@ -315,7 +316,7 @@ public class ImageManipulationsActivity extends Activity implements CvCameraView break; case ImageManipulationsActivity.VIEW_MODE_SOBEL: - Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY); + mGray = inputFrame.gray(); if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height)) CreateAuxiliaryMats(); diff --git a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java index 9e5150f6cb..65e7cd7897 100644 --- a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java +++ b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java @@ -1,6 +1,7 @@ package org.opencv.samples.tutorial1; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; @@ -124,7 +125,7 @@ public class Sample1Java extends Activity implements CvCameraViewListener { public void onCameraViewStopped() { } - public Mat onCameraFrame(Mat inputFrame) { - return inputFrame; + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + return inputFrame.rgba(); } } diff --git a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java index 901e8dd1c5..167d5e027a 100644 --- a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java +++ b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java @@ -1,6 +1,7 @@ package org.opencv.samples.tutorial2; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Core; @@ -10,7 +11,6 @@ import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; -import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import android.app.Activity; @@ -101,20 +101,20 @@ public class Sample2NativeCamera extends Activity implements CvCameraViewListene mIntermediateMat.release(); } - public Mat onCameraFrame(Mat inputFrame) { + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { switch (Sample2NativeCamera.viewMode) { case Sample2NativeCamera.VIEW_MODE_GRAY: { - Imgproc.cvtColor(inputFrame, mRgba, Imgproc.COLOR_GRAY2RGBA, 4); + Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA, 4); } break; case Sample2NativeCamera.VIEW_MODE_RGBA: { - inputFrame.copyTo(mRgba); - Core.putText(mRgba, "OpenCV+Android", new Point(10, inputFrame.rows() - 10), 3, 1, new Scalar(255, 0, 0, 255), 2); + mRgba = inputFrame.rgba(); + Core.putText(mRgba, "OpenCV+Android", new Point(10, mRgba.rows() - 10), 3, 1, new Scalar(255, 0, 0, 255), 2); } break; case Sample2NativeCamera.VIEW_MODE_CANNY: { - Imgproc.Canny(inputFrame, mIntermediateMat, 80, 100); + Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100); Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4); } break; } @@ -136,17 +136,14 @@ public class Sample2NativeCamera extends Activity implements CvCameraViewListene Log.i(TAG, "called onOptionsItemSelected; selected item: " + item); if (item == mItemPreviewRGBA) { - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); viewMode = VIEW_MODE_RGBA; } else if (item == mItemPreviewGray) { - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_GREY_FRAME); viewMode = VIEW_MODE_GRAY; } else if (item == mItemPreviewCanny) { - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_GREY_FRAME); viewMode = VIEW_MODE_CANNY; } diff --git a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java index ea7998edc4..50baab1ce8 100644 --- a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java +++ b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java @@ -1,13 +1,13 @@ package org.opencv.samples.tutorial3; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; -import org.opencv.imgproc.Imgproc; import android.app.Activity; import android.os.Bundle; @@ -18,7 +18,7 @@ public class Sample3Native extends Activity implements CvCameraViewListener { private static final String TAG = "OCVSample::Activity"; private Mat mRgba; - private Mat mGrayMat; + private Mat mGray; private CameraBridgeViewBase mOpenCvCameraView; private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @@ -82,18 +82,18 @@ public class Sample3Native extends Activity implements CvCameraViewListener { public void onCameraViewStarted(int width, int height) { mRgba = new Mat(height, width, CvType.CV_8UC4); - mGrayMat = new Mat(height, width, CvType.CV_8UC1); + mGray = new Mat(height, width, CvType.CV_8UC1); } public void onCameraViewStopped() { mRgba.release(); - mGrayMat.release(); + mGray.release(); } - public Mat onCameraFrame(Mat inputFrame) { - inputFrame.copyTo(mRgba); - Imgproc.cvtColor(mRgba, mGrayMat, Imgproc.COLOR_RGBA2GRAY); - FindFeatures(mGrayMat.getNativeObjAddr(), mRgba.getNativeObjAddr()); + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + mRgba = inputFrame.rgba(); + mGray = inputFrame.gray(); + FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr()); return mRgba; } diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java index a1b455b680..d16b7355d8 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java @@ -1,13 +1,13 @@ package org.opencv.samples.tutorial4; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; -import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import android.app.Activity; @@ -28,7 +28,7 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener { private int mViewMode; private Mat mRgba; private Mat mIntermediateMat; - private Mat mGrayMat; + private Mat mGray; private MenuItem mItemPreviewRGBA; private MenuItem mItemPreviewGray; @@ -109,37 +109,38 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener { public void onCameraViewStarted(int width, int height) { mRgba = new Mat(height, width, CvType.CV_8UC4); mIntermediateMat = new Mat(height, width, CvType.CV_8UC4); - mGrayMat = new Mat(height, width, CvType.CV_8UC1); + mGray = new Mat(height, width, CvType.CV_8UC1); } public void onCameraViewStopped() { mRgba.release(); - mGrayMat.release(); + mGray.release(); mIntermediateMat.release(); } - public Mat onCameraFrame(Mat inputFrame) { + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { final int viewMode = mViewMode; - switch (viewMode) { case VIEW_MODE_GRAY: // input frame has gray scale format - Imgproc.cvtColor(inputFrame, mRgba, Imgproc.COLOR_GRAY2RGBA, 4); + Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA, 4); break; case VIEW_MODE_RGBA: // input frame has RBGA format - inputFrame.copyTo(mRgba); + mRgba = inputFrame.rgba(); break; case VIEW_MODE_CANNY: // input frame has gray scale format - Imgproc.Canny(inputFrame, mIntermediateMat, 80, 100); + mRgba = inputFrame.rgba(); + Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100); Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4); break; case VIEW_MODE_FEATURES: // input frame has RGBA format - inputFrame.copyTo(mRgba); - Imgproc.cvtColor(mRgba, mGrayMat, Imgproc.COLOR_RGBA2GRAY); - FindFeatures(mGrayMat.getNativeObjAddr(), mRgba.getNativeObjAddr()); + mRgba = inputFrame.rgba(); + mGray = inputFrame.gray(); + Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY); + FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr()); break; } @@ -150,17 +151,13 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener { Log.i(TAG, "called onOptionsItemSelected; selected item: " + item); if (item == mItemPreviewRGBA) { - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); mViewMode = VIEW_MODE_RGBA; } else if (item == mItemPreviewGray) { - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_GREY_FRAME); mViewMode = VIEW_MODE_GRAY; } else if (item == mItemPreviewCanny) { - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_GREY_FRAME); mViewMode = VIEW_MODE_CANNY; } else if (item == mItemPreviewFeatures) { mViewMode = VIEW_MODE_FEATURES; - mOpenCvCameraView.SetCaptureFormat(Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); } return true; diff --git a/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java b/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java index 46d038a0e6..9c2f66dde7 100644 --- a/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java +++ b/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.ListIterator; import org.opencv.android.BaseLoaderCallback; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; @@ -102,8 +103,8 @@ public class Sample5CameraControl extends Activity implements CvCameraViewListen public void onCameraViewStopped() { } - public Mat onCameraFrame(Mat inputFrame) { - return inputFrame; + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + return inputFrame.rgba(); } @Override From f8720ec60eabeaa10eb36cc01e42dc81a7b2e96d Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Thu, 7 Feb 2013 13:01:26 +0400 Subject: [PATCH 3/4] Code review issues fixed. Compatibility issues fixed. --- .../java/android+CameraBridgeViewBase.java | 86 ++++++++++++++++++- .../samples/puzzle15/Puzzle15Activity.java | 4 +- .../ColorBlobDetectionActivity.java | 4 +- .../opencv/samples/facedetect/FdActivity.java | 4 +- .../ImageManipulationsActivity.java | 4 +- .../opencv/samples/tutorial1/Sample1Java.java | 4 +- .../tutorial2/Sample2NativeCamera.java | 4 +- .../samples/tutorial3/Sample3Native.java | 4 +- .../samples/tutorial4/Sample4Mixed.java | 7 +- .../tutorial5/Sample5CameraControl.java | 4 +- 10 files changed, 101 insertions(+), 24 deletions(-) diff --git a/modules/java/generator/src/java/android+CameraBridgeViewBase.java b/modules/java/generator/src/java/android+CameraBridgeViewBase.java index 44b97e7611..2b25e1d03f 100644 --- a/modules/java/generator/src/java/android+CameraBridgeViewBase.java +++ b/modules/java/generator/src/java/android+CameraBridgeViewBase.java @@ -6,6 +6,7 @@ import org.opencv.R; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.core.Size; +import org.opencv.highgui.Highgui; import android.app.Activity; import android.app.AlertDialog; @@ -35,7 +36,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac private int mState = STOPPED; private Bitmap mCacheBitmap; - private CvCameraViewListener mListener; + private CvCameraViewListener2 mListener; private boolean mSurfaceExist; private Object mSyncObject = new Object(); @@ -43,6 +44,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac protected int mFrameHeight; protected int mMaxHeight; protected int mMaxWidth; + protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; protected int mCameraIndex = -1; protected boolean mEnabled; protected FpsMeter mFpsMeter = null; @@ -89,10 +91,71 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac * The returned values - is a modified frame which needs to be displayed on the screen. * TODO: pass the parameters specifying the format of the frame (BPP, YUV or RGB and etc) */ - public Mat onCameraFrame(CvCameraViewFrame inputFrame); - + public Mat onCameraFrame(Mat inputFrame); } + public interface CvCameraViewListener2 { + /** + * 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. + * TODO: pass the parameters specifying the format of the frame (BPP, YUV or RGB and etc) + */ + public Mat onCameraFrame(CvCameraViewFrame inputFrame); + }; + + protected class CvCameraViewListenerAdapter implements CvCameraViewListener2 { + public CvCameraViewListenerAdapter(CvCameraViewListener oldStypeListener) { + mOldStyleListener = oldStypeListener; + } + + public void onCameraViewStarted(int width, int height) { + mOldStyleListener.onCameraViewStarted(width, height); + } + + public void onCameraViewStopped() { + mOldStyleListener.onCameraViewStopped(); + } + + public Mat onCameraFrame(CvCameraViewFrame inputFrame) { + Mat result = null; + switch (mPreviewFormat) { + case Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA: + result = mOldStyleListener.onCameraFrame(inputFrame.rgba()); + break; + case Highgui.CV_CAP_ANDROID_GREY_FRAME: + result = mOldStyleListener.onCameraFrame(inputFrame.gray()); + break; + default: + Log.e(TAG, "Invalid frame format! Only RGBA and Gray Scale are supported!"); + }; + + return result; + } + + public void setFrameFormat(int format) { + mPreviewFormat = format; + } + + private CvCameraViewListenerAdapter() {} + + private int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; + private CvCameraViewListener mOldStyleListener; + }; + public interface CvCameraViewFrame { public abstract Mat rgba(); public abstract Mat gray(); @@ -168,10 +231,16 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac * @param listener */ - public void setCvCameraViewListener(CvCameraViewListener listener) { + public void setCvCameraViewListener(CvCameraViewListener2 listener) { mListener = listener; } + public void setCvCameraViewListener(CvCameraViewListener listener) { + CvCameraViewListenerAdapter adapter = new CvCameraViewListenerAdapter(listener); + adapter.setFrameFormat(mPreviewFormat); + mListener = adapter; + } + /** * 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. @@ -186,6 +255,15 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac mMaxHeight = maxHeight; } + public void SetCaptureFormat(int format) + { + mPreviewFormat = format; + if (mListener instanceof CvCameraViewListenerAdapter) { + CvCameraViewListenerAdapter adapter = (CvCameraViewListenerAdapter) mListener; + adapter.setFrameFormat(mPreviewFormat); + } + } + /** * Called when mSyncObject lock is held */ diff --git a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java index d32c46de26..15c8b8ff53 100644 --- a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java +++ b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java @@ -6,7 +6,7 @@ import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import android.os.Bundle; import android.app.Activity; @@ -17,7 +17,7 @@ import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; -public class Puzzle15Activity extends Activity implements CvCameraViewListener, View.OnTouchListener { +public class Puzzle15Activity extends Activity implements CvCameraViewListener2, View.OnTouchListener { private static final String TAG = "Sample::Puzzle15::Activity"; 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 index c7ac9a2ae0..f6be4b93e2 100644 --- 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 @@ -14,7 +14,7 @@ import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.imgproc.Imgproc; import android.app.Activity; @@ -26,7 +26,7 @@ import android.view.Window; import android.view.WindowManager; import android.view.View.OnTouchListener; -public class ColorBlobDetectionActivity extends Activity implements OnTouchListener, CvCameraViewListener { +public class ColorBlobDetectionActivity extends Activity implements OnTouchListener, CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; private boolean mIsColorSelected = false; diff --git a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java index 333d08bfb3..a9dfab06a3 100644 --- a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java +++ b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java @@ -16,7 +16,7 @@ import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.objdetect.CascadeClassifier; import android.app.Activity; @@ -27,7 +27,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager; -public class FdActivity extends Activity implements CvCameraViewListener { +public class FdActivity extends Activity implements CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255); diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java index 513e9b770c..52f77db2a6 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java @@ -15,7 +15,7 @@ import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.imgproc.Imgproc; import android.app.Activity; @@ -25,7 +25,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager; -public class ImageManipulationsActivity extends Activity implements CvCameraViewListener { +public class ImageManipulationsActivity extends Activity implements CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; public static final int VIEW_MODE_RGBA = 0; diff --git a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java index 65e7cd7897..1a1f06fadd 100644 --- a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java +++ b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java @@ -6,7 +6,7 @@ import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import android.app.Activity; import android.os.Bundle; @@ -17,7 +17,7 @@ import android.view.SurfaceView; import android.view.WindowManager; import android.widget.Toast; -public class Sample1Java extends Activity implements CvCameraViewListener { +public class Sample1Java extends Activity implements CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; private CameraBridgeViewBase mOpenCvCameraView; diff --git a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java index 167d5e027a..45c3b37cb4 100644 --- a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java +++ b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java @@ -10,7 +10,7 @@ import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.imgproc.Imgproc; import android.app.Activity; @@ -20,7 +20,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager; -public class Sample2NativeCamera extends Activity implements CvCameraViewListener { +public class Sample2NativeCamera extends Activity implements CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; public static final int VIEW_MODE_RGBA = 0; diff --git a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java index 50baab1ce8..ded920f0fd 100644 --- a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java +++ b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java @@ -7,14 +7,14 @@ import org.opencv.android.OpenCVLoader; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.WindowManager; -public class Sample3Native extends Activity implements CvCameraViewListener { +public class Sample3Native extends Activity implements CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; private Mat mRgba; diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java index d16b7355d8..ddae246b08 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java @@ -7,7 +7,7 @@ import org.opencv.android.OpenCVLoader; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.imgproc.Imgproc; import android.app.Activity; @@ -17,7 +17,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager; -public class Sample4Mixed extends Activity implements CvCameraViewListener { +public class Sample4Mixed extends Activity implements CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; private static final int VIEW_MODE_RGBA = 0; @@ -133,13 +133,12 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener { // input frame has gray scale format mRgba = inputFrame.rgba(); Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100); - Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4); + Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4); break; case VIEW_MODE_FEATURES: // input frame has RGBA format mRgba = inputFrame.rgba(); mGray = inputFrame.gray(); - Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY); FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr()); break; } diff --git a/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java b/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java index 9c2f66dde7..7eba7a345b 100644 --- a/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java +++ b/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java @@ -10,7 +10,7 @@ import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import android.annotation.SuppressLint; import android.app.Activity; @@ -28,7 +28,7 @@ import android.view.View.OnTouchListener; import android.view.WindowManager; import android.widget.Toast; -public class Sample5CameraControl extends Activity implements CvCameraViewListener, OnTouchListener { +public class Sample5CameraControl extends Activity implements CvCameraViewListener2, OnTouchListener { private static final String TAG = "OCVSample::Activity"; private SampleJavaCameraView mOpenCvCameraView; From 076941bb079865d58fa009c53c7c3b838b73668a Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Fri, 8 Feb 2013 09:43:21 +0400 Subject: [PATCH 4/4] 15-puzzle app reverted to usage of old CvCameraViewListener implementation; super.onPause() call moved to begining of onPause method according Google recomandations. --- .../generator/src/java/android+JavaCameraView.java | 2 +- .../org/opencv/samples/puzzle15/Puzzle15Activity.java | 11 +++++------ .../colorblobdetect/ColorBlobDetectionActivity.java | 2 +- .../src/org/opencv/samples/facedetect/FdActivity.java | 2 +- .../ImageManipulationsActivity.java | 2 +- .../src/org/opencv/samples/tutorial1/Sample1Java.java | 2 +- .../opencv/samples/tutorial2/Sample2NativeCamera.java | 2 +- .../org/opencv/samples/tutorial3/Sample3Native.java | 2 +- .../org/opencv/samples/tutorial4/Sample4Mixed.java | 2 +- .../samples/tutorial5/Sample5CameraControl.java | 2 +- 10 files changed, 14 insertions(+), 15 deletions(-) diff --git a/modules/java/generator/src/java/android+JavaCameraView.java b/modules/java/generator/src/java/android+JavaCameraView.java index 508a650dae..b545c241ba 100644 --- a/modules/java/generator/src/java/android+JavaCameraView.java +++ b/modules/java/generator/src/java/android+JavaCameraView.java @@ -263,7 +263,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb mWidth = width; mHeight = height; mYuvFrameData = Yuv420sp; - mRgba = new Mat(mHeight, mWidth, CvType.CV_8UC4); + mRgba = new Mat(); } public void release() { diff --git a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java index 15c8b8ff53..466400d873 100644 --- a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java +++ b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java @@ -1,12 +1,11 @@ package org.opencv.samples.puzzle15; import org.opencv.android.BaseLoaderCallback; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; import org.opencv.android.CameraBridgeViewBase; -import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; +import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener; import android.os.Bundle; import android.app.Activity; @@ -17,7 +16,7 @@ import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; -public class Puzzle15Activity extends Activity implements CvCameraViewListener2, View.OnTouchListener { +public class Puzzle15Activity extends Activity implements CvCameraViewListener, View.OnTouchListener { private static final String TAG = "Sample::Puzzle15::Activity"; @@ -64,9 +63,9 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener2, @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override @@ -127,7 +126,7 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener2, return false; } - public Mat onCameraFrame(CvCameraViewFrame inputFrame) { - return mPuzzle15.puzzleFrame(inputFrame.rgba()); + public Mat onCameraFrame(Mat inputFrame) { + return mPuzzle15.puzzleFrame(inputFrame); } } 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 index f6be4b93e2..e6e1c036c4 100644 --- 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 @@ -79,9 +79,9 @@ public class ColorBlobDetectionActivity extends Activity implements OnTouchListe @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java index a9dfab06a3..23727739e4 100644 --- a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java +++ b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java @@ -130,9 +130,9 @@ public class FdActivity extends Activity implements CvCameraViewListener2 { @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java index 52f77db2a6..f0556976bd 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java @@ -112,9 +112,9 @@ public class ImageManipulationsActivity extends Activity implements CvCameraView @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java index 1a1f06fadd..1fbac34fef 100644 --- a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java +++ b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1Java.java @@ -67,9 +67,9 @@ public class Sample1Java extends Activity implements CvCameraViewListener2 { @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java index 45c3b37cb4..3a19ffd5e4 100644 --- a/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java +++ b/samples/android/tutorial-2-opencvcamera/src/org/opencv/samples/tutorial2/Sample2NativeCamera.java @@ -73,9 +73,9 @@ public class Sample2NativeCamera extends Activity implements CvCameraViewListene @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java index ded920f0fd..71990fab86 100644 --- a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java +++ b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3Native.java @@ -62,9 +62,9 @@ public class Sample3Native extends Activity implements CvCameraViewListener2 { @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java index ddae246b08..97a563be4c 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/Sample4Mixed.java @@ -88,9 +88,9 @@ public class Sample4Mixed extends Activity implements CvCameraViewListener2 { @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override diff --git a/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java b/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java index 7eba7a345b..f19580b496 100644 --- a/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java +++ b/samples/android/tutorial-5-cameracontrol/src/org/opencv/samples/tutorial5/Sample5CameraControl.java @@ -79,9 +79,9 @@ public class Sample5CameraControl extends Activity implements CvCameraViewListen @Override public void onPause() { + super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); - super.onPause(); } @Override