解决scannerlib保存图像时dpi不正常的问题

This commit is contained in:
luoliangyi 2023-01-31 15:54:54 +08:00
parent c2ab5c4416
commit a75620c6a4
3 changed files with 29 additions and 10 deletions

View File

@ -220,6 +220,7 @@ HGLibDeviceImpl::HGLibDeviceImpl()
HGBase_CreateLock(&m_lock); HGBase_CreateLock(&m_lock);
m_devHandle = NULL; m_devHandle = NULL;
m_devName.clear(); m_devName.clear();
m_dpi = 96;
m_scanning = HGFALSE; m_scanning = HGFALSE;
m_scanEvent = NULL; m_scanEvent = NULL;
m_eventFunc = NULL; m_eventFunc = NULL;
@ -975,6 +976,13 @@ HGBool HGLibDeviceImpl::StartScan(HGLibDeviceScanEventFunc eventFunc, HGPointer
HGBase_EnterLock(m_lock); HGBase_EnterLock(m_lock);
if (!m_scanning) if (!m_scanning)
{ {
HGLibDeviceParam* devParam = GetParam(HGLIB_OPTION_NAME_FBL);
if (NULL != devParam && devParam->type == HGLIB_OPTION_VALUETYPE_INT)
{
m_dpi = devParam->intValue;
ReleaseParam(devParam);
}
HGBase_CreateEvent(HGFALSE, HGFALSE, &m_scanEvent); HGBase_CreateEvent(HGFALSE, HGFALSE, &m_scanEvent);
assert(NULL != m_scanEvent); assert(NULL != m_scanEvent);
m_eventFunc = eventFunc; m_eventFunc = eventFunc;
@ -993,6 +1001,7 @@ HGBool HGLibDeviceImpl::StartScan(HGLibDeviceScanEventFunc eventFunc, HGPointer
m_imageParam = NULL; m_imageParam = NULL;
HGBase_DestroyEvent(m_scanEvent); HGBase_DestroyEvent(m_scanEvent);
m_scanEvent = NULL; m_scanEvent = NULL;
m_dpi = 96;
} }
else else
{ {
@ -1020,6 +1029,7 @@ HGBool HGLibDeviceImpl::StopScan()
m_imageFunc = NULL; m_imageFunc = NULL;
m_imageParam = NULL; m_imageParam = NULL;
m_scanning = HGFALSE; m_scanning = HGFALSE;
m_dpi = 96;
ret = HGTRUE; ret = HGTRUE;
} }
HGBase_LeaveLock(m_lock); HGBase_LeaveLock(m_lock);
@ -1177,6 +1187,7 @@ int HGLibDeviceImpl::sane_ex_callback(SANE_Handle hdev, int code, void* data, un
deviceImpl->m_imageFunc = NULL; deviceImpl->m_imageFunc = NULL;
deviceImpl->m_imageParam = NULL; deviceImpl->m_imageParam = NULL;
deviceImpl->m_scanning = HGFALSE; deviceImpl->m_scanning = HGFALSE;
deviceImpl->m_dpi = 96;
HGBase_LeaveLock(deviceImpl->m_lock); HGBase_LeaveLock(deviceImpl->m_lock);
if (NULL != eventFunc) if (NULL != eventFunc)
@ -1224,6 +1235,7 @@ int HGLibDeviceImpl::sane_ex_callback(SANE_Handle hdev, int code, void* data, un
HGBase_CreateImageFromData(data, &imgInfo, NULL, 0, HGBASE_IMGORIGIN_TOP, &img); HGBase_CreateImageFromData(data, &imgInfo, NULL, 0, HGBASE_IMGORIGIN_TOP, &img);
if (NULL != img) if (NULL != img)
{ {
HGBase_SetImageDpi(img, deviceImpl->m_dpi, deviceImpl->m_dpi);
deviceImpl->m_imageFunc((HGLibDevice)deviceImpl, (HGLibImage)img, deviceImpl->m_imageParam); deviceImpl->m_imageFunc((HGLibDevice)deviceImpl, (HGLibImage)img, deviceImpl->m_imageParam);
HGBase_DestroyImage(img); HGBase_DestroyImage(img);
} }

View File

@ -57,6 +57,7 @@ private:
HGLock m_lock; HGLock m_lock;
SANE_Handle m_devHandle; SANE_Handle m_devHandle;
std::string m_devName; std::string m_devName;
HGInt m_dpi;
HGBool m_scanning; HGBool m_scanning;
HGEvent m_scanEvent; HGEvent m_scanEvent;
HGLibDeviceScanEventFunc m_eventFunc; HGLibDeviceScanEventFunc m_eventFunc;

View File

@ -156,7 +156,13 @@ namespace WindowsFormsApp1
gamma[0] = 1.0; gamma[0] = 1.0;
Int32 ret3 = HGScannerLib.HGLib_SetDeviceParam(device, 35, Int32 ret3 = HGScannerLib.HGLib_SetDeviceParam(device, 35,
Marshal.UnsafeAddrOfPinnedArrayElement(gamma, 0)); Marshal.UnsafeAddrOfPinnedArrayElement(gamma, 0));
// 设置DPI
UInt32[] dpi = new UInt32[1];
dpi[0] = 133;
Int32 ret4 = HGScannerLib.HGLib_SetDeviceParam(device, 27,
Marshal.UnsafeAddrOfPinnedArrayElement(dpi, 0));
// 获取旋转的配置 // 获取旋转的配置
IntPtr rotateParam = HGScannerLib.HGLib_GetDeviceParam(device, 59); IntPtr rotateParam = HGScannerLib.HGLib_GetDeviceParam(device, 59);
if (IntPtr.Zero != rotateParam) if (IntPtr.Zero != rotateParam)
@ -246,16 +252,16 @@ namespace WindowsFormsApp1
String fileName = String.Format("Scan_{0}.jpg", m_scanCount); String fileName = String.Format("Scan_{0}.jpg", m_scanCount);
++m_scanCount; ++m_scanCount;
Bitmap curBitmap = createBitmap(image); //Bitmap curBitmap = createBitmap(image);
curBitmap.Save(fileName); //curBitmap.Save(fileName);
//HGScannerLib.HGLibSaveImageParam saveParam; HGScannerLib.HGLibSaveImageParam saveParam;
//saveParam.size = (UInt32)Marshal.SizeOf(typeof(HGScannerLib.HGLibSaveImageParam)); saveParam.size = (UInt32)Marshal.SizeOf(typeof(HGScannerLib.HGLibSaveImageParam));
//saveParam.jpegQuality = 80; saveParam.jpegQuality = 80;
//saveParam.tiffCompression = 4; saveParam.tiffCompression = 4;
//saveParam.tiffJpegQuality = 80; saveParam.tiffJpegQuality = 80;
//saveParam.ocr = 0; saveParam.ocr = 0;
//HGScannerLib.HGLib_SaveImage(image, StringToUtf8(fileName), ref saveParam); HGScannerLib.HGLib_SaveImage(image, StringToUtf8(fileName), ref saveParam);
} }
public String Utf8ToString(IntPtr str) public String Utf8ToString(IntPtr str)