This commit is contained in:
luoliangyi 2023-11-23 15:51:22 +08:00
parent f6cb1d072b
commit 4cfa003d35
1 changed files with 32 additions and 52 deletions

View File

@ -165,12 +165,12 @@ extern "C" JNIEXPORT jobject JNICALL Java_com_easing_java_HGScannerLib_GetImageD
return imgDpi; return imgDpi;
} }
static JNICallbackParam g_deviceHotPlugParam; static JNICallbackParam g_deviceJNIParam;
static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName, HGPointer param) static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName, HGPointer param)
{ {
(void)param; (void)param;
if (NULL == g_deviceHotPlugParam.vm) if (NULL == g_deviceJNIParam.vm)
{ {
return; return;
} }
@ -179,11 +179,11 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_deviceHotPlugParam.vm->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = g_deviceJNIParam.vm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (g_deviceHotPlugParam.vm->AttachCurrentThread((void **)&env, NULL) != 0) if (g_deviceJNIParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -192,19 +192,19 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_deviceHotPlugParam.obj); jclass cls = env->GetObjectClass(g_deviceJNIParam.obj);
if (NULL != cls) if (NULL != cls)
{ {
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceHotPlugEvent", "(ILjava/lang/String;)V"); jmethodID jmethodId = env->GetMethodID(cls, "onDeviceHotPlugEvent", "(ILjava/lang/String;)V");
if (NULL != jmethodId) if (NULL != jmethodId)
{ {
env->CallVoidMethod(g_deviceHotPlugParam.obj, jmethodId, (jint)event, env->NewStringUTF(deviceName)); env->CallVoidMethod(g_deviceJNIParam.obj, jmethodId, (jint)event, env->NewStringUTF(deviceName));
} }
} }
if (needDetach) if (needDetach)
{ {
g_deviceHotPlugParam.vm->DetachCurrentThread(); g_deviceJNIParam.vm->DetachCurrentThread();
} }
} }
@ -213,22 +213,22 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_InitDevi
{ {
printf("\nJava_com_easing_java_HGScannerLib_InitDevice\n"); printf("\nJava_com_easing_java_HGScannerLib_InitDevice\n");
if (NULL != g_deviceHotPlugParam.vm) if (NULL != g_deviceJNIParam.vm)
{ {
printf("Already Init Device\n"); printf("Already Init Device\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
} }
env->GetJavaVM(&g_deviceHotPlugParam.vm); env->GetJavaVM(&g_deviceJNIParam.vm);
g_deviceHotPlugParam.obj = env->NewGlobalRef(thiz); g_deviceJNIParam.obj = env->NewGlobalRef(thiz);
HGBool ret = HGLib_InitDevice(DeviceHotPlugEventFunc, NULL); HGBool ret = HGLib_InitDevice(DeviceHotPlugEventFunc, NULL);
printf("ret=%d\n", ret); printf("ret=%d\n", ret);
if (!ret) if (!ret)
{ {
env->DeleteGlobalRef(g_deviceHotPlugParam.obj); env->DeleteGlobalRef(g_deviceJNIParam.obj);
g_deviceHotPlugParam.obj = NULL; g_deviceJNIParam.obj = NULL;
g_deviceHotPlugParam.vm = NULL; g_deviceJNIParam.vm = NULL;
} }
printf("\n"); printf("\n");
@ -241,7 +241,7 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_DeinitDe
(void)clazz; (void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_DeinitDevice\n"); printf("\nJava_com_easing_java_HGScannerLib_DeinitDevice\n");
if (NULL == g_deviceHotPlugParam.vm) if (NULL == g_deviceJNIParam.vm)
{ {
printf("Do not Init Device yet\n"); printf("Do not Init Device yet\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
@ -251,9 +251,9 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_DeinitDe
printf("ret=%d\n", ret); printf("ret=%d\n", ret);
if (ret) if (ret)
{ {
env->DeleteGlobalRef(g_deviceHotPlugParam.obj); env->DeleteGlobalRef(g_deviceJNIParam.obj);
g_deviceHotPlugParam.obj = NULL; g_deviceJNIParam.obj = NULL;
g_deviceHotPlugParam.vm = NULL; g_deviceJNIParam.vm = NULL;
} }
printf("\n"); printf("\n");
@ -304,11 +304,10 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_com_easing_java_HGScannerLib_GetD
return devNameList; return devNameList;
} }
static std::map<HGLibDevice, JNICallbackParam> g_deviceParams;
extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_OpenDevice( extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_OpenDevice(
JNIEnv* env, jobject thiz, jstring deviceName) JNIEnv* env, jclass clazz, jstring deviceName)
{ {
(void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_OpenDevice\n"); printf("\nJava_com_easing_java_HGScannerLib_OpenDevice\n");
jboolean isCopy; jboolean isCopy;
@ -318,14 +317,6 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_OpenDevice(
printf("dev=0x%p\n", dev); printf("dev=0x%p\n", dev);
env->ReleaseStringUTFChars(deviceName, ptr); env->ReleaseStringUTFChars(deviceName, ptr);
if (NULL != dev)
{
JNICallbackParam deviceParam;
env->GetJavaVM(&deviceParam.vm);
deviceParam.obj = env->NewGlobalRef(thiz);
g_deviceParams[(HGLibDevice)dev] = deviceParam;
}
printf("\n"); printf("\n");
return (jlong)dev; return (jlong)dev;
} }
@ -333,21 +324,12 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_OpenDevice(
extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_CloseDevice( extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_CloseDevice(
JNIEnv* env, jclass clazz, jlong dev) JNIEnv* env, jclass clazz, jlong dev)
{ {
(void)env;
(void)clazz; (void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_CloseDevice\n"); printf("\nJava_com_easing_java_HGScannerLib_CloseDevice\n");
HGBool ret = HGLib_CloseDevice((HGLibDevice)dev); HGBool ret = HGLib_CloseDevice((HGLibDevice)dev);
printf("ret=%d\n", ret); printf("ret=%d\n", ret);
if (ret)
{
std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceParams.find((HGLibDevice)dev);
assert(iter != g_deviceParams.end());
env->DeleteGlobalRef(iter->second.obj);
iter->second.obj = NULL;
iter->second.vm = NULL;
g_deviceParams.erase(iter);
}
printf("\n"); printf("\n");
return (jboolean)ret; return (jboolean)ret;
@ -404,8 +386,7 @@ extern "C" JNIEXPORT jstring JNICALL Java_com_easing_java_HGScannerLib_GetDevice
static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool err, const HGChar *info, HGPointer param) static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool err, const HGChar *info, HGPointer param)
{ {
(void)param; (void)param;
std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceParams.find(device); if (NULL == g_deviceJNIParam.vm)
if (iter == g_deviceParams.end() || NULL == iter->second.vm)
{ {
return; return;
} }
@ -414,11 +395,11 @@ static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool e
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = iter->second.vm->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = g_deviceJNIParam.vm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (iter->second.vm->AttachCurrentThread((void **)&env, NULL) != 0) if (g_deviceJNIParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -427,27 +408,26 @@ static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool e
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(iter->second.obj); jclass cls = env->GetObjectClass(g_deviceJNIParam.obj);
if (NULL != cls) if (NULL != cls)
{ {
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanEvent", "(JIZLjava/lang/String;)V"); jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanEvent", "(JIZLjava/lang/String;)V");
if (NULL != jmethodId) if (NULL != jmethodId)
{ {
env->CallVoidMethod(iter->second.obj, jmethodId, (jlong)device, (jint)event, (jboolean)err, env->NewStringUTF(info)); env->CallVoidMethod(g_deviceJNIParam.obj, jmethodId, (jlong)device, (jint)event, (jboolean)err, env->NewStringUTF(info));
} }
} }
if (needDetach) if (needDetach)
{ {
iter->second.vm->DetachCurrentThread(); g_deviceJNIParam.vm->DetachCurrentThread();
} }
} }
static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param) static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param)
{ {
(void)param; (void)param;
std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceParams.find(device); if (NULL == g_deviceJNIParam.vm)
if (iter == g_deviceParams.end() || NULL == iter->second.vm)
{ {
return; return;
} }
@ -456,11 +436,11 @@ static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPo
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = iter->second.vm->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = g_deviceJNIParam.vm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (iter->second.vm->AttachCurrentThread((void **)&env, NULL) != 0) if (g_deviceJNIParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -469,19 +449,19 @@ static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPo
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(iter->second.obj); jclass cls = env->GetObjectClass(g_deviceJNIParam.obj);
if (NULL != cls) if (NULL != cls)
{ {
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanImage", "(JJ)V"); jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanImage", "(JJ)V");
if (NULL != jmethodId) if (NULL != jmethodId)
{ {
env->CallVoidMethod(iter->second.obj, jmethodId, (jlong)device, (jlong)image); env->CallVoidMethod(g_deviceJNIParam.obj, jmethodId, (jlong)device, (jlong)image);
} }
} }
if (needDetach) if (needDetach)
{ {
iter->second.vm->DetachCurrentThread(); g_deviceJNIParam.vm->DetachCurrentThread();
} }
} }