a
554325746@qq.com
2019-12-25 84e391f79e4c298e31b990667a54d991d645949f
app/src/main/cpp/native-lib.cpp
@@ -5,6 +5,12 @@
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "turbojpeg.h"
#include "libyuv.h"
#define LOG_TAG "turbojpeg"
#define LOGW(...)  __android_log_write(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#include "FiStdDefEx.h"
#include "THFaceImage_i.h"
@@ -80,36 +86,42 @@
Java_com_basic_security_utils_FaceId_initSdk(
        JNIEnv *env, jobject, jstring jModelPath) {
    const char *modelPath = env->GetStringUTFChars(jModelPath, 0);
    bool init_sdk_success = false;
    THFI_Param detParam;
    detParam.nMinFaceSize = 50;
    detParam.nRollAngle = 30;
    detParam.bOnlyDetect = false;
    detParam.dwReserved = NULL;
    THFI_SetDir(modelPath, modelPath);
    int ret = THFI_Create(1, &detParam);
    if (ret < 0) {
        printf("THFI_Create failed!(ret=%d)\n", ret);
        init_sdk_success = false;
    }
//    THFL_SDK_SetDir(modelPath, modelPath);
//    ret = THFL_Create(1);
    jboolean success = false;
    success = FaceDetectHelper::getInstance()->initSdk(modelPath);
    return success;
//    bool init_sdk_success = false;
//    THFI_Param detParam;
//    detParam.nMinFaceSize = 50;
//    detParam.nRollAngle = 30;
//    detParam.bOnlyDetect = false;
//    detParam.dwReserved = NULL;
//    THFI_SetDir(modelPath, modelPath);
//    int ret = THFI_Create(1, &detParam);
//    if (ret < 0) {
//        printf("THFL_Create failed!(ret=%d)\n", ret);
//        printf("THFI_Create failed!(ret=%d)\n", ret);
//        init_sdk_success = false;
//    }
////    THFL_SDK_SetDir(modelPath, modelPath);
////    ret = THFL_Create(1);
////    if (ret < 0) {
////        printf("THFL_Create failed!(ret=%d)\n", ret);
////        THFI_Release();
////        init_sdk_success = false;
////    }
//    EF_SetDir(modelPath, modelPath);
//    ret = EF_Init(1);
//    if (ret < 0) {
//        printf("EF_Init failed!(ret=%d)\n", ret);
//        THFI_Release();
//        init_sdk_success = false;
//    }
    EF_SetDir(modelPath, modelPath);
    ret = EF_Init(1);
    if (ret < 0) {
        printf("EF_Init failed!(ret=%d)\n", ret);
        THFI_Release();
        init_sdk_success = false;
    }
//    featureSize = EF_Size();
//    feature1 = new BYTE[featureSize];
    init_sdk_success = true;
    return init_sdk_success;
//    init_sdk_success = true;
//    return init_sdk_success;
}
jint getIntValue(JNIEnv *env, jobject detectedResult, char *fieldName) {
@@ -282,6 +294,7 @@
        JNIEnv *env, jobject,jint channel, jstring jModelPath, jstring jRgbFileName, jint width, int height,
        jstring baseFeatureName, int shouldExtractFeature, bool useGrayCamera,
        int detectFaceCount, jbyteArray bgrArray) {
//    new char[80*1024*1024];
    string faces = FaceDetectHelper::getInstance()->detectRealFace4(env,
                                                                    channel, env->GetStringUTFChars(jModelPath, 0), env->GetStringUTFChars(jRgbFileName, 0), nullptr,
                                                                    width, height, env->GetStringUTFChars(baseFeatureName, 0), shouldExtractFeature,
@@ -310,6 +323,15 @@
        JNIEnv *env, jobject, jstring jpgFileName) {
    return FaceDetectHelper::getInstance()->extractFeature(env,
                                                           env->GetStringUTFChars(jpgFileName, 0));
}
extern "C" JNIEXPORT jstring
JNICALL
Java_com_basic_security_utils_FaceId_facePosition(
        JNIEnv *env, jobject, jstring jpgFileName) {
    string facePosition = FaceDetectHelper::getInstance()->facePosition(env,
                                                           env->GetStringUTFChars(jpgFileName, 0));
    return env->NewStringUTF(facePosition.c_str());
}
extern "C" JNIEXPORT jbyteArray
@@ -746,4 +768,79 @@
}
void cnv212yuv420(unsigned char *Src_data, unsigned char *Dst_data,
                  int src_width, int src_height) {
    int NV12_Y_Size = src_width * src_height;
    //dst:YUV420 video size
    int I420_Y_Size = src_width * src_height;
    int I420_U_Size = (src_width >> 1) * (src_height >> 1);
    // video format transformation process
    unsigned char *Y_data_Src = Src_data;
    unsigned char *UV_data_Src = Src_data + NV12_Y_Size;
    int src_stride_y = src_width;
    int src_stride_uv = src_width;
    unsigned char *Y_data_Dst = Dst_data;
    unsigned char *U_data_Dst = Dst_data + I420_Y_Size;
    unsigned char *V_data_Dst = Dst_data + I420_Y_Size + I420_U_Size;
    int Dst_Stride_Y = src_width;
    int Dst_Stride_U = src_width >> 1;
    int Dst_Stride_V = Dst_Stride_U;
    libyuv::NV21ToI420(Y_data_Src, src_stride_y,
                       UV_data_Src, src_stride_uv,
                       Y_data_Dst, Dst_Stride_Y,
                       U_data_Dst, Dst_Stride_U,
                       V_data_Dst, Dst_Stride_V,
                       src_width, src_height);
}
extern "C"
JNIEXPORT jint JNICALL Java_com_basic_security_utils_FaceId_yuv2jpeg
        (JNIEnv * env, jclass jclazz, jbyteArray jNv21,jbyteArray jI420, jint width, jint height,
         jint subsample, jbyteArray jpeg,jint quality,jint flags){
    tjhandle handle = NULL;
    int padding = 1; // 1或4均可,但不能是0
    int ret = 0;
    if((handle=tjInitCompress())==NULL){
        LOGI("tjInitCompress error");
        return 1001;
    }
//   if(env->GetArrayLength(jpeg) < (jsize)tjBufSizeYUV2(width, padding, height, subsample)){
//      LOGI("Destination buffer is not large enough");
//      return 1002;
//   }
    unsigned char *srcBuf = (unsigned char *)env->GetPrimitiveArrayCritical(jNv21, 0);
    unsigned char *i420Buf = (unsigned char *)env->GetPrimitiveArrayCritical(jI420, 0);
    cnv212yuv420(srcBuf,i420Buf,width,height);
    unsigned char *jpegBuf = (unsigned char *)env->GetPrimitiveArrayCritical(jpeg, 0);
    unsigned long compressSize;
    ret = tjCompressFromYUV(handle, i420Buf, width, padding, height,
                            subsample, &jpegBuf, &compressSize, quality, flags);
    tjDestroy(handle);
    env->ReleasePrimitiveArrayCritical(jNv21, srcBuf, 0);
    env->ReleasePrimitiveArrayCritical(jI420, i420Buf, 0);
    env->ReleasePrimitiveArrayCritical(jpeg, jpegBuf, 0);
//   tjFree(jpegBuf);
//   tjFree(srcBuf);
    jpegBuf=srcBuf=NULL;
    if (ret < 0) {
        LOGI("compress to jpeg failed: %d\n",ret);
        return ret;
    }
    ret = compressSize;
    return ret;
}