创建OpenCV3.X-Android库

起因

2016年的最后一周,和西安研发中心的罗总,一起联调了安卓开发板中图像识别的算法。罗总提出OpenCV-Android库中包含了太多安卓开发板不需要的动态库,让我把这些不需要的动态库从OpenCV-Android库中剔除去。

参考 steveliles 的opencv-android项目,我对新发布的 opencv-3.2.0-android-sdk.zip 进行了封装。

为什么要对OpenCV的安卓库进行封装?

因为官网提供的 opencv-3.2.0-android-sdk.zip,虽然可以直接导入使用,但是,需要用到相应平台的 apk。也就是说,如果,你要在自己的app中运行opencv的代码,需要先安装相应平台的apk。

OpenCV_3.2.0_Manager_3.20_arm64-v8a.apk
OpenCV_3.2.0_Manager_3.20_armeabi-v7a.apk
OpenCV_3.2.0_Manager_3.20_armeabi.apk
OpenCV_3.2.0_Manager_3.20_mips.apk
OpenCV_3.2.0_Manager_3.20_mips64.apk
OpenCV_3.2.0_Manager_3.20_x86.apk
OpenCV_3.2.0_Manager_3.20_x86_64.apk

readme.txt

How to select the proper version of OpenCV Manager
--------------------------------------------------

Since version 1.7 several packages of OpenCV Manager are built. Every package is targeted for some
specific hardware platform and includes corresponding OpenCV binaries. So, in all cases OpenCV
Manager uses built-in version of OpenCV. The new package selection logic in most cases simplifies
OpenCV installation on end user devices. In most cases OpenCV Manager may be installed automatically
from Google Play.

If Google Play is not available (i.e. on emulator, developer board, etc), you can install it
manually using adb tool:

    adb install /apk/OpenCV__Manager__.apk

Example: OpenCV_3.2.0-dev_Manager_3.20_armeabi-v7a.apk

Use the list of platforms below to determine proper OpenCV Manager package for your device:

- armeabi (ARMv5, ARMv6)
- armeabi-v7a (ARMv7-A + NEON)
- arm64-v8a
- mips
- mips64
- x86
- x86_64

创建自己的OpenCV-Android库

1. 下载 OpenCV for Android

从OpenCV官网下载OpenCV3.2.0的OpenCV for Android压缩文件opencv-3.2.0-android-sdk.zip。

2. 创建OpenCV4Android项目

利用Android Studio 2.2.3创建一个android项目,这里我创建的是OpenCV4Android项目。

3. 创建OpenCV-Android库

  • 依次点击File->New->New Module…,在弹出的对话框中,选择Android Library,然后,点击Next
  • Application/Library name:处,输入安卓库名称OpenCV-Android
    点击Package name:后面的Edit,修改包名称为org.opencv

4. 配置OpenCV-Android库

  • 解压下载的opencv-3.2.0-android-sdk.zip

  • 复制OpenCV-android-sdk/sdk/java/src/org/opencv文件夹内的所有子文件夹,并粘贴到自己创建的OpenCV-Android库中opencv-android/src/main/java/org/opencv的文件夹下。

  • opencv-android/src/main/java/org/opencv/android子文件中,做如下操作:

    • 删除AsyncServiceHelper.java文件
    • 修改OpenCVLoader.java文件,注释掉以下内容:
    /**
     * Loads and initializes OpenCV library using OpenCV Engine service.
     * @param Version OpenCV library version.
     * @param AppContext application context for connecting to the service.
     * @param Callback object, that implements LoaderCallbackInterface for handling the connection status.
     * @return Returns true if initialization of OpenCV is successful.
     */
    

// public static boolean initAsync(String Version, Context AppContext,
// LoaderCallbackInterface Callback)
// {
// return AsyncServiceHelper.initOpenCV(Version, AppContext, Callback);
// }
```

  • 复制OpenCV-android-sdk/sdk/java/res/values/attrs.xml到**opencv-android/src/main/java/res/values/**文件内;

  • 修改opencv-android/src/main/java/res/values/string.xml,修改后如下所示:

    
    OpenCV-3.2.0
    
    
  • OpenCV-android-sdk/sdk/native/libs文件夹下的子文件夹和OpenCV-android-sdk/sdk/3rdparty/libs文件夹下的子文件夹拷贝opencv-android/src/main文件下创建的jniLibs文件夹内。

app文件内相关文件修改

修改app/src/main/res/layout/activity_main.xml

添加id,修改后代码片段如下所示:

    

为app添加opencv-android库

  1. 点击File->Project Structure…打开Project Structure对话框;
  2. 点击Modules下的app,再在右侧点击Dependencies
  3. 点击Dependencies下框附近的+号,选择3 Module Dependency,在弹出的对话框中,选择**:opencv-android**,最后依次点击OKOK

修改MainActivity

添加TextView对象,如果点击该对象,则弹出一个短消息!

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "You clicked textView!", Toast.LENGTH_SHORT).show();
                if (OpenCVLoader.initDebug()) {
                    textView.setText("OpenCV320 loaded successful!");
                }
            }
        });
    }
}

运行

点击Run->Run ‘app’,启动虚拟手机的界面如下所示:

创建OpenCV3.X-Android库_第1张图片

点击,Hello World!后的界面如下所示:

创建OpenCV3.X-Android库_第2张图片

找到opencv-android库对应的aar文件

opencv-android/build/outputs/aar文件夹下,有生成的debug和release两种格式的aar文件夹。

opencv-android-debug.aar
opencv-android-release.aar

如何导入aar文件

参考这里

Bonus

修改app和opencv-android文件夹内的build.gradle文件中的buildTypes字段,仅生成适合指定平台的apk文件。

    buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            // added by JohnTian
            splits {
                abi {
                    enable true
                    reset()
                    include 'armeabi-v7a', 'arm64-v8a'
                    universalApk false
                }
            }
        }

Github

OpenCV4Android

写在最后

新的一年,祝福小伙伴们,新年快乐,身体健康,阖家欢乐,工作顺利,事事顺心!

你可能感兴趣的:(OpenCV,android,opencv)