This commit is contained in:
luoliangyi 2023-11-04 10:03:44 +08:00
parent 24538f5d11
commit 62966eaf71
3 changed files with 71 additions and 55 deletions

View File

@ -1189,8 +1189,11 @@ int HGLibDeviceImpl::sane_ex_callback(SANE_Handle hdev, int code, void* data, un
(0 != *len), (const char*)data, deviceImpl->m_eventParam); (0 != *len), (const char*)data, deviceImpl->m_eventParam);
} }
HGLibDeviceScanEventFunc eventFunc = deviceImpl->m_eventFunc; if (NULL != deviceImpl->m_eventFunc)
HGPointer eventParam = deviceImpl->m_eventParam; {
deviceImpl->m_eventFunc((HGLibDevice)deviceImpl, HGLIB_DEVSCAN_EVENT_END,
HGFALSE, NULL, deviceImpl->m_eventParam);
}
HGBase_EnterLock(deviceImpl->m_lock); HGBase_EnterLock(deviceImpl->m_lock);
deviceImpl->m_eventFunc = NULL; deviceImpl->m_eventFunc = NULL;
@ -1200,12 +1203,6 @@ int HGLibDeviceImpl::sane_ex_callback(SANE_Handle hdev, int code, void* data, un
deviceImpl->m_scanning = HGFALSE; deviceImpl->m_scanning = HGFALSE;
deviceImpl->m_dpi = 0; deviceImpl->m_dpi = 0;
HGBase_LeaveLock(deviceImpl->m_lock); HGBase_LeaveLock(deviceImpl->m_lock);
if (NULL != eventFunc)
{
eventFunc((HGLibDevice)deviceImpl, HGLIB_DEVSCAN_EVENT_END,
HGFALSE, NULL, eventParam);
}
} }
break; break;
case SANE_EVENT_STATUS: case SANE_EVENT_STATUS:

View File

@ -1,5 +1,13 @@
#include <jni.h> #include <jni.h>
#include "HGScannerLib.h" #include "HGScannerLib.h"
#include <map>
#include <assert.h>
struct JNICallbackParam
{
JavaVM *vm;
jobject obj;
};
extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_LoadImage( extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_LoadImage(
JNIEnv* env, jclass clazz, jstring filePath) JNIEnv* env, jclass clazz, jstring filePath)
@ -143,13 +151,12 @@ extern "C" JNIEXPORT jobject JNICALL Java_com_easing_java_HGScannerLib_GetImageD
return imgDpi; return imgDpi;
} }
JavaVM *g_VMDeviceHotPlugEvent = NULL; static JNICallbackParam g_deviceHotPlugParam;
jobject g_objDeviceHotPlugEvent = NULL;
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_VMDeviceHotPlugEvent) if (NULL == g_deviceHotPlugParam.vm)
{ {
return; return;
} }
@ -158,11 +165,11 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_VMDeviceHotPlugEvent->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = g_deviceHotPlugParam.vm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (g_VMDeviceHotPlugEvent->AttachCurrentThread((void **)&env, NULL) != 0) if (g_deviceHotPlugParam.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -171,19 +178,19 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_objDeviceHotPlugEvent); jclass cls = env->GetObjectClass(g_deviceHotPlugParam.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_objDeviceHotPlugEvent, jmethodId, (jint)event, env->NewStringUTF(deviceName)); env->CallVoidMethod(g_deviceHotPlugParam.obj, jmethodId, (jint)event, env->NewStringUTF(deviceName));
} }
} }
if (needDetach) if (needDetach)
{ {
g_VMDeviceHotPlugEvent->DetachCurrentThread(); g_deviceHotPlugParam.vm->DetachCurrentThread();
} }
} }
@ -192,22 +199,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_VMDeviceHotPlugEvent) if (NULL != g_deviceHotPlugParam.vm)
{ {
printf("Already Init Device\n"); printf("Already Init Device\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
} }
env->GetJavaVM(&g_VMDeviceHotPlugEvent); env->GetJavaVM(&g_deviceHotPlugParam.vm);
g_objDeviceHotPlugEvent = env->NewGlobalRef(thiz); g_deviceHotPlugParam.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_objDeviceHotPlugEvent); env->DeleteGlobalRef(g_deviceHotPlugParam.obj);
g_objDeviceHotPlugEvent = NULL; g_deviceHotPlugParam.obj = NULL;
g_VMDeviceHotPlugEvent = NULL; g_deviceHotPlugParam.vm = NULL;
} }
printf("\n"); printf("\n");
@ -220,7 +227,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_VMDeviceHotPlugEvent) if (NULL == g_deviceHotPlugParam.vm)
{ {
printf("Do not Init Device yet\n"); printf("Do not Init Device yet\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
@ -230,9 +237,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_objDeviceHotPlugEvent); env->DeleteGlobalRef(g_deviceHotPlugParam.obj);
g_objDeviceHotPlugEvent = NULL; g_deviceHotPlugParam.obj = NULL;
g_VMDeviceHotPlugEvent = NULL; g_deviceHotPlugParam.vm = NULL;
} }
printf("\n"); printf("\n");
@ -365,13 +372,13 @@ extern "C" JNIEXPORT jstring JNICALL Java_com_easing_java_HGScannerLib_GetDevice
return fwVersion; return fwVersion;
} }
JavaVM *g_VMDeviceScan = NULL; static std::map<HGLibDevice, JNICallbackParam> g_deviceScanParams;
jobject g_objDeviceScan = NULL;
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;
if (NULL == g_VMDeviceScan) std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceScanParams.find(device);
if (iter == g_deviceScanParams.end() || NULL == iter->second.vm)
{ {
return; return;
} }
@ -380,11 +387,11 @@ static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool e
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_VMDeviceScan->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = iter->second.vm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (g_VMDeviceScan->AttachCurrentThread((void **)&env, NULL) != 0) if (iter->second.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -393,35 +400,37 @@ static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool e
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_objDeviceScan); jclass cls = env->GetObjectClass(iter->second.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(g_objDeviceScan, jmethodId, (jlong)device, (jint)event, (jboolean)err, env->NewStringUTF(info)); env->CallVoidMethod(iter->second.obj, jmethodId, (jlong)device, (jint)event, (jboolean)err, env->NewStringUTF(info));
} }
} }
if (needDetach) if (needDetach)
{ {
g_VMDeviceScan->DetachCurrentThread(); iter->second.vm->DetachCurrentThread();
} }
if (HGLIB_DEVSCAN_EVENT_END == event) if (HGLIB_DEVSCAN_EVENT_END == event)
{ {
printf("HGLIB_DEVSCAN_EVENT_END == event\n"); printf("HGLIB_DEVSCAN_EVENT_END == event\n");
env->DeleteGlobalRef(g_objDeviceScan); env->DeleteGlobalRef(iter->second.obj);
g_objDeviceScan = NULL; iter->second.obj = NULL;
g_VMDeviceScan = NULL; iter->second.vm = NULL;
g_deviceScanParams.erase(iter);
} }
} }
static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param) static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param)
{ {
(void)param; (void)param;
if (NULL == g_VMDeviceScan) std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceScanParams.find(device);
if (iter == g_deviceScanParams.end() || NULL == iter->second.vm)
{ {
return; return;
} }
@ -430,11 +439,11 @@ static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPo
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_VMDeviceScan->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = iter->second.vm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (g_VMDeviceScan->AttachCurrentThread((void **)&env, NULL) != 0) if (iter->second.vm->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -443,19 +452,19 @@ static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPo
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_objDeviceScan); jclass cls = env->GetObjectClass(iter->second.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(g_objDeviceScan, jmethodId, (jlong)device, (jlong)image); env->CallVoidMethod(iter->second.obj, jmethodId, (jlong)device, (jlong)image);
} }
} }
if (needDetach) if (needDetach)
{ {
g_VMDeviceScan->DetachCurrentThread(); iter->second.vm->DetachCurrentThread();
} }
} }
@ -464,22 +473,29 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_StartDev
{ {
printf("\nJava_com_easing_java_HGScannerLib_StartDeviceScan\n"); printf("\nJava_com_easing_java_HGScannerLib_StartDeviceScan\n");
if (NULL != g_VMDeviceScan) std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceScanParams.find((HGLibDevice)dev);
if (iter != g_deviceScanParams.end())
{ {
printf("Already Start Device Scan\n"); printf("Already Start Device Scan\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
} }
env->GetJavaVM(&g_VMDeviceScan); JNICallbackParam deviceScanParam;
g_objDeviceScan = env->NewGlobalRef(thiz); env->GetJavaVM(&deviceScanParam.vm);
deviceScanParam.obj = env->NewGlobalRef(thiz);
g_deviceScanParams[(HGLibDevice)dev] = deviceScanParam;
HGBool ret = HGLib_StartDeviceScan((HGLibDevice)dev, DeviceScanEventFunc, NULL, DeviceScanImageFunc, NULL); HGBool ret = HGLib_StartDeviceScan((HGLibDevice)dev, DeviceScanEventFunc, NULL, DeviceScanImageFunc, NULL);
printf("ret=%d\n", ret); printf("ret=%d\n", ret);
if (!ret) if (!ret)
{ {
env->DeleteGlobalRef(g_objDeviceScan); std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceScanParams.find((HGLibDevice)dev);
g_objDeviceScan = NULL; assert(iter != g_deviceScanParams.end());
g_VMDeviceScan = NULL;
env->DeleteGlobalRef(iter->second.obj);
iter->second.obj = NULL;
iter->second.vm = NULL;
g_deviceScanParams.erase(iter);
} }
printf("\n"); printf("\n");
@ -492,7 +508,8 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_StopDevi
(void)clazz; (void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_StopDeviceScan\n"); printf("\nJava_com_easing_java_HGScannerLib_StopDeviceScan\n");
if (NULL == g_VMDeviceScan) std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceScanParams.find((HGLibDevice)dev);
if (iter == g_deviceScanParams.end())
{ {
printf("Do not Start Device Scan yet\n"); printf("Do not Start Device Scan yet\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
@ -502,11 +519,13 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_StopDevi
printf("ret=%d\n", ret); printf("ret=%d\n", ret);
if (ret) if (ret)
{ {
if (NULL != g_VMDeviceScan) std::map<HGLibDevice, JNICallbackParam>::iterator iter = g_deviceScanParams.find((HGLibDevice)dev);
if (iter != g_deviceScanParams.end())
{ {
env->DeleteGlobalRef(g_objDeviceScan); env->DeleteGlobalRef(iter->second.obj);
g_objDeviceScan = NULL; iter->second.obj = NULL;
g_VMDeviceScan = NULL; iter->second.vm = NULL;
g_deviceScanParams.erase(iter);
} }
} }

View File

@ -112,7 +112,7 @@ public class HGScannerLib {
inst.StartDeviceScan(dev); inst.StartDeviceScan(dev);
inst.StopDeviceScan(dev); //inst.StopDeviceScan(dev);
inst.CloseDevice(dev); inst.CloseDevice(dev);
} }