Updated Manager for Android
This commit is contained in:
@@ -507,55 +507,6 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Core_n_1minMaxLocManual
|
||||
"moveWindow" : {'j_code' : '', 'jn_code' : '', 'cpp_code' : '' },
|
||||
"resizeWindow" : {'j_code' : '', 'jn_code' : '', 'cpp_code' : '' },
|
||||
}, # Highgui
|
||||
|
||||
'VideoCapture' :
|
||||
{
|
||||
"getSupportedPreviewSizes" :
|
||||
{
|
||||
'j_code' :
|
||||
"""
|
||||
public java.util.List<org.opencv.core.Size> getSupportedPreviewSizes()
|
||||
{
|
||||
String[] sizes_str = getSupportedPreviewSizes_0(nativeObj).split(",");
|
||||
java.util.List<org.opencv.core.Size> sizes = new java.util.ArrayList<org.opencv.core.Size>(sizes_str.length);
|
||||
|
||||
for (String str : sizes_str) {
|
||||
String[] wh = str.split("x");
|
||||
sizes.add(new org.opencv.core.Size(Double.parseDouble(wh[0]), Double.parseDouble(wh[1])));
|
||||
}
|
||||
|
||||
return sizes;
|
||||
}
|
||||
|
||||
""",
|
||||
'jn_code' :
|
||||
"""\n private static native String getSupportedPreviewSizes_0(long nativeObj);\n""",
|
||||
'cpp_code' :
|
||||
"""
|
||||
JNIEXPORT jstring JNICALL Java_org_opencv_videoio_VideoCapture_getSupportedPreviewSizes_10
|
||||
(JNIEnv *env, jclass, jlong self);
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_org_opencv_videoio_VideoCapture_getSupportedPreviewSizes_10
|
||||
(JNIEnv *env, jclass, jlong self)
|
||||
{
|
||||
static const char method_name[] = "videoio::VideoCapture_getSupportedPreviewSizes_10()";
|
||||
try {
|
||||
LOGD("%s", method_name);
|
||||
VideoCapture* me = (VideoCapture*) self; //TODO: check for NULL
|
||||
union {double prop; const char* name;} u;
|
||||
u.prop = me->get(CAP_PROP_ANDROID_PREVIEW_SIZES_STRING);
|
||||
return env->NewStringUTF(u.name);
|
||||
} catch(const std::exception &e) {
|
||||
throwJavaException(env, &e, method_name);
|
||||
} catch (...) {
|
||||
throwJavaException(env, 0, method_name);
|
||||
}
|
||||
return env->NewStringUTF("");
|
||||
}
|
||||
|
||||
""",
|
||||
}, # getSupportedPreviewSizes
|
||||
}, # VideoCapture
|
||||
}
|
||||
|
||||
# { class : { func : { arg_name : {"ctype" : ctype, "attrib" : [attrib]} } } }
|
||||
|
@@ -4,7 +4,7 @@ import java.io.File;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.engine3.OpenCVEngineInterface;
|
||||
import org.opencv.engine.OpenCVEngineInterface;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
@@ -21,8 +21,8 @@ class AsyncServiceHelper
|
||||
final LoaderCallbackInterface Callback)
|
||||
{
|
||||
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
|
||||
Intent intent = new Intent("org.opencv.engine3.BIND");
|
||||
intent.setPackage("org.opencv.engine3");
|
||||
Intent intent = new Intent("org.opencv.engine.BIND");
|
||||
intent.setPackage("org.opencv.engine");
|
||||
if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE))
|
||||
{
|
||||
return true;
|
||||
@@ -151,7 +151,7 @@ class AsyncServiceHelper
|
||||
/**
|
||||
* URL of OpenCV Manager page on Google Play Market.
|
||||
*/
|
||||
protected static final String OPEN_CV_SERVICE_URL = "market://details?id=org.opencv.engine3";
|
||||
protected static final String OPEN_CV_SERVICE_URL = "market://details?id=org.opencv.engine";
|
||||
|
||||
protected ServiceConnection mServiceConnection = new ServiceConnection()
|
||||
{
|
||||
|
@@ -46,7 +46,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
protected int mMaxHeight;
|
||||
protected int mMaxWidth;
|
||||
protected float mScale = 0;
|
||||
protected int mPreviewFormat = Videoio.CV_CAP_ANDROID_COLOR_FRAME_RGBA;
|
||||
protected int mPreviewFormat = RGBA;
|
||||
protected int mCameraIndex = CAMERA_ID_ANY;
|
||||
protected boolean mEnabled;
|
||||
protected FpsMeter mFpsMeter = null;
|
||||
@@ -54,6 +54,8 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
public static final int CAMERA_ID_ANY = -1;
|
||||
public static final int CAMERA_ID_BACK = 99;
|
||||
public static final int CAMERA_ID_FRONT = 98;
|
||||
public static final int RGBA = 1;
|
||||
public static final int GRAY = 2;
|
||||
|
||||
public CameraBridgeViewBase(Context context, int cameraId) {
|
||||
super(context);
|
||||
@@ -151,10 +153,10 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
|
||||
Mat result = null;
|
||||
switch (mPreviewFormat) {
|
||||
case Videoio.CV_CAP_ANDROID_COLOR_FRAME_RGBA:
|
||||
case RGBA:
|
||||
result = mOldStyleListener.onCameraFrame(inputFrame.rgba());
|
||||
break;
|
||||
case Videoio.CV_CAP_ANDROID_GREY_FRAME:
|
||||
case GRAY:
|
||||
result = mOldStyleListener.onCameraFrame(inputFrame.gray());
|
||||
break;
|
||||
default:
|
||||
@@ -168,7 +170,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
mPreviewFormat = format;
|
||||
}
|
||||
|
||||
private int mPreviewFormat = Videoio.CV_CAP_ANDROID_COLOR_FRAME_RGBA;
|
||||
private int mPreviewFormat = RGBA;
|
||||
private CvCameraViewListener mOldStyleListener;
|
||||
};
|
||||
|
||||
@@ -296,6 +298,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
* Called when mSyncObject lock is held
|
||||
*/
|
||||
private void checkCurrentState() {
|
||||
Log.d(TAG, "call checkCurrentState");
|
||||
int targetState;
|
||||
|
||||
if (mEnabled && mSurfaceExist && getVisibility() == VISIBLE) {
|
||||
@@ -313,6 +316,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
}
|
||||
|
||||
private void processEnterState(int state) {
|
||||
Log.d(TAG, "call processEnterState: " + state);
|
||||
switch(state) {
|
||||
case STARTED:
|
||||
onEnterStartedState();
|
||||
@@ -330,6 +334,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
}
|
||||
|
||||
private void processExitState(int state) {
|
||||
Log.d(TAG, "call processExitState: " + state);
|
||||
switch(state) {
|
||||
case STARTED:
|
||||
onExitStartedState();
|
||||
@@ -351,6 +356,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
// NOTE: The order of bitmap constructor and camera connection is important for android 4.1.x
|
||||
// Bitmap must be constructed before surface
|
||||
private void onEnterStartedState() {
|
||||
Log.d(TAG, "call ponEnterStartedState");
|
||||
/* Connect camera */
|
||||
if (!connectCamera(getWidth(), getHeight())) {
|
||||
AlertDialog ad = new AlertDialog.Builder(getContext()).create();
|
||||
|
@@ -1,182 +0,0 @@
|
||||
package org.opencv.android;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.videoio.Videoio;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
|
||||
/**
|
||||
* 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 CameraBridgeViewBase {
|
||||
|
||||
public static final String TAG = "NativeCameraView";
|
||||
private boolean mStopThread;
|
||||
private Thread mThread;
|
||||
|
||||
protected VideoCapture mCamera;
|
||||
protected NativeCameraFrame mFrame;
|
||||
|
||||
public NativeCameraView(Context context, int cameraId) {
|
||||
super(context, cameraId);
|
||||
}
|
||||
|
||||
public NativeCameraView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean 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 */
|
||||
if (!initializeCamera(width, height))
|
||||
return false;
|
||||
|
||||
/* now we can start update thread */
|
||||
mThread = new Thread(new CameraWorker());
|
||||
mThread.start();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disconnectCamera() {
|
||||
/* 1. We need to stop thread which updating the frames
|
||||
* 2. Stop camera and release it
|
||||
*/
|
||||
if (mThread != null) {
|
||||
try {
|
||||
mStopThread = true;
|
||||
mThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
mThread = null;
|
||||
mStopThread = false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Now release camera */
|
||||
releaseCamera();
|
||||
}
|
||||
|
||||
public static class OpenCvSizeAccessor implements ListItemAccessor {
|
||||
|
||||
public int getWidth(Object obj) {
|
||||
Size size = (Size)obj;
|
||||
return (int)size.width;
|
||||
}
|
||||
|
||||
public int getHeight(Object obj) {
|
||||
Size size = (Size)obj;
|
||||
return (int)size.height;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean initializeCamera(int width, int height) {
|
||||
synchronized (this) {
|
||||
|
||||
if (mCameraIndex == -1)
|
||||
mCamera = new VideoCapture(Videoio.CV_CAP_ANDROID);
|
||||
else
|
||||
mCamera = new VideoCapture(Videoio.CV_CAP_ANDROID + mCameraIndex);
|
||||
|
||||
if (mCamera == null)
|
||||
return false;
|
||||
|
||||
if (mCamera.isOpened() == false)
|
||||
return false;
|
||||
|
||||
mFrame = new NativeCameraFrame(mCamera);
|
||||
|
||||
java.util.List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||
|
||||
/* Select the size that fits surface considering maximum size allowed */
|
||||
Size frameSize = calculateCameraFrameSize(sizes, new OpenCvSizeAccessor(), width, height);
|
||||
|
||||
mFrameWidth = (int)frameSize.width;
|
||||
mFrameHeight = (int)frameSize.height;
|
||||
|
||||
if ((getLayoutParams().width == LayoutParams.MATCH_PARENT) && (getLayoutParams().height == LayoutParams.MATCH_PARENT))
|
||||
mScale = Math.min(((float)height)/mFrameHeight, ((float)width)/mFrameWidth);
|
||||
else
|
||||
mScale = 0;
|
||||
|
||||
if (mFpsMeter != null) {
|
||||
mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
|
||||
}
|
||||
|
||||
AllocateCache();
|
||||
|
||||
mCamera.set(Videoio.CV_CAP_PROP_FRAME_WIDTH, frameSize.width);
|
||||
mCamera.set(Videoio.CV_CAP_PROP_FRAME_HEIGHT, frameSize.height);
|
||||
}
|
||||
|
||||
Log.i(TAG, "Selected camera frame size = (" + mFrameWidth + ", " + mFrameHeight + ")");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void releaseCamera() {
|
||||
synchronized (this) {
|
||||
if (mFrame != null) mFrame.release();
|
||||
if (mCamera != null) mCamera.release();
|
||||
}
|
||||
}
|
||||
|
||||
private static class NativeCameraFrame implements CvCameraViewFrame {
|
||||
|
||||
@Override
|
||||
public Mat rgba() {
|
||||
mCapture.retrieve(mRgba, Videoio.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
return mRgba;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mat gray() {
|
||||
mCapture.retrieve(mGray, Videoio.CV_CAP_ANDROID_GREY_FRAME);
|
||||
return mGray;
|
||||
}
|
||||
|
||||
public NativeCameraFrame(VideoCapture capture) {
|
||||
mCapture = capture;
|
||||
mGray = new Mat();
|
||||
mRgba = new Mat();
|
||||
}
|
||||
|
||||
public void release() {
|
||||
if (mGray != null) mGray.release();
|
||||
if (mRgba != null) mRgba.release();
|
||||
}
|
||||
|
||||
private VideoCapture mCapture;
|
||||
private Mat mRgba;
|
||||
private Mat mGray;
|
||||
};
|
||||
|
||||
private class CameraWorker implements Runnable {
|
||||
|
||||
public void run() {
|
||||
do {
|
||||
if (!mCamera.grab()) {
|
||||
Log.e(TAG, "Camera frame grab failed");
|
||||
break;
|
||||
}
|
||||
|
||||
deliverAndDrawFrame(mFrame);
|
||||
} while (!mStopThread);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package org.opencv.engine3;
|
||||
package org.opencv.engine;
|
||||
|
||||
/**
|
||||
* Class provides a Java interface for OpenCV Engine Service. It's synchronous with native OpenCVEngine class.
|
Reference in New Issue
Block a user