code_app/sdk/scannerlib/HGScannerLib.cpp

470 lines
10 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "HGScannerLib.h"
#include "base/HGInc.h"
#include "base/HGImage.h"
#include "imgfmt/HGImgFmt.h"
#include "imgproc/HGOCR.h"
#include "HGString.h"
#include "HGLibDeviceImpl.hpp"
#pragma pack(push)
#pragma pack(4)
struct HGLibSaveImageParam_V1
{
HGUInt size; /* 结构体大小 */
HGUInt jpegQuality; /* jpeg下有效, 0-100 */
HGUInt tiffCompression; /* tiff下有效, HGLIB_TIFFCOMPRESSION_* */
HGUInt tiffJpegQuality; /* tiff且HGLIB_TIFFCOMPRESSION_JPEG下有效, 0-100 */
HGBool ocr; /* 是否OCRpdf和ofd格式有效 */
};
#pragma pack(pop)
HGBool HGAPI HGLib_MemoryCopy(HGVoid *dest, const HGVoid *src, HGUInt size)
{
if (NULL == dest || NULL == src || 0 == size)
{
return HGFALSE;
}
memcpy(dest, src, size);
return HGTRUE;
}
HGLibImage HGAPI HGLib_LoadImage(const HGChar* filePath)
{
if (NULL == filePath)
{
return NULL;
}
HGImage image = NULL;
HGImgFmt_LoadImage(Utf8ToStdString(filePath).c_str(), 0, NULL, 0, HGBASE_IMGORIGIN_TOP, &image);
return (HGLibImage)image;
}
HGLibImage HGAPI HGLib_CloneImage(HGLibImage image, HGUInt type, HGUInt origin)
{
if (NULL == image)
{
return NULL;
}
HGImage destImage = NULL;
HGBase_CloneImage((HGImage)image, type, origin, &destImage);
return (HGLibImage)destImage;
}
HGByte* HGAPI HGLib_GetImageData(HGLibImage image)
{
if (NULL == image)
{
return NULL;
}
HGByte* data = NULL;
HGBase_GetImageData((HGImage)image, &data);
return data;
}
HGBool HGAPI HGLib_GetImageInfo(HGLibImage image, HGLibImageInfo* imageInfo)
{
if (NULL == image || NULL == imageInfo)
{
return HGFALSE;
}
HGImageInfo imgInfo;
if (HGBASE_ERR_OK != HGBase_GetImageInfo((HGImage)image, &imgInfo))
{
return HGFALSE;
}
imageInfo->width = imgInfo.width;
imageInfo->height = imgInfo.height;
imageInfo->type = imgInfo.type;
imageInfo->origin = imgInfo.origin;
imageInfo->widthStep = imgInfo.widthStep;
return HGTRUE;
}
HGBool HGAPI HGLib_GetImageDpi(HGLibImage image, HGUInt *xDpi, HGUInt *yDpi)
{
if (NULL == image || NULL == xDpi || NULL == yDpi)
{
return HGFALSE;
}
HGBase_GetImageDpi((HGImage)image, xDpi, yDpi);
return HGTRUE;
}
HGBool HGAPI HGLib_SaveImage(HGLibImage image, const HGChar* savePath, const HGLibSaveImageParam* saveParam)
{
if (NULL == image || NULL == savePath)
{
return HGFALSE;
}
HGUInt fmtType = 0;
HGImgFmt_GetImgFmtTypeFromFileName(Utf8ToStdString(savePath).c_str(), &fmtType);
if (fmtType < HGIMGFMT_TYPE_JPEG || fmtType > HGIMGFMT_TYPE_GIF)
{
return HGFALSE;
}
if (NULL == saveParam)
{
return (HGBASE_ERR_OK == HGImgFmt_SaveImage((HGImage)image, fmtType, NULL, Utf8ToStdString(savePath).c_str()));
}
if (saveParam->size == sizeof(HGLibSaveImageParam_V1))
{
const HGLibSaveImageParam_V1* saveParamV1 = (const HGLibSaveImageParam_V1*)saveParam;
if ((HGIMGFMT_TYPE_PDF == fmtType || HGIMGFMT_TYPE_OFD == fmtType) && saveParamV1->ocr)
{
HGOCRMgr ocrMgr = NULL;
HGImgProc_CreateOCRMgr(0, &ocrMgr);
if (NULL == ocrMgr)
{
return HGFALSE;
}
HGBool ret = (HGBASE_ERR_OK == HGImgProc_ImageOCRToFile(ocrMgr, (HGImage)image, 0, Utf8ToStdString(savePath).c_str()));
HGImgProc_DestroyOCRMgr(ocrMgr);
return ret;
}
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = saveParamV1->jpegQuality;
saveInfo.tiffCompression = 0;
if (HGLIB_TIFFCOMPRESSION_NONE == saveParamV1->tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
else if (HGLIB_TIFFCOMPRESSION_CCITTFAX4 == saveParamV1->tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
else if (HGLIB_TIFFCOMPRESSION_LZW == saveParamV1->tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (HGLIB_TIFFCOMPRESSION_JPEG == saveParamV1->tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
saveInfo.tiffJpegQuality = saveParamV1->tiffJpegQuality;
return (HGBASE_ERR_OK == HGImgFmt_SaveImage((HGImage)image, fmtType, &saveInfo, Utf8ToStdString(savePath).c_str()));
}
return HGFALSE;
}
HGBool HGAPI HGLib_ReleaseImage(HGLibImage image)
{
if (NULL == image)
{
return HGFALSE;
}
HGBase_DestroyImage((HGImage)image);
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)
{
return HGLibDeviceImpl::Init(func, param);
}
HGBool HGAPI HGLib_DeinitDevice()
{
return HGLibDeviceImpl::Deinit();
}
HGChar** HGAPI HGLib_GetDeviceNameList()
{
return HGLibDeviceImpl::GetNameList();
}
HGBool HGAPI HGLib_ReleaseDeviceNameList(HGChar** deviceName)
{
return HGLibDeviceImpl::ReleaseNameList(deviceName);
}
HGLibDevice HGAPI HGLib_OpenDevice(const HGChar* deviceName)
{
return (HGLibDevice)HGLibDeviceImpl::Open(deviceName);
}
HGBool HGAPI HGLib_CloseDevice(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->Close();
}
HGBool HGAPI HGLib_GetDeviceType(HGLibDevice device, HGChar *type, HGUInt maxLen)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetType(type, maxLen);
}
HGBool HGAPI HGLib_GetDeviceSN(HGLibDevice device, HGChar* sn, HGUInt maxLen)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetSN(sn, maxLen);
}
HGBool HGAPI HGLib_GetDeviceFWVersion(HGLibDevice device, HGChar* fwVersion, HGUInt maxLen)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetFWVersion(fwVersion, maxLen);
}
HGInt HGAPI HGLib_GetDeviceRollerCount(HGLibDevice device)
{
if (NULL == device)
{
return -1;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetRollerCount();
}
HGInt HGAPI HGLib_GetDeviceTotalCount(HGLibDevice device)
{
if (NULL == device)
{
return -1;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetTotalCount();
}
HGBool HGAPI HGLib_ClearDeviceRollerCount(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->ClearRollerCount();
}
HGBool HGAPI HGLib_SetDeviceParam(HGLibDevice device, HGUInt option, const HGVoid* data)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->SetParam(option, data);
}
HGLibDeviceParamGroup* HGAPI HGLib_GetDeviceParamGroupList(HGLibDevice device, HGUInt* count)
{
if (NULL == device)
{
return NULL;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetParamGroupList(count);
}
HGLibDeviceParam* HGAPI HGLib_GetDeviceParam(HGLibDevice device, HGUInt option)
{
if (NULL == device)
{
return NULL;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetParam(option);
}
HGBool HGAPI HGLib_ReleaseDeviceParamGroupList(HGLibDeviceParamGroup* paramGroup, HGUInt count)
{
return HGLibDeviceImpl::ReleaseParamGroupList(paramGroup, count);
}
HGBool HGAPI HGLib_ReleaseDeviceParam(HGLibDeviceParam* param)
{
return HGLibDeviceImpl::ReleaseParam(param);
}
HGBool HGAPI HGLib_ResetDeviceParam(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->ResetParam();
}
HGBool HGAPI HGLib_DeviceIsPaperOn(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->IsPaperOn();
}
HGInt HGAPI HGLib_GetDeviceStatus(HGLibDevice device)
{
if (NULL == device)
{
return -1;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetStatus();
}
HGBool HGAPI HGLib_DeviceRestart(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->Restart();
}
HGBool HGAPI HGLib_DeviceShutDown(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->ShutDown();
}
HGBool HGAPI HGLib_StartDeviceScan(HGLibDevice device, HGLibDeviceScanEventFunc eventFunc, HGPointer eventParam,
HGLibDeviceScanImageFunc imageFunc, HGPointer imageParam)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->StartScan(eventFunc, eventParam, imageFunc, imageParam);
}
HGBool HGAPI HGLib_StopDeviceScan(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->StopScan();
}
HGBool HGAPI HGLib_StopDeviceScanAsyn(HGLibDevice device)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->StopScanAsyn();
}
HGInt HGAPI HGLib_GetDeviceOperateCode(HGLibDevice device)
{
if (NULL == device)
{
return 0;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetOperateCode();
}