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;
}
static JNICallbackParam g_deviceHotPlugParam;
static JNICallbackParam g_deviceJNIParam;
static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName, HGPointer param)
{
(void)param;
if (NULL == g_deviceHotPlugParam.vm)
if (NULL == g_deviceJNIParam.vm)
{
return;
}
@ -179,11 +179,11 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
JNIEnv *env = NULL;
//获取当前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)
{
// 如果没有主动附加到jvm环境中获取到env
if (g_deviceHotPlugParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
if (g_deviceJNIParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{
return;
}
@ -192,19 +192,19 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
}
//通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_deviceHotPlugParam.obj);
jclass cls = env->GetObjectClass(g_deviceJNIParam.obj);
if (NULL != cls)
{
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceHotPlugEvent", "(ILjava/lang/String;)V");
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)
{
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");
if (NULL != g_deviceHotPlugParam.vm)
if (NULL != g_deviceJNIParam.vm)
{
printf("Already Init Device\n");
return (jboolean)HGFALSE;
}
env->GetJavaVM(&g_deviceHotPlugParam.vm);
g_deviceHotPlugParam.obj = env->NewGlobalRef(thiz);
env->GetJavaVM(&g_deviceJNIParam.vm);
g_deviceJNIParam.obj = env->NewGlobalRef(thiz);
HGBool ret = HGLib_InitDevice(DeviceHotPlugEventFunc, NULL);
printf("ret=%d\n", ret);
if (!ret)
{
env->DeleteGlobalRef(g_deviceHotPlugParam.obj);
g_deviceHotPlugParam.obj = NULL;
g_deviceHotPlugParam.vm = NULL;
env->DeleteGlobalRef(g_deviceJNIParam.obj);
g_deviceJNIParam.obj = NULL;
g_deviceJNIParam.vm = NULL;
}
printf("\n");
@ -241,7 +241,7 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_DeinitDe
(void)clazz;
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");
return (jboolean)HGFALSE;
@ -251,9 +251,9 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_DeinitDe
printf("ret=%d\n", ret);
if (ret)
{
env->DeleteGlobalRef(g_deviceHotPlugParam.obj);
g_deviceHotPlugParam.obj = NULL;
g_deviceHotPlugParam.vm = NULL;
env->DeleteGlobalRef(g_deviceJNIParam.obj);
g_deviceJNIParam.obj = NULL;
g_deviceJNIParam.vm = NULL;
}
printf("\n");
@ -304,11 +304,10 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_com_easing_java_HGScannerLib_GetD
return devNameList;
}
static std::map<HGLibDevice, JNICallbackParam> g_deviceParams;
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");
jboolean isCopy;
@ -318,14 +317,6 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_OpenDevice(
printf("dev=0x%p\n", dev);
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");
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(
JNIEnv* env, jclass clazz, jlong dev)
{
(void)env;
(void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_CloseDevice\n");
HGBool ret = HGLib_CloseDevice((HGLibDevice)dev);
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");
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)
{
(void)param;
std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceParams.find(device);
if (iter == g_deviceParams.end() || NULL == iter->second.vm)
if (NULL == g_deviceJNIParam.vm)
{
return;
}
@ -414,11 +395,11 @@ static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool e
JNIEnv *env = NULL;
//获取当前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)
{
// 如果没有主动附加到jvm环境中获取到env
if (iter->second.vm->AttachCurrentThread((void **)&env, NULL) != 0)
if (g_deviceJNIParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{
return;
}
@ -427,27 +408,26 @@ static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool e
}
//通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(iter->second.obj);
jclass cls = env->GetObjectClass(g_deviceJNIParam.obj);
if (NULL != cls)
{
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanEvent", "(JIZLjava/lang/String;)V");
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)
{
iter->second.vm->DetachCurrentThread();
g_deviceJNIParam.vm->DetachCurrentThread();
}
}
static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param)
{
(void)param;
std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceParams.find(device);
if (iter == g_deviceParams.end() || NULL == iter->second.vm)
if (NULL == g_deviceJNIParam.vm)
{
return;
}
@ -456,11 +436,11 @@ static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPo
JNIEnv *env = NULL;
//获取当前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)
{
// 如果没有主动附加到jvm环境中获取到env
if (iter->second.vm->AttachCurrentThread((void **)&env, NULL) != 0)
if (g_deviceJNIParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{
return;
}
@ -469,19 +449,19 @@ static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPo
}
//通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(iter->second.obj);
jclass cls = env->GetObjectClass(g_deviceJNIParam.obj);
if (NULL != cls)
{
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanImage", "(JJ)V");
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)
{
iter->second.vm->DetachCurrentThread();
g_deviceJNIParam.vm->DetachCurrentThread();
}
}