1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include "com_xiasuhuei321_studyforopencv_OpenCVHelper.h" #include <stdio.h> #include <stdlib.h> #include <android/log.h> #include <opencv2/opencv.hpp>
#define TAG "Jerry-NDK-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 jintArray JNICALL Java_com_xiasuhuei321_studyforopencv_OpenCVHelper_gray (JNIEnv *, jclass, jintArray, jint, jint);
JNIEXPORT jintArray JNICALL Java_com_xiasuhuei321_studyforopencv_OpenCVHelper_gray(JNIEnv *env, jclass obj, jintArray buf, int w, int h) { jint *cbuf; cbuf = env->GetIntArrayElements(buf, JNI_FALSE); if (NULL == cbuf) { return 0; }
Mat imgData(h, w, CV_8UC4, (unsigned char *) cbuf);
u_char *ptr = imgData.ptr(0); for (int i = 0; i < w * h; ++i) { int grayScale = (int) (ptr[4 * i + 2] * 0.299 + ptr[4 * i + 1] * 0.587 + ptr[4 * i + 0] * 0.144); ptr[4 * i + 0] = grayScale; ptr[4 * i + 1] = grayScale; ptr[4 * i + 2] = grayScale; }
int size = w * h; jintArray result = env->NewIntArray(size); env->SetIntArrayRegion(result, 0, size, cbuf); env->ReleaseIntArrayElements(buf, cbuf, 0); return result; }
}
extern "C" JNIEXPORT void JNICALL Java_com_xiasuhuei321_studyforopencv_OpenCVHelper_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("图片宽度:%d, 高度:%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);
blur(temp, temp, Size(85, 85));
cvtColor(temp, temp, CV_RGBA2GRAY, 4);
env->ReleaseIntArrayElements(jarr_pixels, c_pixels, JNI_FALSE); }
|