JniOpencv environment configuration
1, Prepare development package
Copy opencv Android SDK - > SDK - > native - > LIBS - > armeabi to project main - > jnilibs
2, Preparation Library
Copy the following configuration to CMakeLists.txt, and change the path according to the actual situation
# Opencv Android SDK path, configure public path
set(pathToOpenCv /Volumes/D/material/opencv/opencv-3.2.0-android-sdk)
# Support - std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
# Configure and load native dependency (introduce OpenCV header file, similar to Java jar package), configure header file
include_directories(${pathToOpenCv}/sdk/native/jni/include)
# Dynamic loading, dynamic library configuration
add_library( opencv_java3
SHARED
IMPORTED )
# Introduce libopencv? Java3.so file to configure dynamic library
set_target_properties( opencv_java3
PROPERTIES IMPORTED_LOCATION
../../../../src/main/jniLibs/armeabi/libopencv_java3.so)
# Link to opencv? Java3
target_link_libraries( # Specifies the target library.
native-lib opencv_java3
# Links the target library to the
log library
# included in the NDK.
${log-lib} )
3, Configure CPU platform architecture type
Add it under the defaultconfig of android node, and select it according to the actual situation:
externalNativeBuild {
cmake {
...
// To configure
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
A small Demo to enhance image brightness
native
// C + + implementation
public class CppImageProcessUtils {
static {
System.loadLibrary("native-lib");
}
public static Bitmap getBitmap(Bitmap bitmap){
// Step 1: determine image size
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// Step 2: set bitmap - > pixel array
int[] pixArr = new int[width*height];
bitmap.getPixels(pixArr,0,width,0,0,width,height);
// Step 3: call the native method
cppImageProcess(width,height,pixArr,60);
// Return to a new picture
Bitmap newBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);
// Fill in the data for our pictures
newBitmap.setPixels(pixArr,0,width,0,0,width,height);
return newBitmap;
}
// Define java Native methods
public static native void cppImageProcess(int w, int h, int[] pixArr, int ld);
}
native-lib.so
#include <jni.h>
#include <string>
#include <opencv2/opencv.hpp>
// C + + namespace - > similar to java package
using namespace cv;
extern "C"
JNIEXPORT void JNICALL
Java_com_example_changxiaoyu_jniopencvdemo_CppImageProcessUtils_cppImageProcess(JNIEnv *env,
jobject jobj,
jint jw,
jint jh,
jintArray jPixArr,
jint jld) {
// Step 1: import the OpenCV header file
// Step 2: Java array - > C / C + + array
jint *cPixArr = env->GetIntArrayElements(jPixArr, JNI_FALSE);
if (cPixArr == NULL) {
return;
}
// Step 3: C/C + + picture - > opencv picture
Mat mat_image_src(jh, jw, CV_8UC4, (unsigned char *) cPixArr);
// Add a step that is often ignored. Convert 4-channel Mat to 3-channel Mat for image processing
Mat mat_image_dst;
cvtColor(mat_image_src, mat_image_dst, CV_RGBA2BGR, 3);
// Step 4: image processing
// Clone a picture
Mat mat_image_clone = mat_image_dst.clone();
for (int i = 0; i < jh; i++) {
for (int j = 0; j < jw; j++) {
// Get color value - > modify color value
// At < vec3b > (I, J); get pixel value, color value array
// Color value - > Blue
// Mat ﹣ image ﹣ clone. At < vec3b > (I, J) [0] means to obtain the blue value, saturate ﹣ cast < uchar > (), and intercept the data of uchar length
mat_image_clone.at<Vec3b>(i, j)[0] = saturate_cast<uchar>(
mat_image_dst.at<Vec3b>(i, j)[0] + jld);
// Color value - > Red
// Mat_image_clone. At < vec3b > (I, J) [1] means get blue value
mat_image_clone.at<Vec3b>(i, j)[1] = saturate_cast<uchar>(
mat_image_dst.at<Vec3b>(i, j)[1] + jld);
// Color value - > Green
// At < vec3b > (I, J) [2] means to get the blue value
mat_image_clone.at<Vec3b>(i, j)[2] = saturate_cast<uchar>(
mat_image_dst.at<Vec3b>(i, j)[2] + jld);
}
}
// Step 5: assign the modified data to the original mat - > mat? Image? SRC
cvtColor(mat_image_clone, mat_image_src, CV_RGB2RGBA, 4);
// Step 6: update Java array
// 0: after processing, free the memory of C
env->ReleaseIntArrayElements(jPixArr, cPixArr, 0);
}