gpt4 book ai didi

java - 尝试使用 JNI 组件启动 Android + OpenCV 文件时出现令人困惑的 UnsatisfiedLinkError

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:59 26 4
gpt4 key购买 nike

如果我的答案已经在互联网上的某个地方,我深表歉意,但我已经在这里待了好几天了,而且似乎根本不存在我的问题的答案。我查看了其他用户发布的大多数其他类似问题,并且尝试了提供的所有解决方案,但顽固地拒绝工作。

我一直在尝试运行一个简单的 Android + OpenCV + JNI 代码。我已经配置了所有必要的设置,包括 Android 和应用程序 .mk 文件,并且我还确保我的所有 OpenCV 库都已正确导入,它们看起来确实如此。我的代码在启动前没有给我任何错误,但是应用程序启动后,我得到了一个可怕的'java.lang.UnsatisfiedLinkError: Native method not found: com.test.opencvcameratest.MainActivity.convertNativeGray(I)我' 错误似乎没有合乎逻辑的原因,因为我相当确定我已经采取了所有必要的步骤来达到这一点。我在下面包含了我的代码,我非常感谢任何能帮助我解开这个令人费解的谜团的人。

package com.test.opencvcameratest;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.WindowManager;

public class MainActivity extends Activity implements CvCameraViewListener2 { //implement cvcameraviewlistener2 for camera functionality

private Mat mRgba;
private Mat mGray;

private CameraBridgeViewBase mOpenCvCameraView;

public native int convertNativeGray(int n);

private final static String TAG = "MainActivity";

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
{
@Override
public void onManagerConnected(int status)
{
switch(status)
{
case LoaderCallbackInterface.SUCCESS:
{
System.loadLibrary("OpenCVCameraTest");// Load Native module
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();

}
}
}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);


}

@Override
public void onPause()
{
super.onPause();
if(mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, mLoaderCallback);
Log.i("LOG MESSAGE", "OPENCV LOADED");
}

@Override
public void onDestroy()
{
super.onDestroy();
if(mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}

@Override
public void onCameraViewStarted(int width, int height)
{
//
}

@Override
public void onCameraViewStopped()
{
//
}

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
mRgba = inputFrame.rgba();
//mGray = null;

int i = convertNativeGray(5);

return mRgba;
}

这是原生 .cpp 文件 (OpenCVCameraTest.cpp)。确实存在一个 convertNativeGray 方法,这与链接器所声称的相反。

#include <jni.h>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>

using namespace cv;

extern "C"
{

JNIEXPORT jint JNICALL Java_com_test_opencvcameratest_convertNativeGray(JNIEnv*, jobject, jint n);

JNIEXPORT jint JNICALL Java_com_test_opencvcameratest_convertNativeGray(JNIEnv*, jobject, jint n)
{
return n*2;
}

}

这是 Android.mk 文件。

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on

LOCAL_SRC_FILES := OpenCVCameraTest.cpp

include C:\Users\user\Downloads\OpenCV-2.4.8-android-sdk\sdk\native\jni\OpenCV.mk

LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := OpenCVCameraTest

include $(BUILD_SHARED_LIBRARY)

为了更好地衡量,我也会包括突然出现的错误。

03-31 16:57:47.627: E/AndroidRuntime(14335): FATAL EXCEPTION: Thread-33554
03-31 16:57:47.627: E/AndroidRuntime(14335): Process: com.test.opencvcameratest, PID: 14335
03-31 16:57:47.627: E/AndroidRuntime(14335): java.lang.UnsatisfiedLinkError: Native method not found: com.test.opencvcameratest.MainActivity.convertNativeGray:(I)I
03-31 16:57:47.627: E/AndroidRuntime(14335): at com.test.opencvcameratest.MainActivity.convertNativeGray(Native Method)
03-31 16:57:47.627: E/AndroidRuntime(14335): at com.test.opencvcameratest.MainActivity.onCameraFrame(MainActivity.java:101)
03-31 16:57:47.627: E/AndroidRuntime(14335): at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
03-31 16:57:47.627: E/AndroidRuntime(14335): at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
03-31 16:57:47.627: E/AndroidRuntime(14335): at java.lang.Thread.run(Thread.java:864)

最佳答案

Native函数应该是

JNIEXPORT jint JNICALL Java_com_test_opencvcameratest_MainActivity_convertNativeGray() 

关于java - 尝试使用 JNI 组件启动 Android + OpenCV 文件时出现令人困惑的 UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22762227/

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