为了将c++代码移植到Android,并且c++里面用的是opencv,那么就需要在android里面通过底层调用opencv。
OpenCV是一个基于开源的跨平台计算机视觉库,实现了许多图像处理和计算机视觉方面的通用算法,是计算机视觉领域最有力的研究工具之一。
OpenCV应用领域: 人机互动 物体识别 图像分割 人脸识别 动作识别 运动跟踪 机器人 运动分析 机器视觉 结构分析 汽车安全驾驶 自动驾驶。。。
1、首先我们去下载:
https://opencv.org/releases.html
2、点击Android pack下载opencv-4.0.1-android-sdk.zip
然后解压到目录下
3、启动Android Studio ,在SDK Manager下安装好CMake\LLDB\NDK
4、创建工程(创建步骤这里省略)
5、开始配置opencv
在src/main下创建jni(用于存放我们写的c++代码),然后复制下载的OpenCV-android-sdk\sdk\native\jni目录下的include 到jni目录
同时别忘记复制sdk\native\libs 到工程中的jniLibs目录下
6 开始写c/c++文件
首先创建一个java类
package com.cayden.opencvtest;
/**
* Created by caydencui on 2019/3/27.
*/
public class NativeLib {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public static native int[] Bitmap2Grey(int[] pixels,int w,int h);
}
然后在jni目录下创建文件native-lib.cpp
//
// Created by caydencui on 2019/3/27.
//
#include
#include
#include
using namespace cv;
using namespace std;
extern "C" JNIEXPORT jintArray
JNICALL
Java_com_cayden_opencvtest_NativeLib_Bitmap2Grey(
JNIEnv *env,
jobject /* this */,jintArray buf,jint w,jint h) {
jint *cbuf;
jboolean ptfalse = false;
cbuf = env->GetIntArrayElements(buf, &ptfalse);
if(cbuf == NULL){
return 0;
}
Mat imgData(h, w, CV_8UC4, (unsigned char*)cbuf);
// 注意,Android的Bitmap是ARGB四通道,而不是RGB三通道
cvtColor(imgData,imgData,CV_BGRA2GRAY);
cvtColor(imgData,imgData,CV_GRAY2BGRA);
int size=w * h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result, 0, size, (jint*)imgData.data);
env->ReleaseIntArrayElements(buf, cbuf, 0);
return result;
}
7 创建CMakeLists.txt【很重要的哦,因为cmake会找这个文件然后进行编译】
里面的很多属性 可以看一下英文大概知道什么意思
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# 设置CMAKE的版本号
cmake_minimum_required(VERSION 3.4.1)
# 设置include文件夹的地址
include_directories(${CMAKE_SOURCE_DIR}/src/main/jni/include)
# 设置opencv的动态库
add_library(libopencv_java3 SHARED IMPORTED)
set_target_properties(libopencv_java3 PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
add_library( # Sets the name of the library.
native-lib #.so库名 可自定义
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
libopencv_java3
# Links the target library to the log library
# included in the NDK.
${log-lib} )
9 最后需要在app中的build.gradle进行配置(这里直接给出编译正确的文件代码,此前编译这个里面容易出问题,需要引起注意)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.cayden.opencvtest"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
arguments "-DANDROID_STL=c++_shared"
}
}
ndk{
abiFilters "armeabi-v7a","arm64-v8a"
}
}
sourceSets{
main{
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
splits {
abi {
enable true
reset()
include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' //select ABIs to build APKs for
universalApk true //generate an additional APK that contains all the ABIs
}
}
project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
经过上面的环境配置,现在可以进行编译,如果没有问题会生成对应的so动态库,具体目录如图
1、opencv jni文件的导入方式问题
我这里采用的是<>
2、编译时候cmake参数设置
cmake {
cppFlags “”
arguments “-DANDROID_STL=c++_shared”
}
最后附上源码地址:https://github.com/cayden/OpenCVTest
感谢大家的阅读,也希望能转发并关注我的公众号