2022-08-26 07:52:06 +00:00
|
|
|
import ctypes
|
2022-09-16 07:28:23 +00:00
|
|
|
import os
|
|
|
|
from os import system
|
2022-08-26 07:52:06 +00:00
|
|
|
import time
|
2022-09-16 07:28:23 +00:00
|
|
|
import platform
|
2022-08-26 07:52:06 +00:00
|
|
|
from ctypes import *
|
2022-09-16 10:54:44 +00:00
|
|
|
import random
|
2022-08-26 07:52:06 +00:00
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibSaveImageParam(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-16 08:56:42 +00:00
|
|
|
_fields_ = [ ("size", c_uint),
|
|
|
|
("jpegQuality", c_uint),
|
|
|
|
("tiffCompression", c_uint),
|
|
|
|
("tiffJpegQuality", c_uint),
|
2022-08-26 07:52:06 +00:00
|
|
|
("ocr", c_int)]
|
2022-09-16 07:28:23 +00:00
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceIntValueList(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("value", POINTER(c_int)),
|
|
|
|
("count", c_uint)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceEnumValueList(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("value", POINTER(c_uint)),
|
|
|
|
("count", c_uint)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceDoubleValueList(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("value", POINTER(c_double)),
|
|
|
|
("count", c_uint)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceIntValueRange(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("minValue", c_int),
|
|
|
|
("maxValue", c_int)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceDoubleValueRange(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("minValue", c_double),
|
|
|
|
("maxValue", c_double)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceParamType(Union):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("intValue", c_int),
|
|
|
|
("enumValue", c_uint),
|
|
|
|
("doubleValue", c_double),
|
|
|
|
("boolValue", c_int)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceParamRangeType(Union):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("intValueList", HGLibDeviceIntValueList),
|
|
|
|
("enumValueList", HGLibDeviceEnumValueList),
|
|
|
|
("doubleValueList", HGLibDeviceDoubleValueList),
|
|
|
|
("intValueRange", HGLibDeviceIntValueRange),
|
|
|
|
("doubleValueRange", HGLibDeviceDoubleValueRange)]
|
|
|
|
|
|
|
|
class HGLibDeviceParam(Structure):
|
2022-10-12 03:35:09 +00:00
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("option", c_uint),
|
|
|
|
("type", c_uint),
|
|
|
|
("typeValue", HGLibDeviceParamType),
|
|
|
|
("rangeType", c_uint),
|
|
|
|
("rangeTypeValue", HGLibDeviceParamRangeType)]
|
|
|
|
|
2022-10-12 03:35:09 +00:00
|
|
|
class HGLibDeviceParamGroup(Structure):
|
|
|
|
_pack_ = 4
|
2022-09-20 05:49:45 +00:00
|
|
|
_fields_ = [ ("group", c_uint),
|
|
|
|
("param", POINTER(HGLibDeviceParam)),
|
|
|
|
("paramCount", c_uint)]
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
print(os.path.abspath('.'))
|
2022-08-26 07:52:06 +00:00
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#加载动态库 所有依赖库必须在同一运行目录下边
|
|
|
|
if platform.system() == 'Windows':
|
2022-10-17 01:37:42 +00:00
|
|
|
Objdll = ctypes.WinDLL(os.path.abspath('.') + "/HWScannerLib.dll")
|
2022-09-16 07:28:23 +00:00
|
|
|
elif platform.system() == "Linux":
|
2022-10-17 01:37:42 +00:00
|
|
|
Objdll = cdll.LoadLibrary(os.path.abspath('.') + "/libHWScannerLib.so")
|
2022-08-26 07:52:06 +00:00
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#加载图像接口示例
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_LoadImage = Objdll.HGLib_LoadImage
|
|
|
|
HGLib_LoadImage.argtypes = [ctypes.c_char_p]
|
|
|
|
HGLib_LoadImage.restype = ctypes.c_void_p
|
2022-09-16 08:21:21 +00:00
|
|
|
Image = HGLib_LoadImage(c_char_p(b"1.jpg"))
|
2022-08-26 07:52:06 +00:00
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#保存图像接口示例
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_SaveImage = Objdll.HGLib_SaveImage
|
|
|
|
HGLib_SaveImage.argtypes = [ctypes.c_void_p, ctypes.c_char_p, POINTER(HGLibSaveImageParam)]
|
|
|
|
HGLib_SaveImage.restype = ctypes.c_int
|
|
|
|
ImageParam = HGLibSaveImageParam()
|
2022-10-12 05:37:03 +00:00
|
|
|
ImageParam.size = sizeof(ImageParam)
|
2022-10-12 03:35:09 +00:00
|
|
|
ImageParam.jpegQuality = 80
|
|
|
|
ImageParam.tiffCompression = 4
|
|
|
|
ImageParam.tiffJpegQuality = 80
|
2022-08-26 07:52:06 +00:00
|
|
|
ImageParam.ocr = 0
|
2022-09-16 08:21:21 +00:00
|
|
|
Ret = HGLib_SaveImage(Image, c_char_p(b"2.jpg"), pointer(ImageParam))
|
2022-08-26 07:52:06 +00:00
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#释放图像资源
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_ReleaseImage = Objdll.HGLib_ReleaseImage
|
|
|
|
HGLib_ReleaseImage.argtypes = [ctypes.c_void_p]
|
|
|
|
HGLib_ReleaseImage.restype = ctypes.c_int
|
|
|
|
Ret = HGLib_ReleaseImage(Image)
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#设备热拔插回调事件
|
2022-09-16 08:56:42 +00:00
|
|
|
def HGLibDeviceHotPlugEventFunc(event: c_uint, deviceName: c_char_p, param: c_void_p):
|
2022-08-26 07:52:06 +00:00
|
|
|
print(deviceName)
|
|
|
|
return
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#初始化操作
|
2022-09-16 08:56:42 +00:00
|
|
|
FuncType = CFUNCTYPE(None, c_uint, c_char_p, c_void_p)
|
2022-08-26 07:52:06 +00:00
|
|
|
cb = FuncType(HGLibDeviceHotPlugEventFunc)
|
|
|
|
HGLib_InitDevice = Objdll.HGLib_InitDevice
|
|
|
|
HGLib_InitDevice.argtypes = [FuncType, c_void_p]
|
|
|
|
HGLib_InitDevice.restype = ctypes.c_int
|
|
|
|
Ret = HGLib_InitDevice(cb, 0)
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#获取设备列表
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_GetDeviceNameList = Objdll.HGLib_GetDeviceNameList
|
|
|
|
HGLib_GetDeviceNameList.argtypes = []
|
|
|
|
HGLib_GetDeviceNameList.restype = POINTER(ctypes.c_char_p)
|
|
|
|
DeviceNameList = HGLib_GetDeviceNameList()
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#打开指定设备
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_OpenDevice = Objdll.HGLib_OpenDevice
|
|
|
|
HGLib_OpenDevice.argtypes = [ctypes.c_char_p]
|
|
|
|
HGLib_OpenDevice.restype = ctypes.c_void_p
|
|
|
|
Device = HGLib_OpenDevice(DeviceNameList[0])
|
|
|
|
|
2022-09-20 05:49:45 +00:00
|
|
|
#获取设备所有参数
|
|
|
|
HGLib_GetDeviceParamGroupList = Objdll.HGLib_GetDeviceParamGroupList
|
|
|
|
HGLib_GetDeviceParamGroupList.argtypes = [ctypes.c_void_p, POINTER(c_uint)]
|
|
|
|
HGLib_GetDeviceParamGroupList.restype = POINTER(HGLibDeviceParamGroup)
|
|
|
|
DevParamCount = ctypes.c_uint()
|
|
|
|
DevParamGroup = HGLib_GetDeviceParamGroupList(Device, pointer(DevParamCount))
|
|
|
|
|
|
|
|
GroupList = ['None', 'BaseSetting', 'Brightness', 'ImageProc', 'PaperFeeding']
|
|
|
|
ValueTypeList = ['None', 'Int', 'Enum', 'Double', 'Bool']
|
|
|
|
ValueRangeTypeList = ['None', 'IntList', 'EnumList', 'DoubleList', 'IntRange', 'DoubleRange']
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while i < int(DevParamCount.value):
|
|
|
|
print("group" + str(i) + ":" + GroupList[DevParamGroup[i].group])
|
|
|
|
print(" paramCount:" + str(DevParamGroup[i].paramCount))
|
|
|
|
j = 0
|
|
|
|
while (j < int(DevParamGroup[i].paramCount)):
|
|
|
|
print(" paramOption:" + str(DevParamGroup[i].param[j].option))
|
|
|
|
print(" paramValueType:" + ValueTypeList[DevParamGroup[i].param[j].type])
|
|
|
|
print(" paramValueRangeType:" + ValueRangeTypeList[DevParamGroup[i].param[j].rangeType])
|
|
|
|
j += 1
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
#销毁
|
|
|
|
HGLib_ReleaseDeviceParamGroupList = Objdll.HGLib_ReleaseDeviceParamGroupList
|
|
|
|
HGLib_ReleaseDeviceParamGroupList.argtypes = [POINTER(HGLibDeviceParamGroup), c_uint]
|
|
|
|
HGLib_ReleaseDeviceParamGroupList.restype = ctypes.c_int
|
|
|
|
Ret = HGLib_ReleaseDeviceParamGroupList(DevParamGroup, DevParamCount)
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#设置扫描参数
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_SetDeviceParam = Objdll.HGLib_SetDeviceParam
|
2022-09-16 08:56:42 +00:00
|
|
|
HGLib_SetDeviceParam.argtypes = [ctypes.c_void_p, ctypes.c_uint, ctypes.c_void_p]
|
2022-08-26 07:52:06 +00:00
|
|
|
HGLib_SetDeviceParam.restype = ctypes.c_int
|
2022-09-16 07:28:23 +00:00
|
|
|
DevParam = ctypes.c_int(1) #HGLIB_OPTION_ENUMVALUE_DLSCLX_W -> 不进行多流输出类型
|
|
|
|
Ret = HGLib_SetDeviceParam(Device, 1, byref(DevParam)) #见HGLib_SetDeviceParam 头文件接口说明 1 -> HGLIB_OPTION_NAME_DLSC 多流输出
|
2022-08-26 07:52:06 +00:00
|
|
|
|
2022-09-16 10:54:44 +00:00
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#扫描事件回调
|
2022-09-16 08:56:42 +00:00
|
|
|
def HGLibDeviceScanEventFunc(device: c_void_p, event: c_uint, err: c_int, info: c_char_p, param: c_void_p):
|
2022-09-16 10:54:44 +00:00
|
|
|
s_info=info
|
|
|
|
global bStop
|
|
|
|
print("event code",event," event info:",s_info)
|
2022-09-16 07:28:23 +00:00
|
|
|
if event == 2:#HGLIB_DEVSCAN_EVENT_END 扫描停止
|
|
|
|
bStop=True
|
2022-09-16 10:54:44 +00:00
|
|
|
print("bStop true")
|
2022-09-16 07:28:23 +00:00
|
|
|
elif event == 1:
|
|
|
|
bStop=False
|
2022-09-16 10:54:44 +00:00
|
|
|
print("bStop false")
|
2022-08-26 07:52:06 +00:00
|
|
|
return
|
2022-09-16 10:54:44 +00:00
|
|
|
imgindex=0
|
2022-09-16 07:28:23 +00:00
|
|
|
#扫描图像事件回调
|
2022-08-26 07:52:06 +00:00
|
|
|
def HGLibDeviceScanImageFunc(device: c_void_p, image: c_void_p, param: c_void_p):
|
2022-09-16 10:54:44 +00:00
|
|
|
global imgindex
|
2022-08-26 07:52:06 +00:00
|
|
|
ImageParam = HGLibSaveImageParam()
|
2022-10-12 05:37:03 +00:00
|
|
|
ImageParam.size = sizeof(ImageParam)
|
|
|
|
ImageParam.jpegQuality = 80
|
|
|
|
ImageParam.tiffCompression = 4
|
|
|
|
ImageParam.tiffJpegQuality = 80
|
2022-08-26 07:52:06 +00:00
|
|
|
ImageParam.ocr = 0
|
2022-09-16 10:54:44 +00:00
|
|
|
print("image call back!!")
|
|
|
|
imgindex+=1
|
|
|
|
t_index=imgindex
|
|
|
|
imgname=str(t_index) + '_scanned.jpg'
|
|
|
|
print(imgname)
|
|
|
|
b_imagename=imgname.encode('utf-8')
|
|
|
|
pchar=c_char_p(b_imagename)
|
|
|
|
Ret = HGLib_SaveImage(image, pchar, pointer(ImageParam))
|
2022-08-26 07:52:06 +00:00
|
|
|
return
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#注册扫描相关事件并启动扫描
|
2022-09-16 08:56:42 +00:00
|
|
|
FuncType1 = CFUNCTYPE(None, c_void_p, c_uint, c_int, c_char_p, c_void_p)
|
2022-08-26 07:52:06 +00:00
|
|
|
cb1 = FuncType1(HGLibDeviceScanEventFunc)
|
|
|
|
FuncType2 = CFUNCTYPE(None, c_void_p, c_void_p, c_void_p)
|
|
|
|
cb2 = FuncType2(HGLibDeviceScanImageFunc)
|
|
|
|
HGLib_StartDeviceScan = Objdll.HGLib_StartDeviceScan
|
|
|
|
HGLib_StartDeviceScan.argtypes = [c_void_p, FuncType1, c_void_p, FuncType2, c_void_p]
|
|
|
|
HGLib_StartDeviceScan.restyped = ctypes.c_int
|
|
|
|
print("start scan")
|
|
|
|
Ret = HGLib_StartDeviceScan(Device, cb1, 0, cb2, 0)
|
|
|
|
|
2022-09-16 07:28:23 +00:00
|
|
|
#模拟扫描持续应等待扫描事件回调返回扫描停止才结束本次扫描流程
|
2022-09-16 10:54:44 +00:00
|
|
|
while(not bStop):
|
2022-09-16 07:28:23 +00:00
|
|
|
time.sleep(1)
|
2022-09-16 10:54:44 +00:00
|
|
|
print("scanning...")
|
|
|
|
|
|
|
|
|
|
|
|
print("scan done!!!")
|
2022-09-16 07:28:23 +00:00
|
|
|
|
|
|
|
#关闭当前打开的设备
|
|
|
|
HGLib_CloseDevice=Objdll.HGLib_CloseDevice
|
2022-09-16 09:14:04 +00:00
|
|
|
HGLib_CloseDevice.argtypes = [c_void_p]
|
2022-09-16 07:28:23 +00:00
|
|
|
HGLib_CloseDevice.restype = ctypes.c_int
|
|
|
|
HGLib_CloseDevice(Device)
|
|
|
|
print("Close Devices")
|
|
|
|
|
|
|
|
#释放设备列表资源
|
|
|
|
HGLib_ReleaseDeviceNameList=Objdll.HGLib_ReleaseDeviceNameList
|
2022-09-16 09:14:04 +00:00
|
|
|
HGLib_ReleaseDeviceNameList.argtypes=[POINTER(ctypes.c_char_p)]
|
2022-09-16 07:28:23 +00:00
|
|
|
HGLib_ReleaseDeviceNameList.restype = ctypes.c_int
|
|
|
|
HGLib_ReleaseDeviceNameList(DeviceNameList)
|
|
|
|
print("ReleaseDeviceNameList done")
|
|
|
|
|
|
|
|
print("exit test")
|
|
|
|
|
|
|
|
|