完善python demo

This commit is contained in:
luoliangyi 2022-09-16 15:28:23 +08:00
parent 05dc3aadbd
commit 48706a5d20
1 changed files with 51 additions and 6 deletions

View File

@ -1,5 +1,8 @@
import ctypes
import os
from os import system
import time
import platform
from ctypes import *
class HGLibSaveImageParam(Structure):
@ -8,14 +11,22 @@ class HGLibSaveImageParam(Structure):
("tiffCompression", c_ulong),
("tiffJpegQuality", c_ulong),
("ocr", c_int)]
print(os.path.abspath('.'))
Objdll = ctypes.WinDLL("D:/sane/code_app/test/scannerlib/HWScannerLib.dll")
#加载动态库 所有依赖库必须在同一运行目录下边
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
@ -27,16 +38,18 @@ 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
@ -46,26 +59,36 @@ 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)
Ret = HGLib_SetDeviceParam(Device, 1, byref(DevParam))
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
@ -76,6 +99,7 @@ def HGLibDeviceScanImageFunc(device: c_void_p, image: c_void_p, param: c_void_p)
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)
@ -86,4 +110,25 @@ HGLib_StartDeviceScan.restyped = ctypes.c_int
print("start scan")
Ret = HGLib_StartDeviceScan(Device, cb1, 0, cb2, 0)
time.sleep(10)
#模拟扫描持续应等待扫描事件回调返回扫描停止才结束本次扫描流程
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")