感知哈希算法android ndk OpenCV

2019-05-23  本文已影响0人  MacLi

\app\src\main\cpp\native-lib.cpp
传入图片预览数据

extern "C"
JNIEXPORT jlong JNICALL
Java_com_junyang_baiduai_Main3Activity_getPhotoArray2(JNIEnv *env, jobject instance,
                                                      jbyteArray data_, jint w, jint h,
                                                      jint cameraId) {
    jbyte *data = env->GetByteArrayElements(data_, NULL);
    Mat matSrc(h, w, CV_8UC1, data), matDst1;
    cvtColor(matSrc, matSrc, COLOR_YUV2RGBA_NV21);
    int iAvg1 = 0;
    int arr1[64];
    if (!matSrc.data) {
        env->ReleaseByteArrayElements(data_, data, 0);
        return 0;
    }
    cv::resize(matSrc, matDst1, cv::Size(8, 8), 0, 0, cv::INTER_CUBIC);
    cv::cvtColor(matDst1, matDst1, CV_BGR2GRAY);
    for (int i = 0; i < 8; i++) {
        uchar *data1 = matDst1.ptr<uchar>(i);
        int tmp = i * 8;
        for (int j = 0; j < 8; j++) {
            int tmp1 = tmp + j;
            arr1[tmp1] = data1[j] / 4 * 4;
            iAvg1 += arr1[tmp1];
        }
    }
    iAvg1 /= 64;
    jlong p = 1;
    jlong value = 0;
    for (int i = 0; i < 64; i++) {
        if (arr1[i] >= iAvg1) {
            value += p;
        }
        p *= 2;
    }
    env->ReleaseByteArrayElements(data_, data, 0);
    return value;
}

传入图片文件路径:

extern "C"
JNIEXPORT jlong JNICALL
Java_com_junyang_baiduai_Main3Activity_getPhotoArray(JNIEnv *env, jobject instance, jstring strSrcImageName) {
    char *imagPath = NULL;
    jclass clsstring = env->FindClass("java/lang/String");
    jstring strencode = env->NewStringUTF("utf-8");
    jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
    jbyteArray barr = (jbyteArray) env->CallObjectMethod(strSrcImageName, mid, strencode);
    jsize alen = env->GetArrayLength(barr);
    jbyte *ba = env->GetByteArrayElements(barr, JNI_FALSE);
    if (alen > 0) {
        imagPath = (char *) malloc(alen + 1);
        memcpy(imagPath, ba, alen);
        imagPath[alen] = 0;
    }
    env->ReleaseByteArrayElements(barr, ba, 0);
    Mat matSrc, matDst1;
    matSrc = cv::imread(imagPath, CV_LOAD_IMAGE_COLOR);
    int iAvg1 = 0;
    int arr1[64];
    if (!matSrc.data) {
        return 0;
    }
    cv::resize(matSrc, matDst1, cv::Size(8, 8), 0, 0, cv::INTER_CUBIC);
    cv::cvtColor(matDst1, matDst1, CV_BGR2GRAY);
    for (int i = 0; i < 8; i++) {
        uchar *data1 = matDst1.ptr<uchar>(i);
        int tmp = i * 8;
        for (int j = 0; j < 8; j++) {
            int tmp1 = tmp + j;
            arr1[tmp1] = data1[j] / 4 * 4;
            iAvg1 += arr1[tmp1];
        }
    }
    iAvg1 /= 64;
    jlong p = 1;
    jlong value = 0;
    for (int i = 0; i < 64; i++) {
        if (arr1[i] >= iAvg1) {
            value += p;
        }
        p *= 2;
    }
    return value;
}
上一篇下一篇

猜你喜欢

热点阅读