scannerlib增加获取ocr字符串功能

This commit is contained in:
luoliangyi 2023-11-07 16:52:21 +08:00
parent 2f87ca5106
commit 74a9aefc42
6 changed files with 87 additions and 4 deletions

View File

@ -9,6 +9,8 @@ HGLib_GetImageInfo
HGLib_GetImageDpi HGLib_GetImageDpi
HGLib_SaveImage HGLib_SaveImage
HGLib_ReleaseImage HGLib_ReleaseImage
HGLib_GetOcrText
HGLib_ReleaseOcrText
HGLib_InitDevice HGLib_InitDevice
HGLib_DeinitDevice HGLib_DeinitDevice
HGLib_GetDeviceNameList HGLib_GetDeviceNameList

View File

@ -151,6 +151,66 @@ HGBool HGAPI HGLib_ReleaseImage(HGLibImage image)
return HGTRUE; return HGTRUE;
} }
HGChar* HGAPI HGLib_GetOcrText(HGLibImage image)
{
if (NULL == image)
{
return NULL;
}
std::string strText;
HGOCRMgr ocrMgr = NULL;
HGImgProc_CreateOCRMgr(HGIMGPROC_OCRALGO_HANVON, &ocrMgr);
if (NULL != ocrMgr)
{
HGOCRRet ocrRet = NULL;
HGImgProc_ImageOCR(ocrMgr, (HGImage)image, &ocrRet);
if (NULL != ocrRet)
{
HGUInt blockCount = 0;
HGImgProc_GetOCRRetBlockCount(ocrRet, &blockCount);
for (HGUInt i = 0; i < blockCount; ++i)
{
const HGChar* text = NULL;
HGImgProc_GetOCRRetBlockText(ocrRet, i, &text);
strText += StdStringToUtf8(text);
if (i != blockCount - 1)
strText += "\n";
}
HGImgProc_DestroyOCRRet(ocrRet);
}
HGImgProc_DestroyOCRMgr(ocrMgr);
}
if (strText.empty())
{
return NULL;
}
HGChar* ocrText = (HGChar*)malloc(strText.size() + 1);
if (NULL == ocrText)
{
return NULL;
}
strcpy(ocrText, strText.c_str());
return ocrText;
}
HGBool HGAPI HGLib_ReleaseOcrText(HGChar* ocrText)
{
if (NULL == ocrText)
{
return HGFALSE;
}
free(ocrText);
return HGTRUE;
}
HGBool HGAPI HGLib_InitDevice(HGLibDeviceHotPlugEventFunc func, HGPointer param) HGBool HGAPI HGLib_InitDevice(HGLibDeviceHotPlugEventFunc func, HGPointer param)
{ {
return HGLibDeviceImpl::Init(func, param); return HGLibDeviceImpl::Init(func, param);

View File

@ -542,6 +542,12 @@ HGEXPORT HGBool HGAPI HGLib_SaveImage(HGLibImage image, const HGChar* savePath,
/* 释放图像 */ /* 释放图像 */
HGEXPORT HGBool HGAPI HGLib_ReleaseImage(HGLibImage image); HGEXPORT HGBool HGAPI HGLib_ReleaseImage(HGLibImage image);
/* 获取OCR文本 */
HGEXPORT HGChar* HGAPI HGLib_GetOcrText(HGLibImage image);
/* 销毁OCR文本 */
HGEXPORT HGBool HGAPI HGLib_ReleaseOcrText(HGChar* ocrText);
/* 初始化扫描仪 */ /* 初始化扫描仪 */
HGEXPORT HGBool HGAPI HGLib_InitDevice(HGLibDeviceHotPlugEventFunc func, HGPointer param); HGEXPORT HGBool HGAPI HGLib_InitDevice(HGLibDeviceHotPlugEventFunc func, HGPointer param);

View File

@ -94,6 +94,19 @@ ImageParam.tiffJpegQuality = 80
ImageParam.ocr = 0 ImageParam.ocr = 0
Ret = HGLib_SaveImage(Image, c_char_p(b"2.jpg"), pointer(ImageParam)) Ret = HGLib_SaveImage(Image, c_char_p(b"2.jpg"), pointer(ImageParam))
#OCR
HGLib_GetOcrText = Objdll.HGLib_GetOcrText
HGLib_GetOcrText.argtypes = [ctypes.c_void_p]
HGLib_GetOcrText.restype = ctypes.c_void_p
OcrText = HGLib_GetOcrText(Image)
if OcrText:
print(ctypes.string_at(OcrText).decode('utf-8'));
HGLib_ReleaseOcrText = Objdll.HGLib_ReleaseOcrText
HGLib_ReleaseOcrText.argtypes = [ctypes.c_void_p]
HGLib_ReleaseOcrText.restype = ctypes.c_int
Ret = HGLib_ReleaseOcrText(OcrText)
#释放图像资源 #释放图像资源
HGLib_ReleaseImage = Objdll.HGLib_ReleaseImage HGLib_ReleaseImage = Objdll.HGLib_ReleaseImage
HGLib_ReleaseImage.argtypes = [ctypes.c_void_p] HGLib_ReleaseImage.argtypes = [ctypes.c_void_p]
@ -102,7 +115,7 @@ Ret = HGLib_ReleaseImage(Image)
#设备热拔插回调事件 #设备热拔插回调事件
def HGLibDeviceHotPlugEventFunc(event: c_uint, deviceName: c_char_p, param: c_void_p): def HGLibDeviceHotPlugEventFunc(event: c_uint, deviceName: c_char_p, param: c_void_p):
print(deviceName) print(deviceName.decode('utf-8'))
return return
#初始化操作 #初始化操作
@ -196,12 +209,14 @@ Ret = HGLib_SetDeviceParam(Device, 51, byref(DevParam))
DevParam = ctypes.c_double(0.1) DevParam = ctypes.c_double(0.1)
Ret = HGLib_SetDeviceParam(Device, 52, byref(DevParam)) Ret = HGLib_SetDeviceParam(Device, 52, byref(DevParam))
bStop = False
#扫描事件回调 #扫描事件回调
def HGLibDeviceScanEventFunc(device: c_void_p, event: c_uint, err: c_int, info: c_char_p, param: c_void_p): def HGLibDeviceScanEventFunc(device: c_void_p, event: c_uint, err: c_int, info: ctypes.c_char_p, param: c_void_p):
s_info=info
global bStop global bStop
print("event code",event," event info:",s_info) print("event code:", event)
if info:
print("event info:", info.decode('utf-8'))
if event == 2:#HGLIB_DEVSCAN_EVENT_END 扫描停止 if event == 2:#HGLIB_DEVSCAN_EVENT_END 扫描停止
bStop=True bStop=True
print("bStop true") print("bStop true")