如何在没有 OpenCv 管理器的情况下运行 OpenCV 代码 [英] How to run OpenCV code without OpenCv Manager
问题描述
我使用的是 OpenCV4Android 版本 2.4.10,我在 Samsung Galayx GT-I9300 上测试了我的代码.我遇到的问题是,我必须从 Play 商店下载 Opencv Manager,以便我的 opencv 代码运行,否则应用程序将无法启动.我参考了一些帖子如下所示
在弹出窗口中单击右上角的 + 符号,然后选择 3 Module Dependency.现在选择
OpencvLibrary310
.关闭弹出窗口,让 gradle 同步.将 libs 文件夹 {unzipped_opencv}/sdk/native/libs 复制到 app/src/main 下的 Android Studio 并将其重命名为
jniLibs
(注意这里的情况).- 大功告成.
<小时>
公共类 MainActivity 扩展 AppCompatActivity {静止的 {OpenCVLoader.initDebug();}@覆盖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()
更新答案
如果您完成了所有步骤并得到 java.lang.UnsatisfiedLinkError
,则可能您缺少 jniLibs
或者您没有正确实施第 6 步.>
在您的应用级graddle中添加此代码:
andriod{sourceSets.main {jniLibs.srcDirs = ['libs']}}
graddle sync 之后jniLibs 会像这样显示
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.
i tried the follwoing
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);
}
}
but that did not solve the problem. Please let me know how to run opencv code on android without downloading Opencv Manager ?
Update:
use of initAsync:
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());
}
}
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.
- Download OpenCV package for android from Opencv.org [ Direct Download Link V3.1 ]
- 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 aOpenCVLibrary310
directory underSampleOpenCV
. - Now Open
SampleOpenCV/OpenCVLibrary310/build.gradle
and copy the following fields fromSampleOpenCV/app/build.gradle
:- compileSdkVersion
- buildToolsVersion
- minSdkVersion
- targetSdkVersion
- Now right click on your
SampleOpenCV
project and click Open Module Settings, look forModules >> app
and select Dependencies
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.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).- You are Done.
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);
}
}
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()
UPDATED ANSWER
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']
}
}
After graddle sync jniLibs will show up like this
这篇关于如何在没有 OpenCv 管理器的情况下运行 OpenCV 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!