UnsatisfiedLinkError - 在 Android 中旋转相机图像数据(在 OpenCV 中使用,而不仅仅是在屏幕上显示)
Posted
技术标签:
【中文标题】UnsatisfiedLinkError - 在 Android 中旋转相机图像数据(在 OpenCV 中使用,而不仅仅是在屏幕上显示)【英文标题】:UnsatisfiedLinkError - Rotate camera image data in Android (for use in OpenCV not just display on screen) 【发布时间】:2015-05-23 07:01:00 【问题描述】:我正在开发一个纵向模式的 android 应用,我想将相机输出到屏幕上的 2 帧。
第一帧是相机的直接输出,方向错误。我可以使用这一行来解决这个问题:
mCamera.setDisplayOrientation(90);
但是对于第二帧,我想输出从 C++ 中的 OpenCV 函数渲染的内容。我无法正确旋转图像。我最近的尝试是在 Java 中使用 OpenCV 进行轮换,但出现“UnsatisfiedLinkError”,我在下面发布了日志。这是我试图用来旋转图像的代码(来源:Rotate camera preview to Portrait Android OpenCV Camera):
mYuv = new Mat(PreviewSizeHeight + PreviewSizeHeight/2, PreviewSizeWidth, CvType.CV_8UC1);
mYuv.put(0, 0, arg0);
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGBA, 4);
这是我在上面的代码中使用的函数:
@Override
public void onPreviewFrame(byte[] arg0, Camera arg1)
// At preview mode, the frame data will push to here.
if (imageFormat == ImageFormat.NV21)
mYuv = new Mat(PreviewSizeHeight + PreviewSizeHeight/2, PreviewSizeWidth, CvType.CV_8UC1);
mYuv.put(0, 0, arg0);
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGBA, 4);
//We only accept the NV21(YUV420) format.
if ( !bProcessing )
//FrameData = arg0;
mHandler.post(DoImageProcessing);
这是我得到的错误:
03-20 02:06:34.793: D/OpenCV/StaticHelper(13063): Trying to get library list
03-20 02:06:34.803: E/OpenCV/StaticHelper(13063): OpenCV error: Cannot load info library for OpenCV
03-20 02:06:34.803: D/OpenCV/StaticHelper(13063): Library list: ""
03-20 02:06:34.803: D/OpenCV/StaticHelper(13063): First attempt to load libs
03-20 02:06:34.803: D/OpenCV/StaticHelper(13063): Trying to init OpenCV libs
03-20 02:06:34.803: D/OpenCV/StaticHelper(13063): Trying to load library opencv_java
03-20 02:06:34.803: D/OpenCV/StaticHelper(13063): Cannot load library "opencv_java"
03-20 02:06:34.803: W/System.err(13063): java.lang.UnsatisfiedLinkError: Couldn't load opencv_java from loader dalvik.system.PathClassLoader[dexPath=/data/app/my.project.realtimeSpotDetection-102.apk,libraryPath=/data/app-lib/my.project.realtimeSpotDetection-102]: findLibrary returned null
03-20 02:06:34.803: W/System.err(13063): at java.lang.Runtime.loadLibrary(Runtime.java:358)
03-20 02:06:34.803: W/System.err(13063): at java.lang.System.loadLibrary(System.java:526)
03-20 02:06:34.803: W/System.err(13063): at org.opencv.android.StaticHelper.loadLibrary(StaticHelper.java:64)
03-20 02:06:34.803: W/System.err(13063): at org.opencv.android.StaticHelper.initOpenCVLibs(StaticHelper.java:95)
03-20 02:06:34.803: W/System.err(13063): at org.opencv.android.StaticHelper.initOpenCV(StaticHelper.java:39)
03-20 02:06:34.803: W/System.err(13063): at org.opencv.android.OpenCVLoader.initDebug(OpenCVLoader.java:56)
03-20 02:06:34.803: W/System.err(13063): at my.project.realtimeSpotDetection.CameraPreview.<clinit>(CameraPreview.java:33)
03-20 02:06:34.803: W/System.err(13063): at my.project.realtimeSpotDetection.realtimeSpotDetection.onCreate(realtimeSpotDetection.java:77)
03-20 02:06:34.803: W/System.err(13063): at android.app.Activity.performCreate(Activity.java:5453)
03-20 02:06:34.803: W/System.err(13063): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
03-20 02:06:34.803: W/System.err(13063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
03-20 02:06:34.803: W/System.err(13063): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
03-20 02:06:34.803: W/System.err(13063): at android.app.ActivityThread.access$900(ActivityThread.java:175)
03-20 02:06:34.803: W/System.err(13063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
03-20 02:06:34.803: W/System.err(13063): at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 02:06:34.803: W/System.err(13063): at android.os.Looper.loop(Looper.java:146)
03-20 02:06:34.803: W/System.err(13063): at android.app.ActivityThread.main(ActivityThread.java:5602)
03-20 02:06:34.803: W/System.err(13063): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 02:06:34.803: W/System.err(13063): at java.lang.reflect.Method.invoke(Method.java:515)
03-20 02:06:34.803: W/System.err(13063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-20 02:06:34.803: W/System.err(13063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-20 02:06:34.803: W/System.err(13063): at dalvik.system.NativeStart.main(Native Method)
03-20 02:06:34.803: D/OpenCV/StaticHelper(13063): First attempt to load libs fails
03-20 02:06:34.803: D/dalvikvm(13063): Trying to load lib /data/app-lib/my.project.realtimeSpotDetection-102/libImageProcessing.so 0x425086a8
03-20 02:06:34.808: W/linker(13063): libImageProcessing.so has text relocations. This is wasting memory and is a security risk. Please fix.
03-20 02:06:34.843: D/dalvikvm(13063): Added shared lib /data/app-lib/my.project.realtimeSpotDetection-102/libImageProcessing.so 0x425086a8
03-20 02:06:34.848: D/dalvikvm(13063): No JNI_OnLoad found in /data/app-lib/my.project.realtimeSpotDetection-102/libImageProcessing.so 0x425086a8, skipping init
03-20 02:06:35.038: D/libEGL(13063): loaded /system/lib/egl/libEGL_mali.so
03-20 02:06:35.083: D/libEGL(13063): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-20 02:06:35.088: D/libEGL(13063): loaded /system/lib/egl/libGLESv2_mali.so
03-20 02:06:35.098: E/(13063): Device driver API match
03-20 02:06:35.098: E/(13063): Device driver API version: 29
03-20 02:06:35.098: E/(13063): User space API version: 29
03-20 02:06:35.103: E/(13063): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014
03-20 02:06:35.233: D/OpenGLRenderer(13063): Enabling debug mode 0
03-20 02:06:36.068: I/dalvikvm-heap(13063): Grow heap (frag case) to 11.507MB for 1036816-byte allocation
03-20 02:06:36.143: W/dalvikvm(13063): No implementation found for native Lorg/opencv/core/Mat;.n_Mat:(III)J
03-20 02:06:36.143: D/AndroidRuntime(13063): Shutting down VM
03-20 02:06:36.143: W/dalvikvm(13063): threadid=1: thread exiting with uncaught exception (group=0x41c28c08)
03-20 02:06:36.163: E/AndroidRuntime(13063): FATAL EXCEPTION: main
03-20 02:06:36.163: E/AndroidRuntime(13063): Process: my.project.realtimeSpotDetection, PID: 13063
03-20 02:06:36.163: E/AndroidRuntime(13063): java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.core.Mat.n_Mat:(III)J
03-20 02:06:36.163: E/AndroidRuntime(13063): at org.opencv.core.Mat.n_Mat(Native Method)
03-20 02:06:36.163: E/AndroidRuntime(13063): at org.opencv.core.Mat.<init>(Mat.java:477)
03-20 02:06:36.163: E/AndroidRuntime(13063): at my.project.realtimeSpotDetection.CameraPreview.onPreviewFrame(CameraPreview.java:77)
03-20 02:06:36.163: E/AndroidRuntime(13063): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1016)
03-20 02:06:36.163: E/AndroidRuntime(13063): at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 02:06:36.163: E/AndroidRuntime(13063): at android.os.Looper.loop(Looper.java:146)
03-20 02:06:36.163: E/AndroidRuntime(13063): at android.app.ActivityThread.main(ActivityThread.java:5602)
03-20 02:06:36.163: E/AndroidRuntime(13063): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 02:06:36.163: E/AndroidRuntime(13063): at java.lang.reflect.Method.invoke(Method.java:515)
03-20 02:06:36.163: E/AndroidRuntime(13063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-20 02:06:36.163: E/AndroidRuntime(13063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-20 02:06:36.163: E/AndroidRuntime(13063): at dalvik.system.NativeStart.main(Native Method)
03-20 02:06:36.173: I/dalvikvm-heap(13063): Grow heap (frag case) to 12.525MB for 1036816-byte allocation
Android.mk 文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_CAMERA_MODULES := on
OPENCV_INSTALL_MODULES := on
## include ../includeOpenCV.mk
OPENCV_MK_PATH:=/home/android/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
#try to load OpenCV.mk from default install location
include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
else
include $(OPENCV_MK_PATH)
endif
LOCAL_MODULE := ImageProcessing
LOCAL_SRC_FILES := ImageProcessing.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
【问题讨论】:
【参考方案1】:您的 Android 项目中的 libs 目录中是否有 opencv_java 共享库?该库可以很容易地包含在您的 Eclipse 项目中并且不会导致编译错误,但它不会包含在您的 .apk 文件中,除非它位于 libs 目录中
【讨论】:
究竟是什么文件?我在 Android.mk 文件中静态链接了 OpenCV 目录(很多文件)。我需要复制部分/所有这些文件吗?如果您需要更多信息,请告诉我。 在我看来,OpenCV 提供了一个 Eclipse 库项目文件。如果您已将其作为库包含,Project->Properties->Android 并添加为库,则不需要手动包含该库。我确实在 libs 目录中看到了 libOpenCV_java.so 文件,这就是它正在寻找的内容 我已经在 Eclipse 中添加了 OpenCV 作为库(目录是 OpenCV-2.4.9/sdk/java)。我确实在构建后检查了 /project/libs 目录,它不包含 libOpenCV_java.so 文件。我需要做些什么来构建这个文件吗?我在主帖中添加了更多信息。 该文件可能由于依赖关系而无法加载。是否所有 .so 文件都在您的 libs 目录中的 opencv 项目中?具体是opencv核心文件? 你看过这个帖子吗:***.com/questions/14693558/…。他在同一个 n_Mat 对象上失败了。【参考方案2】:所以我最终使用了另一种方法,因为我正在编写本机代码并在 C++ 代码块中使用 OpenCV。 不是一个实际的解决方案,但 Gary 上面的最后一条评论可能是最接近解决方案的(您需要在手机上安装 OpenCV 应用程序,以便它可以访问库)
【讨论】:
以上是关于UnsatisfiedLinkError - 在 Android 中旋转相机图像数据(在 OpenCV 中使用,而不仅仅是在屏幕上显示)的主要内容,如果未能解决你的问题,请参考以下文章
SmartGWT 库的 UnsatisfiedLinkError
java.lang.UnsatisfiedLinkError
在 Java 中使用 TESSERACT API 时出现 java.lang.UnsatisfiedLinkError 异常