如何让 opencv 示例与 Android Studio 2.1 一起使用 [英] How to get opencv samples to work with Android Studio 2.1

查看:42
本文介绍了如何让 opencv 示例与 Android Studio 2.1 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很长时间试图让 opencv 与 Android Studio 一起工作.我已经遵循了所有明显的教程,但根本无法使用 opencv 2.4.11 版示例代码.

I'm having a hell of a time trying to get opencv to work with Android Studio. I've followed all of the obvious tutorials, but can't get the opencv version 2.4.11 sample code to work at all.

应用程序可以编译,但是当我尝试运行它时,我只是在手机上收到一条错误消息:

The app compiles, but when trying to run it I simply get an error message on the phone:

Unfortunately OCV T3 Camera Control has stopped.

和 logcat:

05-02 00:23:37.876 18962-18962/org.opencv.samples.tutorial3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: org.opencv.samples.tutorial3, PID: 18962
                                                                          java.lang.RuntimeException: Unable to resume activity {org.opencv.samples.tutorial3/org.opencv.samples.tutorial3.Tutorial3Activity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }
                                                                              at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
                                                                              at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                           Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }
                                                                              at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1209)
                                                                              at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1308)
                                                                              at android.app.ContextImpl.bindService(ContextImpl.java:1286)
                                                                              at android.content.ContextWrapper.bindService(ContextWrapper.java:604)
                                                                              at org.opencv.android.AsyncServiceHelper.initOpenCV(AsyncServiceHelper.java:24)
                                                                              at org.opencv.android.OpenCVLoader.initAsync(OpenCVLoader.java:89)
                                                                              at org.opencv.samples.tutorial3.Tutorial3Activity.onResume(Tutorial3Activity.java:91)
                                                                              at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
                                                                              at android.app.Activity.performResume(Activity.java:6327)
                                                                              at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
                                                                              at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我正在尝试使用最新的 Android Studio 版本 2.1 和 Android 版本 6.0,即 SDK 版本 23,但我不在乎早期版本是否足够.我看到一些帖子说尝试使用这些更高版本可能是愚蠢的,也许我应该使用 SDK 版本 21 或 19?

I'm trying to work with the most current Android Studio version 2.1 and Android version 6.0, which is SDK version 23, but I don't care if earlier versions would suffice. I've seen posts saying that perhaps it is folly to try using these later versions, and that maybe I should use SDK version 21 or 19?

我尝试修改我的 build.gradle 文件以使用

I've tried modifying my build.gradle files to use

    minSdkVersion 19
    targetSdkVersion 21

这似乎没有任何改变.

请帮忙.

推荐答案

好的,我想出了基本的答案,现在允许示例实际运行,尽管它们仍然存在其他问题中描述的缺陷.

Ok, I figured out the basic answer, which now allows the samples to actually run, although they still have flaws as described in other questions.

核心答案是你必须设置:

The core answer is that you must set:

minSdkVersion 19
targetSdkVersion 19

在 build.gradle 文件中.

in the build.gradle files.

原因显然是在 Android 19 版本之后指定 AndroidIntents"的方式从相对更改为绝对,而 opencv 尚未更新以解决此问题.我首先通过 answers.opencv.org 上的这个晦涩的参考发现了这一点(它显然不再存在 - opencv 到底发生了什么?):

The reason is apparently that the way Android "Intents" are specified changed from relative to absolute after version 19 of Android, and opencv hasn't been updated to take care of this. I found this out first through this obscure reference on answers.opencv.org (which apparently no longer exists - what the heck is happening to opencv?):

https://webcache.googleusercontent.com/search?q=cache:T5pTdiCxRJIJ:answers.opencv.org/question/54450/error-opencv4android-caused-by-javalangillegalargumentexception-service-intent-must-be-explicit/+&cd=1&hl=en&ct=clnk&gl=us

您必须在包含此 targetSdkVersion 指令的每个 build.gradle 文件中修复此问题,包括在 opencv 库目录和每个示例目录中.

You must fix this in every build.gradle file that includes this targetSdkVersion directive, including in the opencv library directory and in each sample directory.

这些示例仍然只能在横向模式下工作,而不能像我所问的那样在纵向模式下工作:

The samples still only work in landscape mode and not portrait mode as I ask in question:

如何使用 opencv 强制 Android 相机以纵向模式显示?

而且在示例应用程序中菜单的显示等方面还有更多问题.

And there are still more problems with the display of menus in the sample apps, etc., etc.

opencv 和 Android 到底是怎么回事?Android sdk 版本 19(Android 4.4.2)至少有一年(或两三年)或更久了,而这个(近似)版本的 opencv 已经存在了大约一年或更长时间.和分发一起提供的样本甚至不能开箱即用?有人还在为 Android 维护 opencv 吗?

What the heck is going on with opencv and Android? Android sdk version 19 (Android 4.4.2) is at least a year (or two or three) or more old, and this (approximate) version of opencv has been around for probably a year or more. And the samples supplied with the distribution don't even work out of the box? Is anyone still maintaining opencv for Android?

(对不起,评论,请不要费心回复意见,只要你知道这些东西什么时候会在标准发行版中得到修复.)

(Sorry for the commentary, please don't bother replying with opinions, just if you know something about when this stuff is going to be fixed in the standard distribution.)

这篇关于如何让 opencv 示例与 Android Studio 2.1 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆