抱歉,如果我的答案已经存在于互联网上的某个地方,但我已经在这里呆了好几天了,而且似乎根本不存在我问题的答案。我查看了其他用户发布的大多数其他类似问题,并且尝试了提供的所有解决方案,但它顽固地拒绝工作。
我一直在尝试运行一个简单的Android + OpenCV + JNI代码。我已经配置了所有必要的设置,包括Android和应用程序.mk文件,我还确保我所有的OpenCV库都正确导入,它们似乎是这样。我的代码在启动之前没有给我任何错误,但是一旦应用程序启动,我就会得到一个可怕的'java.lang.UnsatisfiedLinkError: Native method not found: com.test.opencvcameratest.MainActivity.convertNativeGray(I)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:UsersuserDownloadsOpenCV-2.4.8-android-sdksdknativejniOpenCV.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)
本机函数应该是
JNIEXPORT jint JNICALL Java_com_test_opencvcameratest_MainActivity_convertNativeGray()