我在Android上使用OpenCv,当我运行应用程序时,它让我选择安装OpenCv管理器,我不想在我的应用程序中使用此选项。为此,我跟着this answer from this forum,我按照步骤但仍然要求我提供OpenCv经理。
下面是我的android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=STATIC
include C:\OpenCV-2.4.10-android-sdk\sdk\native\jni\OpenCV.mk
LOCAL_MODULE := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
和属性选项
我添加下面的代码但它崩溃了 @覆盖 public void onResume() { super.onResume(); mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); }
静态初始化后的代码
public class Tutorial2Activity extends Activity implements CvCameraViewListener2 {
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
}
private static final String TAG = "OCVSample::Activity";
private static final int VIEW_MODE_RGBA = 0;
private static final int VIEW_MODE_GRAY = 1;
private static final int VIEW_MODE_CANNY = 2;
private static final int VIEW_MODE_FEATURES = 5;
private int mViewMode;
private Mat mRgba;
private Mat mIntermediateMat;
private Mat mGray;
private MenuItem mItemPreviewRGBA;
private MenuItem mItemPreviewGray;
private MenuItem mItemPreviewCanny;
private MenuItem mItemPreviewFeatures;
private CameraBridgeViewBase mOpenCvCameraView;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Load native library after(!) OpenCV initialization
System.loadLibrary("mixed_sample");
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public Tutorial2Activity() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.tutorial2_surface_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial2_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
}
等等
修改
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
final int viewMode = mViewMode;
switch (viewMode) {
case VIEW_MODE_GRAY:
// input frame has gray scale format
Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
break;
case VIEW_MODE_RGBA:
// input frame has RBGA format
mRgba = inputFrame.rgba();
break;
case VIEW_MODE_CANNY:
// input frame has gray scale format
mRgba = inputFrame.rgba();
Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100);
Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
break;
case VIEW_MODE_FEATURES:
// input frame has RGBA format
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr());
break;
}
return mRgba;
}
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
if (item == mItemPreviewRGBA) {
mViewMode = VIEW_MODE_RGBA;
} else if (item == mItemPreviewGray) {
mViewMode = VIEW_MODE_GRAY;
} else if (item == mItemPreviewCanny) {
mViewMode = VIEW_MODE_CANNY;
} else if (item == mItemPreviewFeatures) {
mViewMode = VIEW_MODE_FEATURES;
}
return true;
}
public native void FindFeatures(long matAddrGr, long matAddrRgba);
答案 0 :(得分:0)
在OpenCV中,您可以执行异步或静态初始化。你想要的是静态初始化。请记住,OpenCV不推荐将其用于生产,如何查看here。
根据相同的链接,我可以看到您的Android.mk
是正确的。显然你忘记了第四步,那就是:
调用OpenCV API之前的Java初始化代码。例如,它可以在Activity类的静态部分中完成:
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
}
如果您的应用程序包含其他依赖于OpenCV的本机库,则应在OpenCV初始化后加载它们:
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
} else {
System.loadLibrary("my_jni_lib1");
System.loadLibrary("my_jni_lib2");
}
}
编辑1
不要忘记删除进行asyn初始化的行,即:
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
并添加以下行:
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
ps:当您的项目带有JNI部分时,您可以编辑Android.mk
文件。对于没有JNI部分的项目,您应该将相应的OpenCV本机库从OpenCV-android-sdk/sdk/native/libs/
复制到项目中的libs/
文件夹。 / p>
编辑2
要使用本机库进行构建,请按照this link
中的说明进行操作尝试最后一步:
在重新构建Java部件和创建APK之前,使用手动ndk-build调用或设置Eclipse CDT Builder来构建本机JNI库。
也就是说,你必须在你的项目根目录中运行ndk-build,这样就会生成你应用程序所需的库。