code_app/test/scannerlib/cumtenn/test.py

235 lines
8.2 KiB
Python

import ctypes
import os
from os import system
import time
import platform
from ctypes import *
import random
class HGLibSaveImageParam(Structure):
_pack_ = 4
_fields_ = [ ("size", c_uint),
("jpegQuality", c_uint),
("tiffCompression", c_uint),
("tiffJpegQuality", c_uint),
("ocr", c_int)]
class HGLibDeviceIntValueList(Structure):
_pack_ = 4
_fields_ = [ ("value", POINTER(c_int)),
("count", c_uint)]
class HGLibDeviceEnumValueList(Structure):
_pack_ = 4
_fields_ = [ ("value", POINTER(c_uint)),
("count", c_uint)]
class HGLibDeviceDoubleValueList(Structure):
_pack_ = 4
_fields_ = [ ("value", POINTER(c_double)),
("count", c_uint)]
class HGLibDeviceIntValueRange(Structure):
_pack_ = 4
_fields_ = [ ("minValue", c_int),
("maxValue", c_int)]
class HGLibDeviceDoubleValueRange(Structure):
_pack_ = 4
_fields_ = [ ("minValue", c_double),
("maxValue", c_double)]
class HGLibDeviceParamType(Union):
_pack_ = 4
_fields_ = [ ("intValue", c_int),
("enumValue", c_uint),
("doubleValue", c_double),
("boolValue", c_int)]
class HGLibDeviceParamRangeType(Union):
_pack_ = 4
_fields_ = [ ("intValueList", HGLibDeviceIntValueList),
("enumValueList", HGLibDeviceEnumValueList),
("doubleValueList", HGLibDeviceDoubleValueList),
("intValueRange", HGLibDeviceIntValueRange),
("doubleValueRange", HGLibDeviceDoubleValueRange)]
class HGLibDeviceParam(Structure):
_pack_ = 4
_fields_ = [ ("option", c_uint),
("type", c_uint),
("typeValue", HGLibDeviceParamType),
("rangeType", c_uint),
("rangeTypeValue", HGLibDeviceParamRangeType)]
class HGLibDeviceParamGroup(Structure):
_pack_ = 4
_fields_ = [ ("group", c_uint),
("param", POINTER(HGLibDeviceParam)),
("paramCount", c_uint)]
print(os.path.abspath('.'))
#加载动态库 所有依赖库必须在同一运行目录下边
if platform.system() == 'Windows':
Objdll = ctypes.WinDLL(os.path.abspath('.') + "/CTSScannerLib.dll")
elif platform.system() == "Linux":
Objdll = cdll.LoadLibrary(os.path.abspath('.') + "/libCTSScannerLib.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"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 = sizeof(ImageParam)
ImageParam.jpegQuality = 80
ImageParam.tiffCompression = 4
ImageParam.tiffJpegQuality = 80
ImageParam.ocr = 0
Ret = HGLib_SaveImage(Image, c_char_p(b"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_uint, deviceName: c_char_p, param: c_void_p):
print(deviceName)
return
#初始化操作
FuncType = CFUNCTYPE(None, c_uint, 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_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)
#设置扫描参数
HGLib_SetDeviceParam = Objdll.HGLib_SetDeviceParam
HGLib_SetDeviceParam.argtypes = [ctypes.c_void_p, ctypes.c_uint, 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 多流输出
#扫描事件回调
def HGLibDeviceScanEventFunc(device: c_void_p, event: c_uint, err: c_int, info: c_char_p, param: c_void_p):
s_info=info
global bStop
print("event code",event," event info:",s_info)
if event == 2:#HGLIB_DEVSCAN_EVENT_END 扫描停止
bStop=True
print("bStop true")
elif event == 1:
bStop=False
print("bStop false")
return
imgindex=0
#扫描图像事件回调
def HGLibDeviceScanImageFunc(device: c_void_p, image: c_void_p, param: c_void_p):
global imgindex
ImageParam = HGLibSaveImageParam()
ImageParam.size = sizeof(ImageParam)
ImageParam.jpegQuality = 80
ImageParam.tiffCompression = 4
ImageParam.tiffJpegQuality = 80
ImageParam.ocr = 0
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))
return
#注册扫描相关事件并启动扫描
FuncType1 = CFUNCTYPE(None, c_void_p, c_uint, 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(not bStop):
time.sleep(1)
print("scanning...")
print("scan done!!!")
#关闭当前打开的设备
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")