gpt4 book ai didi

android - UnsatisfiedLinkError : n_Mat while using opencv2. 4.3 与 android 4.0

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:06 26 4
gpt4 key购买 nike

我在 android 中使用 opencv。但是当我在我的代码中添加 Mat() 时,我的应用程序在启动后意外停止。我的错误日志如下:

FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: n_Mat
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:441)
at com.example.imagepro.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)`

我的代码是

import java.io.File;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

import org.opencv.android.Utils;
import org.opencv.imgproc.Imgproc;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

final String TAG = "Hello World";
Mat imgToProcess;

private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Create and set View
setContentView(R.layout.activity_main);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {

Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);

Log.i(TAG, "Trying to load OpenCV library");
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mOpenCVCallBack))
{
Log.e(TAG, "Cannot connect to OpenCV Manager");
}
else{ Log.i(TAG, "opencv successfully added"); }

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String path = Environment.getExternalStorageDirectory()+ "/Koala.jpg"; // get image from sd card

File imgFile = new File(path);

View v = null;
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(path);
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
Log.i(TAG, "opencv successfull 1");
Mat imgToProcess = new Mat();
Mat newmat = new Mat();

Utils.bitmapToMat(myBitmap, imgToProcess);
Imgproc.cvtColor(imgToProcess, newmat, Imgproc.COLOR_RGB2GRAY);
Bitmap outImage = Bitmap.createBitmap(newmat.rows(),newmat.cols(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(newmat, outImage);

myImage.setImageBitmap(outImage);

}

}

我尝试了 stackoverflow 上提供的其他解决方案,但没有一个对我有用。我已经从 F:\OpenCV-2.4.3.2-android-sdk\sdk\native\libs\armeabi-v7a 将 libopencv_java.so 和 libopencv_info.so 复制到我的/libs 文件夹中,但此错误仍然存​​在。

请帮忙。

提前致谢:)

最佳答案

很高兴有人提示我精心发布我的答案。所以我在这里发布我的问题的解决方案:

private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
startDisplay();

} break;


default:
{
super.onManagerConnected(status);
} break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
Log.i(TAG, "Trying to load OpenCV library");
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mOpenCVCallBack))
{
Log.e(TAG, "Cannot connect to OpenCV Manager");
}
else{ Log.i(TAG, "opencv successfull");
System.out.println(java.lang.Runtime.getRuntime().maxMemory()); }
setContentView(R.layout.frameview);
}

此错误背后的问题是我们在 opencv 初始化之前调用了 opencv 相关函数(例如:Mat()),因此显示错误。所以如果你把你的 opencv 函数放在 onManagerConnected() 中,你就可以解决它:

Log.i(TAG, "OpenCV loaded successfully");
startDisplay();

这里,startDisplay() 包含我的 Mat() 初始化。问题是当我们启动一个应用程序时,oncreate() 函数先执行,然后再加载 opencv,因此如果您将 opencv 函数放在 oncreate() 中,那么它会显示错误,因为 opencv 尚未加载。

我希望这能解决您的问题。祝你好运...... Stackoverflow Rocks!!! :)

关于android - UnsatisfiedLinkError : n_Mat while using opencv2. 4.3 与 android 4.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com