scannerlib jni集成扫描功能

This commit is contained in:
luoliangyi 2023-11-03 18:07:27 +08:00
parent 72dcc5813d
commit 24538f5d11
5 changed files with 289 additions and 45 deletions

View File

@ -41,4 +41,10 @@ Java_com_easing_java_HGScannerLib_GetImageDpi
Java_com_easing_java_HGScannerLib_InitDevice Java_com_easing_java_HGScannerLib_InitDevice
Java_com_easing_java_HGScannerLib_DeinitDevice Java_com_easing_java_HGScannerLib_DeinitDevice
Java_com_easing_java_HGScannerLib_GetDeviceNameList Java_com_easing_java_HGScannerLib_GetDeviceNameList
Java_com_easing_java_HGScannerLib_OpenDevice
Java_com_easing_java_HGScannerLib_CloseDevice
Java_com_easing_java_HGScannerLib_GetDeviceSN
Java_com_easing_java_HGScannerLib_GetDeviceFWVersion
Java_com_easing_java_HGScannerLib_StartDeviceScan
Java_com_easing_java_HGScannerLib_StopDeviceScan

View File

@ -234,7 +234,6 @@ HGLibDeviceImpl::HGLibDeviceImpl()
m_devName.clear(); m_devName.clear();
m_dpi = 0; m_dpi = 0;
m_scanning = HGFALSE; m_scanning = HGFALSE;
m_scanEvent = NULL;
m_eventFunc = NULL; m_eventFunc = NULL;
m_eventParam = NULL; m_eventParam = NULL;
m_imageFunc = NULL; m_imageFunc = NULL;
@ -1011,8 +1010,6 @@ HGBool HGLibDeviceImpl::StartScan(HGLibDeviceScanEventFunc eventFunc, HGPointer
ReleaseParam(devParam); ReleaseParam(devParam);
} }
HGBase_CreateEvent(HGFALSE, HGFALSE, &m_scanEvent);
assert(NULL != m_scanEvent);
m_eventFunc = eventFunc; m_eventFunc = eventFunc;
m_eventParam = eventParam; m_eventParam = eventParam;
m_imageFunc = imageFunc; m_imageFunc = imageFunc;
@ -1027,8 +1024,6 @@ HGBool HGLibDeviceImpl::StartScan(HGLibDeviceScanEventFunc eventFunc, HGPointer
m_eventParam = NULL; m_eventParam = NULL;
m_imageFunc = NULL; m_imageFunc = NULL;
m_imageParam = NULL; m_imageParam = NULL;
HGBase_DestroyEvent(m_scanEvent);
m_scanEvent = NULL;
m_dpi = 0; m_dpi = 0;
} }
else else
@ -1045,13 +1040,10 @@ HGBool HGLibDeviceImpl::StopScan()
{ {
HGBool ret = HGFALSE; HGBool ret = HGFALSE;
sane_cancel(m_devHandle);
HGBase_EnterLock(m_lock); HGBase_EnterLock(m_lock);
if (m_scanning) if (m_scanning)
{ {
sane_cancel(m_devHandle);
HGBase_WaitEvent(m_scanEvent);
HGBase_DestroyEvent(m_scanEvent);
m_scanEvent = NULL;
m_eventFunc = NULL; m_eventFunc = NULL;
m_eventParam = NULL; m_eventParam = NULL;
m_imageFunc = NULL; m_imageFunc = NULL;
@ -1067,17 +1059,7 @@ HGBool HGLibDeviceImpl::StopScan()
HGBool HGLibDeviceImpl::StopScanAsyn() HGBool HGLibDeviceImpl::StopScanAsyn()
{ {
HGBool ret = HGFALSE; return StopScan();
HGBase_EnterLock(m_lock);
if (m_scanning)
{
sane_cancel(m_devHandle);
ret = HGTRUE;
}
HGBase_LeaveLock(m_lock);
return ret;
} }
HGInt HGLibDeviceImpl::GetOperateCode() HGInt HGLibDeviceImpl::GetOperateCode()
@ -1209,12 +1191,8 @@ int HGLibDeviceImpl::sane_ex_callback(SANE_Handle hdev, int code, void* data, un
HGLibDeviceScanEventFunc eventFunc = deviceImpl->m_eventFunc; HGLibDeviceScanEventFunc eventFunc = deviceImpl->m_eventFunc;
HGPointer eventParam = deviceImpl->m_eventParam; HGPointer eventParam = deviceImpl->m_eventParam;
HGBase_SetEvent(deviceImpl->m_scanEvent);
HGBase_EnterLock(deviceImpl->m_lock); HGBase_EnterLock(deviceImpl->m_lock);
HGBase_DestroyEvent(deviceImpl->m_scanEvent);
deviceImpl->m_scanEvent = NULL;
deviceImpl->m_eventFunc = NULL; deviceImpl->m_eventFunc = NULL;
deviceImpl->m_eventParam = NULL; deviceImpl->m_eventParam = NULL;
deviceImpl->m_imageFunc = NULL; deviceImpl->m_imageFunc = NULL;

View File

@ -61,7 +61,6 @@ private:
std::string m_devName; std::string m_devName;
HGInt m_dpi; HGInt m_dpi;
HGBool m_scanning; HGBool m_scanning;
HGEvent m_scanEvent;
HGLibDeviceScanEventFunc m_eventFunc; HGLibDeviceScanEventFunc m_eventFunc;
HGPointer m_eventParam; HGPointer m_eventParam;
HGLibDeviceScanImageFunc m_imageFunc; HGLibDeviceScanImageFunc m_imageFunc;
@ -69,4 +68,4 @@ private:
HGInt m_statusCode; HGInt m_statusCode;
}; };
#endif /* __HGLIBDEVICEIMPL_HPP__ */ #endif /* __HGLIBDEVICEIMPL_HPP__ */

View File

@ -143,13 +143,13 @@ extern "C" JNIEXPORT jobject JNICALL Java_com_easing_java_HGScannerLib_GetImageD
return imgDpi; return imgDpi;
} }
JavaVM *g_VMDeviceHotPlug = NULL; JavaVM *g_VMDeviceHotPlugEvent = NULL;
jobject g_objDeviceHotPlug = NULL; 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_VMDeviceHotPlug) if (NULL == g_VMDeviceHotPlugEvent)
{ {
return; return;
} }
@ -158,11 +158,11 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
JNIEnv *env = NULL; JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中 //获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_VMDeviceHotPlug->GetEnv((void **)&env, JNI_VERSION_1_6); int getEnvStat = g_VMDeviceHotPlugEvent->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) if (getEnvStat == JNI_EDETACHED)
{ {
// 如果没有主动附加到jvm环境中获取到env // 如果没有主动附加到jvm环境中获取到env
if (g_VMDeviceHotPlug->AttachCurrentThread((void **)&env, NULL) != 0) if (g_VMDeviceHotPlugEvent->AttachCurrentThread((void **)&env, NULL) != 0)
{ {
return; return;
} }
@ -171,19 +171,19 @@ static void HGAPI DeviceHotPlugEventFunc(HGUInt event, const HGChar *deviceName,
} }
//通过全局变量g_obj获取到要回调的类 //通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_objDeviceHotPlug); jclass cls = env->GetObjectClass(g_objDeviceHotPlugEvent);
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_objDeviceHotPlug, jmethodId, (jint)event, env->NewStringUTF(deviceName)); env->CallVoidMethod(g_objDeviceHotPlugEvent, jmethodId, (jint)event, env->NewStringUTF(deviceName));
} }
} }
if (needDetach) if (needDetach)
{ {
g_VMDeviceHotPlug->DetachCurrentThread(); g_VMDeviceHotPlugEvent->DetachCurrentThread();
} }
} }
@ -192,22 +192,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_VMDeviceHotPlug) if (NULL != g_VMDeviceHotPlugEvent)
{ {
printf("Already Init Device\n"); printf("Already Init Device\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
} }
env->GetJavaVM(&g_VMDeviceHotPlug); env->GetJavaVM(&g_VMDeviceHotPlugEvent);
g_objDeviceHotPlug = env->NewGlobalRef(thiz); g_objDeviceHotPlugEvent = 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_objDeviceHotPlug); env->DeleteGlobalRef(g_objDeviceHotPlugEvent);
g_objDeviceHotPlug = NULL; g_objDeviceHotPlugEvent = NULL;
g_VMDeviceHotPlug = NULL; g_VMDeviceHotPlugEvent = NULL;
} }
printf("\n"); printf("\n");
@ -220,7 +220,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_VMDeviceHotPlug) if (NULL == g_VMDeviceHotPlugEvent)
{ {
printf("Do not Init Device yet\n"); printf("Do not Init Device yet\n");
return (jboolean)HGFALSE; return (jboolean)HGFALSE;
@ -230,9 +230,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_objDeviceHotPlug); env->DeleteGlobalRef(g_objDeviceHotPlugEvent);
g_objDeviceHotPlug = NULL; g_objDeviceHotPlugEvent = NULL;
g_VMDeviceHotPlug = NULL; g_VMDeviceHotPlugEvent = NULL;
} }
printf("\n"); printf("\n");
@ -285,3 +285,231 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_com_easing_java_HGScannerLib_GetD
printf("\n"); printf("\n");
return devNameList; return devNameList;
} }
extern "C" JNIEXPORT jlong JNICALL Java_com_easing_java_HGScannerLib_OpenDevice(
JNIEnv* env, jclass clazz, jstring deviceName)
{
(void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_OpenDevice\n");
jboolean isCopy;
const char* ptr = env->GetStringUTFChars(deviceName, &isCopy);
printf("deviceName=%s\n", ptr);
HGLibDevice dev = HGLib_OpenDevice(ptr);
printf("dev=0x%p\n", dev);
env->ReleaseStringUTFChars(deviceName, ptr);
printf("\n");
return (jlong)dev;
}
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);
printf("\n");
return (jboolean)ret;
}
extern "C" JNIEXPORT jstring JNICALL Java_com_easing_java_HGScannerLib_GetDeviceSN(
JNIEnv* env, jclass clazz, jlong dev)
{
(void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_GetDeviceSN\n");
jstring sn;
HGChar sn2[256];
HGBool ret = HGLib_GetDeviceSN((HGLibDevice)dev, sn2, 256);
printf("ret=%d\n", ret);
if (ret)
{
sn = env->NewStringUTF(sn2);
}
else
{
sn = env->NewStringUTF(NULL);
}
printf("\n");
return sn;
}
extern "C" JNIEXPORT jstring JNICALL Java_com_easing_java_HGScannerLib_GetDeviceFWVersion(
JNIEnv* env, jclass clazz, jlong dev)
{
(void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_GetDeviceFWVersion\n");
jstring fwVersion;
HGChar fwVersion2[256];
HGBool ret = HGLib_GetDeviceFWVersion((HGLibDevice)dev, fwVersion2, 256);
printf("ret=%d\n", ret);
if (ret)
{
fwVersion = env->NewStringUTF(fwVersion2);
}
else
{
fwVersion = env->NewStringUTF(NULL);
}
printf("\n");
return fwVersion;
}
JavaVM *g_VMDeviceScan = NULL;
jobject g_objDeviceScan = NULL;
static void HGAPI DeviceScanEventFunc(HGLibDevice device, HGUInt event, HGBool err, const HGChar *info, HGPointer param)
{
(void)param;
if (NULL == g_VMDeviceScan)
{
return;
}
HGBool needDetach = HGFALSE;
JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_VMDeviceScan->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED)
{
// 如果没有主动附加到jvm环境中获取到env
if (g_VMDeviceScan->AttachCurrentThread((void **)&env, NULL) != 0)
{
return;
}
needDetach = HGTRUE;
}
//通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_objDeviceScan);
if (NULL != cls)
{
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanEvent", "(JIZLjava/lang/String;)V");
if (NULL != jmethodId)
{
env->CallVoidMethod(g_objDeviceScan, jmethodId, (jlong)device, (jint)event, (jboolean)err, env->NewStringUTF(info));
}
}
if (needDetach)
{
g_VMDeviceScan->DetachCurrentThread();
}
if (HGLIB_DEVSCAN_EVENT_END == event)
{
printf("HGLIB_DEVSCAN_EVENT_END == event\n");
env->DeleteGlobalRef(g_objDeviceScan);
g_objDeviceScan = NULL;
g_VMDeviceScan = NULL;
}
}
static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param)
{
(void)param;
if (NULL == g_VMDeviceScan)
{
return;
}
HGBool needDetach = HGFALSE;
JNIEnv *env = NULL;
//获取当前native线程是否有没有被附加到jvm环境中
int getEnvStat = g_VMDeviceScan->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED)
{
// 如果没有主动附加到jvm环境中获取到env
if (g_VMDeviceScan->AttachCurrentThread((void **)&env, NULL) != 0)
{
return;
}
needDetach = HGTRUE;
}
//通过全局变量g_obj获取到要回调的类
jclass cls = env->GetObjectClass(g_objDeviceScan);
if (NULL != cls)
{
jmethodID jmethodId = env->GetMethodID(cls, "onDeviceScanImage", "(JJ)V");
if (NULL != jmethodId)
{
env->CallVoidMethod(g_objDeviceScan, jmethodId, (jlong)device, (jlong)image);
}
}
if (needDetach)
{
g_VMDeviceScan->DetachCurrentThread();
}
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_StartDeviceScan(
JNIEnv* env, jobject thiz, jlong dev)
{
printf("\nJava_com_easing_java_HGScannerLib_StartDeviceScan\n");
if (NULL != g_VMDeviceScan)
{
printf("Already Start Device Scan\n");
return (jboolean)HGFALSE;
}
env->GetJavaVM(&g_VMDeviceScan);
g_objDeviceScan = env->NewGlobalRef(thiz);
HGBool ret = HGLib_StartDeviceScan((HGLibDevice)dev, DeviceScanEventFunc, NULL, DeviceScanImageFunc, NULL);
printf("ret=%d\n", ret);
if (!ret)
{
env->DeleteGlobalRef(g_objDeviceScan);
g_objDeviceScan = NULL;
g_VMDeviceScan = NULL;
}
printf("\n");
return (jboolean)ret;
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_easing_java_HGScannerLib_StopDeviceScan(
JNIEnv* env, jclass clazz, jlong dev)
{
(void)clazz;
printf("\nJava_com_easing_java_HGScannerLib_StopDeviceScan\n");
if (NULL == g_VMDeviceScan)
{
printf("Do not Start Device Scan yet\n");
return (jboolean)HGFALSE;
}
HGBool ret = HGLib_StopDeviceScan((HGLibDevice)dev);
printf("ret=%d\n", ret);
if (ret)
{
if (NULL != g_VMDeviceScan)
{
env->DeleteGlobalRef(g_objDeviceScan);
g_objDeviceScan = NULL;
g_VMDeviceScan = NULL;
}
}
printf("\n");
return (jboolean)ret;
}

View File

@ -36,6 +36,12 @@ public class HGScannerLib {
public native boolean InitDevice(); public native boolean InitDevice();
public native boolean DeinitDevice(); public native boolean DeinitDevice();
public native String[] GetDeviceNameList(); public native String[] GetDeviceNameList();
public native long OpenDevice(String deviceName);
public native boolean CloseDevice(long dev);
public native String GetDeviceSN(long dev);
public native String GetDeviceFWVersion(long dev);
public native boolean StartDeviceScan(long dev);
public native boolean StopDeviceScan(long dev);
public void onDeviceHotPlugEvent(int event, String deviceName) { public void onDeviceHotPlugEvent(int event, String deviceName) {
System.out.println("onDeviceHotPlugEvent"); System.out.println("onDeviceHotPlugEvent");
@ -43,6 +49,18 @@ public class HGScannerLib {
System.out.println("deviceName=" + deviceName); System.out.println("deviceName=" + deviceName);
} }
public void onDeviceScanEvent(long dev, int event, boolean err, String info) {
System.out.println("onDeviceScanEvent");
System.out.println("event=" + event);
System.out.println("err=" + err);
System.out.println("info=" + info);
}
public void onDeviceScanImage(long dev, long img) {
System.out.println("onDeviceScanImage");
System.out.println("img=" + img);
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.out.println("hello world"); System.out.println("hello world");
@ -84,6 +102,21 @@ public class HGScannerLib {
System.out.println(" " + i + ":" + devNameList[i]); System.out.println(" " + i + ":" + devNameList[i]);
} }
if (devNameList.length > 0)
{
long dev = inst.OpenDevice(devNameList[0]);
String sn = inst.GetDeviceSN(dev);
System.out.println("sn=" + sn);
String fwVersion = inst.GetDeviceFWVersion(dev);
System.out.println("fwVersion=" + fwVersion);
inst.StartDeviceScan(dev);
inst.StopDeviceScan(dev);
inst.CloseDevice(dev);
}
inst.DeinitDevice(); inst.DeinitDevice();
} }
} }