Commit cc3430db authored by Alexander Smorkalov's avatar Alexander Smorkalov Committed by Andrey Kamaev

Naming fixed;

Warnings fixed;
Nexus 7 support added to manifest.
parent cd152c3a
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
android:versionCode="1" android:versionCode="1"
android:versionName="1.0" > android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.CAMERA" required="false"/>
<uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera" required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" /> <uses-feature android:name="android.hardware.camera.autofocus" required="false"/>
<uses-sdk android:minSdkVersion="8" /> <uses-sdk android:minSdkVersion="8" />
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/AppTheme" > android:theme="@style/AppTheme" >
<activity <activity
android:name=".CameraWriterActivity" android:name=".PreviewActivity"
android:label="@string/title_activity_camera_writer" > android:label="@string/title_activity_camera_writer" >
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" > android:layout_height="match_parent" >
<view class="org.opencv.test.camerawriter.OpenCvNativeCameraView" <view class="org.opencv.test.camerawriter.NativeCameraView"
android:id="@+id/camera_surface_view" android:id="@+id/camera_surface_view"
android:layout_width = "fill_parent" android:layout_width = "fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
/> />
......
...@@ -5,8 +5,6 @@ import java.util.List; ...@@ -5,8 +5,6 @@ import java.util.List;
import org.opencv.android.Utils; import org.opencv.android.Utils;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.core.Size; import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
...@@ -23,7 +21,7 @@ import android.view.SurfaceView; ...@@ -23,7 +21,7 @@ import android.view.SurfaceView;
* frame to the screen. * frame to the screen.
* The clients shall implement CvCameraViewListener * The clients shall implement CvCameraViewListener
*/ */
public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements SurfaceHolder.Callback { public abstract class CameraViewBase extends SurfaceView implements SurfaceHolder.Callback {
private static final int MAX_UNSPECIFIED = -1; private static final int MAX_UNSPECIFIED = -1;
...@@ -36,8 +34,9 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements ...@@ -36,8 +34,9 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements
private Bitmap mCacheBitmap; private Bitmap mCacheBitmap;
public OpenCvCameraBridgeViewBase(Context context, AttributeSet attrs) { public CameraViewBase(Context context, AttributeSet attrs) {
super(context,attrs); super(context,attrs);
Log.d(TAG, "call CameraViewBase constructor");
getHolder().addCallback(this); getHolder().addCallback(this);
mMaxWidth = MAX_UNSPECIFIED; mMaxWidth = MAX_UNSPECIFIED;
mMaxHeight = MAX_UNSPECIFIED; mMaxHeight = MAX_UNSPECIFIED;
...@@ -113,7 +112,7 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements ...@@ -113,7 +112,7 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements
/** /**
* This method is provided for clients, so they can enable the camera connection. * This method is provided for clients, so they can enable the camera connection.
* The actuall onCameraViewStarted callback will be delivered only after both this method is called and surface is available * The actual onCameraViewStarted callback will be delivered only after both this method is called and surface is available
*/ */
public void enableView() { public void enableView() {
synchronized(mSyncObject) { synchronized(mSyncObject) {
...@@ -124,7 +123,7 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements ...@@ -124,7 +123,7 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements
/** /**
* This method is provided for clients, so they can disable camera connection and stop * This method is provided for clients, so they can disable camera connection and stop
* the delivery of frames eventhough the surfaceview itself is not destroyed and still stays on the scren * the delivery of frames even though the surface view itself is not destroyed and still stays on the scren
*/ */
public void disableView() { public void disableView() {
synchronized(mSyncObject) { synchronized(mSyncObject) {
...@@ -143,9 +142,9 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements ...@@ -143,9 +142,9 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements
* size - the biggest size which less or equal the size set will be selected. * 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 * As an example - we set setMaxFrameSize(200,200) and we have 176x152 and 320x240 sizes. The
* preview frame will be selected with 176x152 size. * preview frame will be selected with 176x152 size.
* This method is usefull when need to restrict the size of preview frame for some reason (for example for video recording) * 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 maxWidth - the maximum width allowed for camera frame.
* @param maxHeight - the maxumum height allowed for camera frame * @param maxHeight - the maximum height allowed for camera frame
*/ */
public void setMaxFrameSize(int maxWidth, int maxHeight) { public void setMaxFrameSize(int maxWidth, int maxHeight) {
mMaxWidth = maxWidth; mMaxWidth = maxWidth;
...@@ -212,7 +211,7 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements ...@@ -212,7 +211,7 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements
private void onEnterStartedState() { private void onEnterStartedState() {
connectCamera(getWidth(), getHeight()); connectCamera(getWidth(), getHeight());
/* Now create cahe Bitmap */ /* Now create cache Bitmap */
mCacheBitmap = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888); mCacheBitmap = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
} }
...@@ -249,7 +248,8 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements ...@@ -249,7 +248,8 @@ public abstract class OpenCvCameraBridgeViewBase extends SurfaceView implements
if (mCacheBitmap != null) { if (mCacheBitmap != null) {
Canvas canvas = getHolder().lockCanvas(); Canvas canvas = getHolder().lockCanvas();
if (canvas != null) { if (canvas != null) {
canvas.drawBitmap(mCacheBitmap, (canvas.getWidth() - mCacheBitmap.getWidth()) / 2, (canvas.getHeight() - mCacheBitmap.getHeight()) / 2, 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); getHolder().unlockCanvasAndPost(canvas);
} }
} }
......
package org.opencv.test.camerawriter; package org.opencv.test.camerawriter;
import org.opencv.android.Utils;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.core.Size; import org.opencv.core.Size;
import org.opencv.highgui.Highgui; import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture; import org.opencv.highgui.VideoCapture;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
...@@ -17,54 +14,54 @@ import android.util.Log; ...@@ -17,54 +14,54 @@ import android.util.Log;
* Due to the big amount of work done, by the base class this child is only responsible * 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 * for creating camera, destroying camera and delivering frames while camera is enabled
*/ */
public class OpenCvNativeCameraView extends OpenCvCameraBridgeViewBase { public class NativeCameraView extends CameraViewBase {
public static final String TAG = "OpenCvNativeCameraView"; public static final String TAG = "OpenCvNativeCameraView";
private boolean mStopThread; private boolean mStopThread;
private Thread mThread; private Thread mThread;
private VideoCapture mCamera; private VideoCapture mCamera;
public OpenCvNativeCameraView(Context context, AttributeSet attrs) { public NativeCameraView(Context context, AttributeSet attrs) {
super(context, 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(getWidth(), getHeight()));
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();
} @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) { }
private void initializeCamera(int width, int height) {
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID); mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
//TODO: improve error handling //TODO: improve error handling
...@@ -85,41 +82,29 @@ public class OpenCvNativeCameraView extends OpenCvCameraBridgeViewBase { ...@@ -85,41 +82,29 @@ public class OpenCvNativeCameraView extends OpenCvCameraBridgeViewBase {
mFrameHeight = (int)frameHeight; mFrameHeight = (int)frameHeight;
Log.i(TAG, "Selected camera frame size = (" + mFrameWidth + ", " + mFrameHeight + ")"); Log.i(TAG, "Selected camera frame size = (" + mFrameWidth + ", " + mFrameHeight + ")");
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
} }
}
private class CameraWorker implements Runnable {
private Mat mRgba = new Mat(); private void releaseCamera() {
private int mWidth; if (mCamera != null) {
private int mHeight; mCamera.release();
}
CameraWorker(int w, int h) {
mWidth = w;
mHeight = h;
} }
public void run() { private class CameraWorker implements Runnable {
Mat modified; 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);
do { } while (!mStopThread);
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();
} }
}
} }
...@@ -4,59 +4,57 @@ import org.opencv.android.BaseLoaderCallback; ...@@ -4,59 +4,57 @@ import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface; 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.test.camerawriter.OpenCvCameraBridgeViewBase.CvCameraViewListener; import org.opencv.test.camerawriter.CameraViewBase.CvCameraViewListener;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;
import android.util.Log; import android.util.Log;
public class CameraWriterActivity extends Activity implements CvCameraViewListener { public class PreviewActivity extends Activity implements CvCameraViewListener {
protected static final String TAG = "CameraWriterActivity"; protected static final String TAG = "CameraWriterActivity";
private CameraViewBase mCameraView;
private OpenCvCameraBridgeViewBase mCameraView;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override @Override
public void onManagerConnected(int status) { public void onManagerConnected(int status) {
switch (status) { switch (status) {
case LoaderCallbackInterface.SUCCESS: case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully"); Log.i(TAG, "OpenCV loaded successfully");
// Create and set View // Create and set View
mCameraView.setMaxFrameSize(640, 480); mCameraView.setMaxFrameSize(640, 480);
mCameraView.enableView(); mCameraView.enableView();
break; break;
default: default:
super.onManagerConnected(status); super.onManagerConnected(status);
break; break;
} }
} }
}; };
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_writer); setContentView(R.layout.activity_camera_writer);
mCameraView = (OpenCvCameraBridgeViewBase)findViewById(R.id.camera_surface_view); mCameraView = (CameraViewBase)findViewById(R.id.camera_surface_view);
mCameraView.setCvCameraViewListener(this); mCameraView.setCvCameraViewListener(this);
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback);
} }
public void onCameraViewStarted(int width, int height) { public void onCameraViewStarted(int width, int height) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
public void onCameraViewStopped() { public void onCameraViewStopped() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
public Mat onCameraFrame(Mat inputFrame) { public Mat onCameraFrame(Mat inputFrame) {
return inputFrame; return inputFrame;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment