code_app/test/scannerlib/test.py

135 lines
4.5 KiB
Python
Raw Normal View History

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 *
class HGLibSaveImageParam(Structure):
_fields_ = [ ("size", c_ulong),
("jpegQuality", c_ulong),
("tiffCompression", c_ulong),
("tiffJpegQuality", c_ulong),
("ocr", c_int)]
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':
Objdll = ctypes.WinDLL(os.path.abspath('.') + "/HGScannerLib.dll")
elif platform.system() == "Linux":
Objdll = cdll.LoadLibrary(os.path.abspath('.') + "/libHGScannerLib.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
Image = HGLib_LoadImage(c_char_p(b"d:/1.jpg"))
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()
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))
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-08-26 07:52:06 +00:00
def HGLibDeviceHotPlugEventFunc(event: c_ulong, deviceName: c_char_p, param: c_void_p):
print(deviceName)
return
2022-09-16 07:28:23 +00:00
#初始化操作
2022-08-26 07:52:06 +00:00
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)
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-16 07:28:23 +00:00
#设置扫描参数
2022-08-26 07:52:06 +00:00
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
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 07:28:23 +00:00
bStop=False
#扫描事件回调
2022-08-26 07:52:06 +00:00
def HGLibDeviceScanEventFunc(device: c_void_p, event: c_ulong, err: c_int, info: c_char_p, param: c_void_p):
print(info)
2022-09-16 07:28:23 +00:00
if event == 2:#HGLIB_DEVSCAN_EVENT_END 扫描停止
bStop=True
elif event == 1:
bStop=False
2022-08-26 07:52:06 +00:00
return
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):
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
2022-09-16 07:28:23 +00:00
#注册扫描相关事件并启动扫描
2022-08-26 07:52:06 +00:00
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)
2022-09-16 07:28:23 +00:00
#模拟扫描持续应等待扫描事件回调返回扫描停止才结束本次扫描流程
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")