Begin to learn OpenCV, frosted glass blur effect is currently popular online there are three ways:

  1. Using Java to write a long list of pixel processing algorithms to change bitmaps (poor performance, and a lot of algorithm code, difficult to understand, not elegant)
  2. The way to use C is to use the same algorithms as Java (good performance, same algorithmic code is difficult to understand and not elegant).
  3. RenderScript is limited by the Api version.

Now we can use the filtering algorithm in The OpenCV framework to realize the image blur.


Preparations:

Go to the official website of OpenCV and download the SDK package for Android platform: www.opencv.org

After decompression: SDK directory is some openCV dynamic libraries, cmake build files, and some Java API.





image.png

Create a new project that supports NDK:





Check c++ support.png




Choose the standard of stdC++11. PNG

Configure integrated OpenCV library to project:

• Build. Gradle for app/Module. • Build.





image.png

By the way, HERE I use the cmake tool of AS to build the link and compilation support of NDK library, so there is no need to write the configuration file of Android.mk, here I can configure the cmakelists. TXT, more simple:

# 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) # Add opencV library add_library(opencv_java3 SHARED) to add_library(image_process SHARED SRC /main/ CPP /image_process If (${ANDROID_ABI} STREQUAL "armeabi") # set_target_properties(opencv_java3 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libopencv_java3.so ) endif(${ANDROID_ABI} STREQUAL "armeabi") # opencV library header path set, in this case opencV-sdk path set, Of course you can also put the include directory is copied into engineering include_directories (D: / opencv - 3.2.0 - android SDK/opencv - android SDK/SDK/native/jni/include) find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want Target_link_libraries (# Specifies the target library.image_process opencv_java3 # Links the target library to the log library # included in the NDK. ${log-lib} )Copy the code

Add dependency library above, and their own to compile so library writing is similar, is these routines. Copy the libopencv_java3.so file from the SDK to the corresponding project directory. For convenience, otherwise you need to configure gradle to modify the source directory mapping path:





image.png




image.png

Write external development call apis for the Java layer

Public class ImageProcessUtils {/** * public static Bitmap * @param srcBitmap */ public static Bitmap Blur (Bitmap srcBitmap){int width = srcbitmap.getwidth (); blur(Bitmap srcBitmap){blur(Bitmap srcBitmap){int width = srcbitmap.getwidth (); int height = srcBitmap.getHeight(); Int [] pixels = new int[width * height]; Srcbitmap.getpixels (pixels, 0, width, 0, 0, width, height); // blurImage(Pixels, Width, height) through the JNI native method; NewBitmap = bitmap.createBitmap (width, height, bitmap.config. RGB_565); // Create a new image. Newbitmap. setPixels(pixels, 0, width, 0, 0, width, height); return newBitmap; Public static native void blurImage(int[] pixels, int w, int h); Static {system. loadLibrary("image_process"); System.loadLibrary("opencv_java3"); }}Copy the code

The next step is to use OpencV in NDK to achieve frosted glass of the image

#include <jni.h> #include <android/log.h> #include <opencv2/opencv. HPP < opencv2 / highgui/highgui HPP > / / introduction of opencv graphical interface, temporarily useless to / / defines the log log macro function, #define TAG "jerry-dk-image-pro" #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO , TAG, __VA_ARGS__) #define LOGW(...) __android_log_print(ANDROID_LOG_WARN , TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , TAG, __VA_ARGS__) using namespace cv; extern "C" JNIEXPORT void JNICALL Java_com_jerry_jerryopencvdemo_imageprocess_ImageProcessUtils_blurImage( JNIEnv *env, Jclass JCLS, jintArray jarr_pixels, Jint j_width, jint j_height) { Jint *c_pixels = env->GetIntArrayElements(jarr_pixels, JNI_FALSE); if(c_pixels == NULL){ return; } LOGE(" width: %d, height: %d", j_width, j_height); Mat mat_image_src(j_height, j_width, CV_8UC4, (unsigned char*) c_pixels); Mat temp = mat_image_src.rowRange(j_height / 3, 2 * j_height / 3); // boxFilter(temp, temp, -1, Size(85, 85)); Blur (temp, temp, Size(85, 85)); // GaussianBlur(temp, temp, Size(45, 13), 0, 0); CvtColor (temp, temp, CV_RGBA2GRAY, 4); cvtColor(temp, temp, CV_RGBA2GRAY, 4); Env ->ReleaseIntArrayElements(jarr_pixels, c_pixels, JNI_FALSE); }Copy the code

Take a look at the renderings and comparison:





The original image. The PNG




Image of frosted glass.png


Simple use of filtering algorithm function processing, to achieve the effect of frosted glass, of course, OpencV is far more powerful than this. Further learning about opencV will continue to be recorded in the blog.