如何在没有OpenCv Manager的情况下运行OpenCV代码 [英] How to run OpenCV code without OpenCv Manager

查看:86
本文介绍了如何在没有OpenCv Manager的情况下运行OpenCV代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV4Android 2.4.10版,并且在Samsung Galayx GT-I9300上测试了我的代码.我的问题是,我必须从Play商店下载Opencv Manager,这样我的opencv代码才能运行,否则该应用程序将无法启动. 我提到了一些帖子如下所示:如何在Android中集成OpenCV Manager应用",了解如何无需下载OpenCV管理器即可在android上运行opncv代码 但不幸的是,只要未安装OpenCV Manager,该应用程序就无法启动.

I am using OpenCV4Android version 2.4.10 and i test my code on Samsung Galayx GT-I9300. the problem i have is, that i must download Opencv Manager from play store so that my opencv code runs, otherwise the App would not start. I referred to some postes "as shown here How to integrate OpenCV Manager in Android App" to know how can i run the opncv code on android without the need to download OpenCV manager but unfortunately the App doesnt start as long as OpenCV Manager is not installed.

我尝试了以下活动

static {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
}

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};
...
...
...

@Override
public void onResume() {
    super.onResume();
    Log.w(TAG, "onResume");

    if (!OpenCVLoader.initDebug()) {
        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, getActivity(), mLoaderCallback);
    } else {
        Log.d(TAG, "OpenCV library found inside package. Using it!");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}

但是并不能解决问题. 请让我知道如何在不下载Opencv Manager的情况下在android上运行opencv代码吗?

but that did not solve the problem. Please let me know how to run opencv code on android without downloading Opencv Manager ?

更新:

使用initAsync :

请提供一个示例,说明如何在应用的生产版本中使用"initAsync()"吗?因为我尝试这样做,但是我当然不能在静态块中使用它,因为"intiAsync"的签名如下所示:"OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10,此为mLoaderCallback);"而且我不能使用"this"关键字,也不能在静态块内使用对回调"mLoaderCallback"的引用

would you please provide an example how to use "initAsync()" in the production version of the App? because i tried to do it but of course i cant use it in the static block because the signature of "intiAsync" is as follows "OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mLoaderCallback);" and i cant use "this" keyword nor a reference to the callback "mLoaderCallback" inside the static block

public class MainActivity extends AppCompatActivity {

static {
    //OpenCVLoader.initDebug();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mLoaderCallback); //this keyword and "mLoaderCallback" are not defined in this scope
}

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS: {
                Log.i("MainActivity", "OpenCV loaded successfully");
            }
            break;
            default: {
                super.onManagerConnected(status);
            }
            break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Mat m = new Mat(100, 100, CvType.CV_8UC4);
    Log.d("MainActivity", "m.width()" + m.width());
    Log.d("MainActivity", "m.height()" + m.height());
}
}

推荐答案

按照您想要的分步过程进行操作,因此,我将从头开始创建SampleOpenCV项目,并建议在以下步骤中执行以下步骤新项目,开始工作后,您可以尝试将更改迁移到主项目.

As you want step by step procedure, So I would start with creating a SampleOpenCV project from scratch, and would also suggest to do the following steps in a new project, Once it starts working you may try to migrate the changes to your main project.

  1. Opencv.org 下载Android的OpenCV软件包 [
  1. Download OpenCV package for android from Opencv.org [ Direct Download Link V3.1 ]
  2. Unpack the zip to a location of your choice, Open the SampleOpenCV project in Android Studio, then File -> New -> Import Module, which would open a new pop-up to enter the module path, select {unzipped_opencv}/sdk/java, this would create a OpenCVLibrary310 directory under SampleOpenCV.
  3. Now Open SampleOpenCV/OpenCVLibrary310/build.gradle and copy the following fields from SampleOpenCV/app/build.gradle:
    • compileSdkVersion
    • buildToolsVersion
    • minSdkVersion
    • targetSdkVersion

  1. 在弹出窗口中单击右上方的 + 符号,然后选择 3 Module Dependency .现在选择OpencvLibrary310.关闭弹出窗口,并让gradle同步.

  1. Click at the top-right + sign, in the pop window, and select 3 Module Dependency. Now choose OpencvLibrary310. Close the pop up and let the gradle sync.

将libs文件夹{unzipped_opencv}/sdk/native/libs复制到app/src/main下的Android Studio,然后将其重命名为jniLibs(请注意此处).

Copy libs folder {unzipped_opencv}/sdk/native/libs to Android Studio under app/src/main and rename from it to jniLibs (Mind the case here).


public class MainActivity extends AppCompatActivity {
    static {
        OpenCVLoader.initDebug();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Mat m = new Mat(100, 100, CvType.CV_8UC4);
    }
}

注意:OpenCVLoader.initDebug()必须仅用于调试目的,就像在计算机上本地开发时一样.但是出于生产目的,您需要在Play商店等上发布该应用程序,则必须使用OpenCVLoader.initAsync().实际上,根据手机的不同,初始化OpenCVLoader会花费一些时间.因此,如果您使用initDebug()加载它,它将在主线程中执行,这可能会在一段时间内阻塞UI.因此建议在后台加载OpenCV,这可以使用initAsync()

NOTE: OpenCVLoader.initDebug() must be used for debugging purposes only as when you are developing locally on your machine. But for production purposes where you need to release the app on Play Store, etc. you must use OpenCVLoader.initAsync(). Actually initializing the OpenCVLoader takes some time depending upon the phone. So if you load it uisng initDebug(), then it would be executed in the main thread, which may block the UI for a fractional time. So it is advised to load the OpenCV in background which can be achieved using initAsync()

更新后的答案

如果您已完成所有步骤并获得了java.lang.UnsatisfiedLinkError,则可能是您遗漏了jniLibs或未正确实施步骤6.

If you are done with all the steps and getting java.lang.UnsatisfiedLinkError, possibly you are missing jniLibs or you haven't implemented step 6 properly.

将此代码添加到您的应用级别等级中:

Add this code in your app level graddle:

 andriod{
        sourceSets.main {
                    jniLibs.srcDirs = ['libs'] 
        }
  }

Gradle同步后, jniLibs 会这样显示

After graddle sync jniLibs will show up like this

这篇关于如何在没有OpenCv Manager的情况下运行OpenCV代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆