ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Android Studio,OpenCV jar,java.lang.ClassNotFoundException:org.opencv.R $styleable

2019-11-21 17:26:56  阅读:267  来源: 互联网

标签:android-studio opencv android


网络上有关如何在Android Studio中使用OpenCV的大多数示例都涉及将OpenCV模块导入到您的项目中.但是,我试图将OpenCV用作jar,以便避免使用OpenCV的副本使源代码控制存储库混乱.

这些就是我为名为“ app”的应用所执行的步骤.

答:在/ Project / apps下创建一个libs目录.将OpenCV jar放在此处.还将opencv本机库放在子目录中.

B.在/Project/app/build.gradle中执行以下操作

将这些行放在顶部:

repositories { flatDir { dirs 'libs'    } }

将这些行放在android部分中:

sourceSets.main.jniLibs.srcDirs = ['libs']

将此行放在依赖项部分:

    compile fileTree(dir: 'libs', include: ['*.jar'])

C.在应用程序标签之后,将这些行添加到AndroidManifest.xml中.

    <uses-sdk android:minSdkVersion="9"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

D.不确定是否需要这样做,我尝试了是否使用-似乎没有区别:我在AndroidManifest.xml的application标记内添加以下行:

        <uses-library android:name="org.opencv.android.JavaCameraView" />

E.最后是我的布局xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/lib/org.opencv.android.JavaCameraView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/main_activity_surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    </LinearLayout>

这一切都很好.我可以在自己的活动中引用OpenCV,而不会出错.但是布局编辑器给出以下错误:无法实例化以下类:org.opencv.android.JavaCameraView. java.lang.ClassNotFoundException:org.opencv.R $styleable.

如果只是一个渲染问题,也许我可以忽略它,但是当我运行它时会发生类似的错误.

我想念什么?

(我知道SO中关于此错误的问题有点类似,但他的解决方案涉及导入整个模块,该模块确实有效,但正是我要避免的事情.)

解决方法:

错误消息:

The following classes could not be instantiated: org.opencv.android.JavaCameraView. java.lang.ClassNotFoundException: org.opencv.R$styleable

表示在构建时找不到OpenCV资源.如果您将OpenCV包含在jar库中,那将是问题的根源-jar中没有资源.

解决方案是将OpenCV构建为AAR,因为AAR确实有资源.为此构造一个库模块,将其编译为AAR,然后在需要的任何地方重复使用该归档文件.

标签:android-studio,opencv,android
来源: https://codeday.me/bug/20191121/2053346.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有