我正在尝试使Android应用程序从库中获取图像并调用用c ++编写的本机函数使其变灰。
我的主要活动是:
static {
if(!OpenCVLoader.initDebug())
{
Log.e("tom", "Tomasku opencv problem static");
}
else
{
Log.e("tom", "OPENCV NACITANE");
System.loadLibrary("halgorithm");
}
}
***在同一个Activity中我调用另一个活动(EditorActivity):
Intent i = new Intent(this, EditorActivity.class);
...
i.putExtra("path", selectedImagePath); //path to image from gallery
startActivity(i);
***在我的EditorActivity中:
...
public native int fce(long matAddrRgba, long matAddrGray);
...
fce(image.getNativeObjAddr(), image_gray.getNativeObjAddr());
// and here it crashes
**在我的halgorithm.cpp
中#include <jni.h>
#include <string.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
//using namespace std;
using namespace cv;
extern "C"
{
JNIEXPORT jint JNICALL Java_com_example_hematoma_MainActivity_fce(JNIEnv*, jobject, jlong addrRgba, jlong addrGray) {
Mat& mRgb = *(Mat*)addrRgba;
Mat& mGray = *(Mat*)addrGray;
int conv;
jint retVal;
conv = toGray(mRgb, mGray);
retVal = (jint)conv;
return retVal;
}
}
int toGray(Mat img, Mat& gray)
{
cvtColor(img, gray, CV_RGBA2GRAY); // Assuming RGBA input
if (gray.rows == img.rows && gray.cols == img.cols)
{
return 1;
}
return 0;
}
**在我的Android.mk
中include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
include /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := halgorithm
LOCAL_SRC_FILE := halgorithm.cpp
LOCAL_C_INCLUDES := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include
include $(BUILD_SHARED_LIBRARY)
*** logcat的
04-30 13:16:28.320: W/dalvikvm(3863): No implementation found for native Lcom/example/hematoma/EditorActivity;.fce (JJ)I
04-30 13:16:28.320: D/AndroidRuntime(3863): Shutting down VM
04-30 13:16:28.320: W/dalvikvm(3863): threadid=1: thread exiting with uncaught exception (group=0x40185760)
04-30 13:16:28.320: E/AndroidRuntime(3863): FATAL EXCEPTION: main
04-30 13:16:28.320: E/AndroidRuntime(3863): java.lang.UnsatisfiedLinkError: fce
04-30 13:16:28.320: E/AndroidRuntime(3863): at com.example.hematoma.EditorActivity.fce(Native Method)
04-30 13:16:28.320: E/AndroidRuntime(3863): at com.example.hematoma.EditorActivity$3.onClick(EditorActivity.java:166)
04-30 13:16:28.320: E/AndroidRuntime(3863): at android.view.View.performClick(View.java:3110)
04-30 13:16:28.320: E/AndroidRuntime(3863): at android.view.View$PerformClick.run(View.java:11934)
04-30 13:16:28.320: E/AndroidRuntime(3863): at android.os.Handler.handleCallback(Handler.java:587)
04-30 13:16:28.320: E/AndroidRuntime(3863): at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 13:16:28.320: E/AndroidRuntime(3863): at android.os.Looper.loop(Looper.java:132)
04-30 13:16:28.320: E/AndroidRuntime(3863): at android.app.ActivityThread.main(ActivityThread.java:4123)
04-30 13:16:28.320: E/AndroidRuntime(3863): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:16:28.320: E/AndroidRuntime(3863): at java.lang.reflect.Method.invoke(Method.java:491)
04-30 13:16:28.320: E/AndroidRuntime(3863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-30 13:16:28.320: E/AndroidRuntime(3863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-30 13:16:28.320: E/AndroidRuntime(3863): at dalvik.system.NativeStart.main(Native Method)
提前致谢!
**************************** EDITED ****************** *****
根据你的建议我改变了:
***在Android.mk中:
JNIEXPORT jint JNICALL
Java_com_example_hematoma_**EditorActivity**_fce(JNIEnv*, jobject, jlong addrRgba, jlong addrGray) {...}
***在EditorActivity中: 我在本机方法之前调用System.loadLibrary(“halgorithm”)。
static {
if(!OpenCVLoader.initDebug())
{
Log.e("tom", "Tomasku opencv problem static");
}
else
{
Log.e("tom", "OPENCV NACITANE");
System.loadLibrary("halgorithm");
}
}
但它并没有解决我的错误。有同样的错误:/
*** logcat的
04-30 13:36:21.760: W/dalvikvm(4255): No implementation found for native Lcom/example/hematoma/EditorActivity;.fce (JJ)I
04-30 13:36:21.760: D/AndroidRuntime(4255): Shutting down VM
04-30 13:36:21.760: W/dalvikvm(4255): threadid=1: thread exiting with uncaught exception (group=0x40185760)
04-30 13:36:21.760: E/AndroidRuntime(4255): FATAL EXCEPTION: main
04-30 13:36:21.760: E/AndroidRuntime(4255): java.lang.UnsatisfiedLinkError: fce
04-30 13:36:21.760: E/AndroidRuntime(4255): at com.example.hematoma.EditorActivity.fce(Native Method)
04-30 13:36:21.760: E/AndroidRuntime(4255): at com.example.hematoma.EditorActivity$3.onClick(EditorActivity.java:166)
04-30 13:36:21.760: E/AndroidRuntime(4255): at android.view.View.performClick(View.java:3110)
04-30 13:36:21.760: E/AndroidRuntime(4255): at android.view.View$PerformClick.run(View.java:11934)
04-30 13:36:21.760: E/AndroidRuntime(4255): at android.os.Handler.handleCallback(Handler.java:587)
04-30 13:36:21.760: E/AndroidRuntime(4255): at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 13:36:21.760: E/AndroidRuntime(4255): at android.os.Looper.loop(Looper.java:132)
04-30 13:36:21.760: E/AndroidRuntime(4255): at android.app.ActivityThread.main(ActivityThread.java:4123)
04-30 13:36:21.760: E/AndroidRuntime(4255): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:36:21.760: E/AndroidRuntime(4255): at java.lang.reflect.Method.invoke(Method.java:491)
04-30 13:36:21.760: E/AndroidRuntime(4255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-30 13:36:21.760: E/AndroidRuntime(4255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-30 13:36:21.760: E/AndroidRuntime(4255): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
public native int fce(...)
是EditorActivity
的成员,但在C ++代码中,您调用了函数Java_com_example_hematoma_MainActivity_fce
。
将其重命名为Java_com_example_hematoma_EditorActivity_fce
。
在拨打本机功能之前,请不要忘记拨打System.loadLibrary()
,例如添加您的活动:
static {
System.loadLibrary("halgorithm");
}
答案 1 :(得分:0)
经过几天绝望之后:D我解决了我的问题。问题是$(OPENCV_INSTALL_MODULES)应该相等。我的Android.mk文件中的行OPENCV_INSTALL_MODULES:= on。
我凌驾于 OPENCV_INSTALL_MODULES:= on
到
<强> OPENCV_INSTALL_MODULES =上强>
现在它起作用......也许它对其他人也有帮助:))
答案 2 :(得分:0)