135 lines
4.5 KiB
Python
135 lines
4.5 KiB
Python
import ctypes
|
|
import os
|
|
from os import system
|
|
import time
|
|
import platform
|
|
from ctypes import *
|
|
|
|
class HGLibSaveImageParam(Structure):
|
|
_fields_ = [ ("size", c_ulong),
|
|
("jpegQuality", c_ulong),
|
|
("tiffCompression", c_ulong),
|
|
("tiffJpegQuality", c_ulong),
|
|
("ocr", c_int)]
|
|
|
|
print(os.path.abspath('.'))
|
|
|
|
#加载动态库 所有依赖库必须在同一运行目录下边
|
|
if platform.system() == 'Windows':
|
|
Objdll = ctypes.WinDLL(os.path.abspath('.') + "/HGScannerLib.dll")
|
|
elif platform.system() == "Linux":
|
|
Objdll = cdll.LoadLibrary(os.path.abspath('.') + "/libHGScannerLib.so")
|
|
|
|
#加载图像接口示例
|
|
HGLib_LoadImage = Objdll.HGLib_LoadImage
|
|
HGLib_LoadImage.argtypes = [ctypes.c_char_p]
|
|
HGLib_LoadImage.restype = ctypes.c_void_p
|
|
Image = HGLib_LoadImage(c_char_p(b"d:/1.jpg"))
|
|
|
|
#保存图像接口示例
|
|
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()
|
|
ImageParam.size = 20
|
|
ImageParam.jpegQuality = 100
|
|
ImageParam.tiffCompression = 0
|
|
ImageParam.tiffJpegQuality = 0
|
|
ImageParam.ocr = 0
|
|
Ret = HGLib_SaveImage(Image, c_char_p(b"d:/2.jpg"), pointer(ImageParam))
|
|
|
|
#释放图像资源
|
|
HGLib_ReleaseImage = Objdll.HGLib_ReleaseImage
|
|
HGLib_ReleaseImage.argtypes = [ctypes.c_void_p]
|
|
HGLib_ReleaseImage.restype = ctypes.c_int
|
|
Ret = HGLib_ReleaseImage(Image)
|
|
|
|
#设备热拔插回调事件
|
|
def HGLibDeviceHotPlugEventFunc(event: c_ulong, deviceName: c_char_p, param: c_void_p):
|
|
print(deviceName)
|
|
return
|
|
|
|
#初始化操作
|
|
FuncType = CFUNCTYPE(None, c_ulong, c_char_p, c_void_p)
|
|
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)
|
|
|
|
#获取设备列表
|
|
HGLib_GetDeviceNameList = Objdll.HGLib_GetDeviceNameList
|
|
HGLib_GetDeviceNameList.argtypes = []
|
|
HGLib_GetDeviceNameList.restype = POINTER(ctypes.c_char_p)
|
|
DeviceNameList = HGLib_GetDeviceNameList()
|
|
|
|
#打开指定设备
|
|
HGLib_OpenDevice = Objdll.HGLib_OpenDevice
|
|
HGLib_OpenDevice.argtypes = [ctypes.c_char_p]
|
|
HGLib_OpenDevice.restype = ctypes.c_void_p
|
|
Device = HGLib_OpenDevice(DeviceNameList[0])
|
|
|
|
#设置扫描参数
|
|
HGLib_SetDeviceParam = Objdll.HGLib_SetDeviceParam
|
|
HGLib_SetDeviceParam.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_void_p]
|
|
HGLib_SetDeviceParam.restype = ctypes.c_int
|
|
DevParam = ctypes.c_int(1) #HGLIB_OPTION_ENUMVALUE_DLSCLX_W -> 不进行多流输出类型
|
|
Ret = HGLib_SetDeviceParam(Device, 1, byref(DevParam)) #见HGLib_SetDeviceParam 头文件接口说明 1 -> HGLIB_OPTION_NAME_DLSC 多流输出
|
|
|
|
bStop=False
|
|
#扫描事件回调
|
|
def HGLibDeviceScanEventFunc(device: c_void_p, event: c_ulong, err: c_int, info: c_char_p, param: c_void_p):
|
|
print(info)
|
|
if event == 2:#HGLIB_DEVSCAN_EVENT_END 扫描停止
|
|
bStop=True
|
|
elif event == 1:
|
|
bStop=False
|
|
return
|
|
|
|
#扫描图像事件回调
|
|
def HGLibDeviceScanImageFunc(device: c_void_p, image: c_void_p, param: c_void_p):
|
|
ImageParam = HGLibSaveImageParam()
|
|
ImageParam.size = 20
|
|
ImageParam.jpegQuality = 100
|
|
ImageParam.tiffCompression = 0
|
|
ImageParam.tiffJpegQuality = 0
|
|
ImageParam.ocr = 0
|
|
Ret = HGLib_SaveImage(image, c_char_p(b"d:/3.jpg"), pointer(ImageParam))
|
|
return
|
|
|
|
#注册扫描相关事件并启动扫描
|
|
FuncType1 = CFUNCTYPE(None, c_void_p, c_ulong, c_int, c_char_p, c_void_p)
|
|
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)
|
|
|
|
#模拟扫描持续应等待扫描事件回调返回扫描停止才结束本次扫描流程
|
|
while(bStop != True):
|
|
time.sleep(1)
|
|
break;
|
|
|
|
#关闭当前打开的设备
|
|
HGLib_CloseDevice=Objdll.HGLib_CloseDevice
|
|
HGLib_CloseDevice.argtypes = c_void_p
|
|
HGLib_CloseDevice.restype = ctypes.c_int
|
|
HGLib_CloseDevice(Device)
|
|
print("Close Devices")
|
|
|
|
#释放设备列表资源
|
|
HGLib_ReleaseDeviceNameList=Objdll.HGLib_ReleaseDeviceNameList
|
|
HGLib_ReleaseDeviceNameList.argtypes=POINTER(ctypes.c_char_p)
|
|
HGLib_ReleaseDeviceNameList.restype = ctypes.c_int
|
|
HGLib_ReleaseDeviceNameList(DeviceNameList)
|
|
print("ReleaseDeviceNameList done")
|
|
|
|
print("exit test")
|
|
|
|
|