2016年的最后一周,和西安研发中心的罗总,一起联调了安卓开发板中图像识别的算法。罗总提出OpenCV-Android库中包含了太多安卓开发板不需要的动态库,让我把这些不需要的动态库从OpenCV-Android库中剔除去。
参考 steveliles 的opencv-android项目,我对新发布的 opencv-3.2.0-android-sdk.zip 进行了封装。
因为官网提供的 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官网下载OpenCV3.2.0的OpenCV for Android压缩文件opencv-3.2.0-android-sdk.zip。
利用Android Studio 2.2.3创建一个android项目,这里我创建的是OpenCV4Android项目。
解压下载的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子文件中,做如下操作:
/**
* 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文件夹内。
为
添加id,修改后代码片段如下所示:
+
号,选择3 Module Dependency,在弹出的对话框中,选择**:opencv-android**,最后依次点击OK,OK。添加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’,启动虚拟手机的界面如下所示:
点击,Hello World!后的界面如下所示:
在opencv-android/build/outputs/aar文件夹下,有生成的debug和release两种格式的aar文件夹。
opencv-android-debug.aar
opencv-android-release.aar
参考这里
修改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
}
}
}
OpenCV4Android
新的一年,祝福小伙伴们,新年快乐,身体健康,阖家欢乐,工作顺利,事事顺心!