我正在尝试创建一个可以录制视频的Android应用程序,并且在录制过程中,它应该捕获帧以便处理它们。 但是当我尝试在我的模拟器上运行我的应用程序时(使用Eclipse Juno,OpenCV4Android版本2.4.5和android-ndk),我在logcat中得到了这个结果:
05-13 18:15:34.555: D/dalvikvm(1595): Trying to load lib /data/app-lib/com.example.provavideocapture-1/libjniVideoCapture.so 0x40ce89b0
05-13 18:15:34.555: E/dalvikvm(1595): dlopen("/data/app-lib/com.example.provavideocapture-1/libjniVideoCapture.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libopencv_java.so" needed by "libjniVideoCapture.so"; caused by load_library(linker.cpp:745): library "libopencv_java.so" not found
05-13 18:15:34.555: W/dalvikvm(1595): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/provavideocapture/MainActivity;
05-13 18:15:34.555: W/dalvikvm(1595): Class init failed in newInstance call (Lcom/example/provavideocapture/MainActivity;)
05-13 18:15:34.555: D/AndroidRuntime(1595): Shutting down VM
05-13 18:15:34.555: W/dalvikvm(1595): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-13 18:15:34.575: E/AndroidRuntime(1595): FATAL EXCEPTION: main
05-13 18:15:34.575: E/AndroidRuntime(1595): java.lang.ExceptionInInitializerError
05-13 18:15:34.575: E/AndroidRuntime(1595): at java.lang.Class.newInstanceImpl(Native Method)
05-13 18:15:34.575: E/AndroidRuntime(1595): at java.lang.Class.newInstance(Class.java:1319)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.os.Looper.loop(Looper.java:137)
05-13 18:15:34.575: E/AndroidRuntime(1595): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-13 18:15:34.575: E/AndroidRuntime(1595): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:15:34.575: E/AndroidRuntime(1595): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 18:15:34.575: E/AndroidRuntime(1595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-13 18:15:34.575: E/AndroidRuntime(1595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-13 18:15:34.575: E/AndroidRuntime(1595): at dalvik.system.NativeStart.main(Native Method)
05-13 18:15:34.575: E/AndroidRuntime(1595): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libopencv_java.so" needed by "libjniVideoCapture.so"; caused by load_library(linker.cpp:745): library "libopencv_java.so" not found
05-13 18:15:34.575: E/AndroidRuntime(1595): at java.lang.Runtime.loadLibrary(Runtime.java:371)
05-13 18:15:34.575: E/AndroidRuntime(1595): at java.lang.System.loadLibrary(System.java:535)
05-13 18:15:34.575: E/AndroidRuntime(1595): at com.example.provavideocapture.MainActivity.<clinit>(MainActivity.java:21)
05-13 18:15:34.575: E/AndroidRuntime(1595): ... 15 more
这是我的代码:
1)MainActivity.java
package com.example.provavideocapture;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTextField = (TextView)findViewById(R.id.myTextField);
myTextField.setText(stringFromJNICPP());
}
public native String stringFromJNICPP();
static{
System.loadLibrary("jniVideoCapture");
}
}
2)jniVideoCapture.cpp
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <opencv2/opencv.hpp>
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_provavideocapture_MainActivity_stringFromJNICPP(JNIEnv * env, jobject obj);
};
JNIEXPORT jstring JNICALL Java_com_example_provavideocapture_MainActivity_stringFromJNICPP(JNIEnv * env, jobject obj){
/*VideoCapture cap(0); //open the default camera
if (!cap.isOpened()) //check if we succedeed
return -1;
Mat edges;
namedWindow("edges",1);
for(;;){
Mat frame;
cap>>frame;
cvtColor(frame,edges,CV_BGR2GRAY);
GaussianBlur(edges,edges,Size(7,7),1.5,1.5);
Canny(edges,edges,0,30,3);
imshow("edges",edges);
if(waitKey(30)>=0) break;
}*/
return env->NewStringUTF("Hello From CPP");
}
3)Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include ../../Desktop/Android/OpenCV-2.4.5-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := jniVideoCapture
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_SRC_FILES := jniVideoCapture.cpp
include $(BUILD_SHARED_LIBRARY)
我试图在项目的lib目录中手动复制库libopencv_java.so,从opencv教程项目中获取它,并且我已经在MainActivity中加载了jniVideoCapture,但是没有更好的结果,因为当我运行项目时,库会自动从libs /中删除。
如何解决此问题?
答案 0 :(得分:1)
只需添加一行:
OPENCV_LIB_TYPE:=STATIC
之后:
include $(CLEAR_VARS)
您刚刚没有将(静态)opencv库链接到您的项目 - 这是一个错误。