externalNativeBuild {
cmake {
path"CMakeLists.txt"
}
}
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'x86' ,'armeabi-v7a','x86_64','arm64-v8a' //CPU架构
arguments "-DANDROID_STL=c++_shared" //解决"libc++_shared.so" not found
}
}
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依赖
#以下路径就是上述第3步提到的头文件路径
include_directories(${PROJECT_SOURCE_DIR}/src/main/cpp/include)
#动态方式加载
add_library(lib_opencv STATIC IMPORTED ) #表示创建一个导入库,静态方式
#引入libopencv_java4.so文件
set_target_properties(lib_opencv
PROPERTIES
IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so
)
#自己的源文件
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).
src/main/cpp/native-lib.cpp )
# 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.
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
# Links the target library to the log library
# included in the NDK.
${log-lib} android -ljnigraphics lib_opencv)
需要修改的地方
private static native int[]grey(int[] pixels, int width, int height);
例如
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
extern "C"
JNIEXPORT jintArray JNICALL
Java_czb_com_opencvlib_OpenCVUtil_grey(JNIEnv *env, jobject instance, jintArray buf, jint w, jint h) {
jint *cbuf = env->GetIntArrayElements(buf, JNI_FALSE );
if (cbuf == NULL) {
return 0;
}
Mat imgData(h, w, CV_8UC4, (unsigned char *) cbuf);
uchar* ptr = imgData.ptr(0);
for(int i =0; i < w*h; i ++){
//计算公式:Y(亮度) = 0.299*R + 0.587*G + 0.114*B
//对于一个int四字节,其彩色值存储方式为:BGRA
int grayScale = (int)(ptr[4*i+2]*0.299 + ptr[4*i+1]*0.587 + ptr[4*i+0]*0.114);
ptr[4*i+1] = grayScale;
ptr[4*i+2] = grayScale;
ptr[4*i+0] = grayScale;
}
int size = w * h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result, 0, size, cbuf);
env->ReleaseIntArrayElements(buf, cbuf, 0);
return result;
}
其中函数名 Java_czb_com_opencvlib_OpenCVUtil_grey,表示OpenCVUtil的包名为
czb.com.opencvlib,类下有grey方法,需根据实际进行改动
运行成功的话,可在该路径下查看所编译的so库
app\build\intermediates\cmake\debug\obj
public class OpenCVUtil {
static {
System.loadLibrary("native-lib");
}
private static native int[]grey(int[] pixels_, int width, int height);
public static Bitmap bmpGrey(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
int[] ps =new int[width * height];
bm.getPixels(ps, 0, width, 0, 0, width, height);
long time = System.currentTimeMillis();
int[] newPs =grey(ps, width, height);
Log.e("CZB","C++ 执行:" + (System.currentTimeMillis() - time));
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(newPs, 0, width, 0, 0, width, height);
return bitmap;
}
}
implementation project(':opencvlib')
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
Bitmap bm=OpenCVUtil.bmpGrey(srcBitmap);//关于图形处理步骤建议在子线程运行
imageView.setImageBitmap(bm);
sourceSets.main {
jniLibs.srcDir 'libs'
jni.srcDirs = []
}
JAVA代码JNI类添加
static {
System.loadLibrary("native-lib");
System.loadLibrary("opencv_java4");
}