OpenCV是图像操作的工具,针对于Android平台,他们也出了相关的sdk,就用最新的4.1.1接入并学习。
下载:https://opencv.org/releases/
当然这个需要翻墙,速度会很慢,这里我也准备了现成的,已放入百度云盘:
链接:https://pan.baidu.com/s/1ITvcA9m01u-EP6AhIMwNKw
提取码:och1
复制这段内容后打开百度网盘手机App,操作更方便哦
正式接入:
1、创建一个支持ndk开发的工程。
2、编辑cmake文件
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
#支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#配置加载native依赖
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/opencv/include)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
#动态方式加载
add_library( opencv_java4 SHARED IMPORTED )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
#引入libopencv_java4.so文件
set_target_properties(opencv_java4
PROPERTIES IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libopencv_java4.so
)
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)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
opencv_java4
# Links the target library to the log library
# included in the NDK.
${log-lib})
注意点:
a、opencv得支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
b、添加所有的头文件,设置他的引入路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/opencv/include)
c、动态方式加载opencv
add_library( opencv_java4 SHARED IMPORTED )
这里的opencv_java4这个命名必须是这样,他其实是我们依赖的libopencv_java4.so去掉lib头去掉.so后缀留下的,用其他的名字比如随便命名opencv就会报libopencv.so找不到。这个要特别注意。
d、依赖opencv的so文件
set_target_properties(opencv_java4
PROPERTIES IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libopencv_java4.so
)
这里的${CMAKE_CURRENT_SOURCE_DIR}指的是你的cmake文件存放路径,然后基于这个路径自己设置找到so文件路径。
e、最后连接依赖关系
target_link_libraries( # Specifies the target library.
native-lib
opencv_java4
# Links the target library to the log library
# included in the NDK.
${log-lib})
3、放入opencv sdk文件
1、根据cmake先来放入我们的头文件
将整个include文件夹存入到cpp文件中,这个我为了分类,所以多创了一个opencv的文件夹并放入,毕竟我们一个项目不止一个三方依赖库。
2、存放so文件到工程
这里,我只想支持arm-v7a的cpu,所以我只创了这个文件夹,libc++_shared.so文件不属于opencv中,那么他在哪呢?进入我们的ndk目录,他就在这,ndk目录\sources\cxx-stl\llvm-libc++\libs\armeabi-v7a,注意一定要是armeabi-v7a,因为我们应用的是这个,不然会报错,是64位和32位不同报错。
3、gradle配置ndk限制以及cmake的ndk设置以及支持c++11
源码:
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'armeabi-v7a'
}
}
ndk {
abiFilters "armeabi-v7a"
}
4、gradle.properties文件中允许对ndk限制
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useDeprecatedNdk=true
4、测试图片转为灰度图
1、创建自己的工具类
OpencvImageUtile引入两个so
public class OpencvImageUtile {
static {
System.loadLibrary("native-lib");
System.loadLibrary("opencv_java4");
}
//转灰度
public static Bitmap goGray(Bitmap bitmap){
Mat sSrc =new Mat();
Mat sDst=new Mat();
org.opencv.android.Utils.bitmapToMat(bitmap, sSrc);
Imgproc.cvtColor(sSrc, sDst, Imgproc.COLOR_BGRA2GRAY);
org.opencv.android.Utils.matToBitmap(sDst, bitmap);
sSrc.release();
sDst.release();
return bitmap;
}
}
然后他会报org.opencv.android.Utils等文件找不到,我们可以去opencv sdk中找
2、activity调用
就是布局放个imageview然后将灰度图设置给他。
最终效果图: