sane_user提供选择设备和扫描的对话框接口

This commit is contained in:
luoliangyi 2023-03-28 17:29:47 +08:00
parent 2fb6bcc3fb
commit 82168a3b94
6 changed files with 184 additions and 27 deletions

View File

@ -7,7 +7,9 @@ HGSane_DestroyManager
HGSane_GetDeviceCount HGSane_GetDeviceCount
HGSane_GetDeviceName HGSane_GetDeviceName
HGSane_OpenDevice HGSane_OpenDevice
HGSane_OpenSelectedDevice
HGSane_CloseDevice HGSane_CloseDevice
HGSane_ShowDeviceSettingDlg HGSane_ShowDeviceSettingDlg
HGSane_StartDevice HGSane_StartDevice
HGSane_StopDevice HGSane_StopDevice
HGSane_StartDeviceWithUI

View File

@ -108,6 +108,17 @@ typedef HGUInt HGResult;
#define HGAPIV #define HGAPIV
#endif #endif
#if defined(HG_CMP_MSC)
#include <windows.h>
typedef HWND HGWindow;
#else
#ifdef __cplusplus
typedef class QWidget* HGWindow;
#else
typedef void* HGWindow;
#endif
#endif
#define HG_DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name #define HG_DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
#pragma pack(push) #pragma pack(push)

View File

@ -79,6 +79,25 @@ HGResult HGAPI HGSane_OpenDevice(HGSaneManager mgr, HGUInt index, HGSaneDevice*
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
HGResult HGAPI HGSane_OpenSelectedDevice(HGSaneManager mgr, HGWindow parent, HGSaneDevice* dev)
{
if (NULL == mgr)
{
return HGBASE_ERR_INVALIDARG;
}
HGSaneManagerImpl* saneManagerImpl = (HGSaneManagerImpl*)mgr;
class HGSaneDeviceImpl* saneDeviceImpl = NULL;
HGResult ret = saneManagerImpl->OpenSelectedDevice(parent, &saneDeviceImpl);
if (HGBASE_ERR_OK != ret)
{
return ret;
}
*dev = (HGSaneDevice)saneDeviceImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev) HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev)
{ {
if (NULL == dev) if (NULL == dev)
@ -90,11 +109,7 @@ HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev)
return saneDeviceImpl->Close(); return saneDeviceImpl->Close();
} }
#ifdef HG_CMP_MSC HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HGWindow parent)
HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HWND parent)
#else
HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, class QWidget* parent)
#endif
{ {
if (NULL == dev) if (NULL == dev)
{ {
@ -126,4 +141,15 @@ HGResult HGAPI HGSane_StopDevice(HGSaneDevice dev)
HGSaneDeviceImpl* saneDeviceImpl = (HGSaneDeviceImpl*)dev; HGSaneDeviceImpl* saneDeviceImpl = (HGSaneDeviceImpl*)dev;
return saneDeviceImpl->Stop(); return saneDeviceImpl->Stop();
}
HGResult HGAPI HGSane_StartDeviceWithUI(HGSaneDevice dev, HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam)
{
if (NULL == dev)
{
return HGBASE_ERR_INVALIDARG;
}
HGSaneDeviceImpl* saneDeviceImpl = (HGSaneDeviceImpl*)dev;
return saneDeviceImpl->StartWithUI(parent, imageFunc, imageParam);
} }

View File

@ -24,17 +24,17 @@ HGEXPORT HGResult HGAPI HGSane_GetDeviceName(HGSaneManager mgr, HGUInt index, HG
HGEXPORT HGResult HGAPI HGSane_OpenDevice(HGSaneManager mgr, HGUInt index, HGSaneDevice *dev, HGChar *errInfo, HGUInt errInfoLen); HGEXPORT HGResult HGAPI HGSane_OpenDevice(HGSaneManager mgr, HGUInt index, HGSaneDevice *dev, HGChar *errInfo, HGUInt errInfoLen);
HGEXPORT HGResult HGAPI HGSane_OpenSelectedDevice(HGSaneManager mgr, HGWindow parent, HGSaneDevice* dev);
HGEXPORT HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev); HGEXPORT HGResult HGAPI HGSane_CloseDevice(HGSaneDevice dev);
#ifdef HG_CMP_MSC HGEXPORT HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HGWindow parent);
HGEXPORT HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, HWND parent);
#else
HGEXPORT HGResult HGAPI HGSane_ShowDeviceSettingDlg(HGSaneDevice dev, class QWidget* parent);
#endif
HGEXPORT HGResult HGAPI HGSane_StartDevice(HGSaneDevice dev, HGSane_DeviceEventFunc eventFunc, HGPointer eventParam, HGEXPORT HGResult HGAPI HGSane_StartDevice(HGSaneDevice dev, HGSane_DeviceEventFunc eventFunc, HGPointer eventParam,
HGSane_DeviceImageFunc imageFunc, HGPointer imageParam, HGChar* errInfo, HGUInt errInfoLen); HGSane_DeviceImageFunc imageFunc, HGPointer imageParam, HGChar* errInfo, HGUInt errInfoLen);
HGEXPORT HGResult HGAPI HGSane_StopDevice(HGSaneDevice dev); HGEXPORT HGResult HGAPI HGSane_StopDevice(HGSaneDevice dev);
HGEXPORT HGResult HGAPI HGSane_StartDeviceWithUI(HGSaneDevice dev, HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam);
#endif /* __HGSANE_H__ */ #endif /* __HGSANE_H__ */

View File

@ -1,10 +1,10 @@
#include "HGSaneImpl.hpp" #include "HGSaneImpl.hpp"
#include "../base/HGInc.h" #include "../base/HGInc.h"
#include <string>
HGSaneManagerImpl::HGSaneManagerImpl() HGSaneManagerImpl::HGSaneManagerImpl()
{ {
m_dll = NULL; m_dll = NULL;
m_saneDlgDll = NULL;
m_f_sane_init = NULL; m_f_sane_init = NULL;
m_f_sane_exit = NULL; m_f_sane_exit = NULL;
m_f_sane_get_devices = NULL; m_f_sane_get_devices = NULL;
@ -18,7 +18,11 @@ HGSaneManagerImpl::HGSaneManagerImpl()
m_f_sane_get_parameters = NULL; m_f_sane_get_parameters = NULL;
m_f_sane_get_option_descriptor = NULL; m_f_sane_get_option_descriptor = NULL;
m_f_sane_control_option = NULL; m_f_sane_control_option = NULL;
m_saneDlgDll = NULL;
m_f_show_devlist_ui = NULL;
m_f_show_setting_ui = NULL; m_f_show_setting_ui = NULL;
m_f_show_scan_ui = NULL;
} }
HGSaneManagerImpl::~HGSaneManagerImpl() HGSaneManagerImpl::~HGSaneManagerImpl()
@ -47,7 +51,9 @@ HGResult HGSaneManagerImpl::Create(const HGChar* sanePath)
HGBase_CreateDll("HGSaneDlg.dll", &m_saneDlgDll); HGBase_CreateDll("HGSaneDlg.dll", &m_saneDlgDll);
if (NULL != m_saneDlgDll) if (NULL != m_saneDlgDll)
{ {
HGBase_GetDllProcAddress(m_saneDlgDll, "show_devlist_ui", (HGPointer*)&m_f_show_devlist_ui);
HGBase_GetDllProcAddress(m_saneDlgDll, "show_setting_ui", (HGPointer*)&m_f_show_setting_ui); HGBase_GetDllProcAddress(m_saneDlgDll, "show_setting_ui", (HGPointer*)&m_f_show_setting_ui);
HGBase_GetDllProcAddress(m_saneDlgDll, "show_scan_ui", (HGPointer*)&m_f_show_scan_ui);
} }
if (SANE_STATUS_GOOD != m_f_sane_init(NULL, NULL)) if (SANE_STATUS_GOOD != m_f_sane_init(NULL, NULL))
@ -164,6 +170,53 @@ HGResult HGSaneManagerImpl::OpenDevice(HGUInt index, HGSaneDeviceImpl** deviceIm
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
HGResult HGSaneManagerImpl::OpenSelectedDevice(HGWindow parent, class HGSaneDeviceImpl** deviceImpl)
{
if (NULL == deviceImpl)
{
return HGBASE_ERR_INVALIDARG;
}
if (NULL == m_f_show_devlist_ui)
{
return HGBASE_ERR_FAIL;
}
const SANE_Device** device_list;
if (SANE_STATUS_GOOD != m_f_sane_get_devices(&device_list, SANE_TRUE))
{
return HGSANE_ERR_FAIL;
}
HGUInt count = 0;
const SANE_Device** p;
for (p = device_list; *p != NULL; ++p)
++count;
const char** devNameList = new const char*[count + 1];
for (HGUInt i = 0; i < count; ++i)
devNameList[i] = device_list[i]->name;
devNameList[count] = 0;
SANE_Handle handle = m_f_show_devlist_ui(devNameList, parent);
delete []devNameList;
if (NULL == handle)
{
return HGSANE_ERR_FAIL;
}
HGSaneDeviceImpl* newDeviceImpl = new HGSaneDeviceImpl(this);
HGResult ret = newDeviceImpl->Init(handle);
if (HGBASE_ERR_OK != ret)
{
delete newDeviceImpl;
return ret;
}
m_listDeviceImpl.push_back(newDeviceImpl);
*deviceImpl = newDeviceImpl;
return HGBASE_ERR_OK;
}
HGResult HGSaneManagerImpl::FindFunctions() HGResult HGSaneManagerImpl::FindFunctions()
{ {
HGResult ret = HGBASE_ERR_OK; HGResult ret = HGBASE_ERR_OK;
@ -262,6 +315,15 @@ HGSaneDeviceImpl::~HGSaneDeviceImpl()
} }
HGResult HGSaneDeviceImpl::Init(SANE_Handle handle)
{
assert(NULL == m_devHandle);
m_mgrImpl->m_f_sane_set_io_mode(handle, SANE_FALSE);
m_devHandle = handle;
return HGBASE_ERR_OK;
}
HGResult HGSaneDeviceImpl::Open(const HGChar* devName, HGChar* errInfo, HGUInt errInfoLen) HGResult HGSaneDeviceImpl::Open(const HGChar* devName, HGChar* errInfo, HGUInt errInfoLen)
{ {
assert(NULL == m_devHandle); assert(NULL == m_devHandle);
@ -303,12 +365,13 @@ HGResult HGSaneDeviceImpl::Close()
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
#ifdef HG_CMP_MSC HGResult HGSaneDeviceImpl::ShowSettingDlg(HGWindow parent)
HGResult HGSaneDeviceImpl::ShowSettingDlg(HWND parent)
#else
HGResult HGSaneDeviceImpl::ShowSettingDlg(class QWidget* parent)
#endif
{ {
if (NULL != m_thread)
{
return HGBASE_ERR_FAIL;
}
if (NULL == m_mgrImpl->m_f_show_setting_ui) if (NULL == m_mgrImpl->m_f_show_setting_ui)
{ {
return HGBASE_ERR_FAIL; return HGBASE_ERR_FAIL;
@ -402,6 +465,25 @@ HGResult HGSaneDeviceImpl::Stop()
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
HGResult HGSaneDeviceImpl::StartWithUI(HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam)
{
if (NULL != m_thread)
{
return HGBASE_ERR_FAIL;
}
if (NULL == m_mgrImpl->m_f_show_scan_ui)
{
return HGBASE_ERR_FAIL;
}
m_dpi = GetDpi();
m_imageFunc = imageFunc;
m_imageParam = imageParam;
m_mgrImpl->m_f_show_scan_ui(m_devHandle, parent, ShowScanImageCallback, this);
return HGBASE_ERR_OK;
}
HGUInt HGSaneDeviceImpl::GetDpi() HGUInt HGSaneDeviceImpl::GetDpi()
{ {
HGUInt dpi = 0; HGUInt dpi = 0;
@ -510,4 +592,36 @@ void HGAPI HGSaneDeviceImpl::ThreadFunc(HGThread thread, HGPointer param)
} }
} }
} }
}
void HGSaneDeviceImpl::ShowScanImageCallback(const SANE_Parameters* imageFormat, const SANE_Byte* imageData, void* callbackParam)
{
HGSaneDeviceImpl* p = (HGSaneDeviceImpl*)callbackParam;
if (NULL != p->m_imageFunc)
{
HGUInt imgType = 0;
if (imageFormat->format == SANE_FRAME_GRAY)
{
if (1 == imageFormat->depth)
imgType = HGBASE_IMGTYPE_BINARY;
else if (8 == imageFormat->depth)
imgType = HGBASE_IMGTYPE_GRAY;
}
else if (imageFormat->format == SANE_FRAME_RGB)
{
imgType = HGBASE_IMGTYPE_RGB;
}
HGImageInfo imgInfo = { (HGUInt)imageFormat->pixels_per_line, (HGUInt)imageFormat->lines,
imgType, (HGUInt)imageFormat->bytes_per_line, HGBASE_IMGORIGIN_TOP };
HGImage img = NULL;
HGBase_CreateImageWithData((HGByte*)imageData, &imgInfo, &img);
if (NULL != img)
{
HGBase_SetImageDpi(img, p->m_dpi, p->m_dpi);
p->m_imageFunc((HGSaneDevice)p, img, p->m_imageParam);
HGBase_DestroyImage(img);
}
}
} }

View File

@ -24,11 +24,11 @@ typedef SANE_String_Const (*f_sane_strstatus)(SANE_Status status);
typedef SANE_Status (*f_sane_get_parameters)(SANE_Handle handle, SANE_Parameters* params); typedef SANE_Status (*f_sane_get_parameters)(SANE_Handle handle, SANE_Parameters* params);
typedef SANE_Option_Descriptor* (*f_sane_get_option_descriptor)(SANE_Handle handle, SANE_Int option); typedef SANE_Option_Descriptor* (*f_sane_get_option_descriptor)(SANE_Handle handle, SANE_Int option);
typedef SANE_Status (*f_sane_control_option)(SANE_Handle handle, SANE_Int option, SANE_Action action, void* value, SANE_Int* info); typedef SANE_Status (*f_sane_control_option)(SANE_Handle handle, SANE_Int option, SANE_Action action, void* value, SANE_Int* info);
#ifdef HG_CMP_MSC
typedef void (*f_show_setting_ui)(SANE_Handle handle, HWND parent); typedef SANE_Handle (*f_show_devlist_ui)(const char **devNameList, HGWindow parent);
#else typedef void (*f_show_setting_ui)(SANE_Handle handle, HGWindow parent);
typedef void (*f_show_setting_ui)(SANE_Handle handle, class QWidget* parent); typedef void (*f_show_scan_ui_image_callback)(const SANE_Parameters *imageFormat, const SANE_Byte *imageData, void * callbackParam);
#endif typedef void (*f_show_scan_ui)(SANE_Handle handle, HGWindow parent, f_show_scan_ui_image_callback callback, void *callbackParam);
class HGSaneManagerImpl class HGSaneManagerImpl
{ {
@ -42,6 +42,7 @@ public:
HGResult GetDeviceCount(HGUInt *count); HGResult GetDeviceCount(HGUInt *count);
HGResult GetDeviceName(HGUInt index, HGChar* name, HGUInt maxLen); HGResult GetDeviceName(HGUInt index, HGChar* name, HGUInt maxLen);
HGResult OpenDevice(HGUInt index, class HGSaneDeviceImpl **deviceImpl, HGChar* errInfo, HGUInt errInfoLen); HGResult OpenDevice(HGUInt index, class HGSaneDeviceImpl **deviceImpl, HGChar* errInfo, HGUInt errInfoLen);
HGResult OpenSelectedDevice(HGWindow parent, class HGSaneDeviceImpl** deviceImpl);
private: private:
HGResult FindFunctions(); HGResult FindFunctions();
@ -49,7 +50,6 @@ private:
private: private:
HGDll m_dll; HGDll m_dll;
HGDll m_saneDlgDll;
f_sane_init m_f_sane_init; f_sane_init m_f_sane_init;
f_sane_exit m_f_sane_exit; f_sane_exit m_f_sane_exit;
f_sane_get_devices m_f_sane_get_devices; f_sane_get_devices m_f_sane_get_devices;
@ -63,7 +63,12 @@ private:
f_sane_get_parameters m_f_sane_get_parameters; f_sane_get_parameters m_f_sane_get_parameters;
f_sane_get_option_descriptor m_f_sane_get_option_descriptor; f_sane_get_option_descriptor m_f_sane_get_option_descriptor;
f_sane_control_option m_f_sane_control_option; f_sane_control_option m_f_sane_control_option;
HGDll m_saneDlgDll;
f_show_devlist_ui m_f_show_devlist_ui;
f_show_setting_ui m_f_show_setting_ui; f_show_setting_ui m_f_show_setting_ui;
f_show_scan_ui m_f_show_scan_ui;
std::list<class HGSaneDeviceImpl*> m_listDeviceImpl; std::list<class HGSaneDeviceImpl*> m_listDeviceImpl;
}; };
@ -73,20 +78,19 @@ public:
HGSaneDeviceImpl(HGSaneManagerImpl* mgrImpl); HGSaneDeviceImpl(HGSaneManagerImpl* mgrImpl);
~HGSaneDeviceImpl(); ~HGSaneDeviceImpl();
HGResult Init(SANE_Handle handle);
HGResult Open(const HGChar *devName, HGChar* errInfo, HGUInt errInfoLen); HGResult Open(const HGChar *devName, HGChar* errInfo, HGUInt errInfoLen);
HGResult Close(); HGResult Close();
#ifdef HG_CMP_MSC HGResult ShowSettingDlg(HGWindow parent);
HGResult ShowSettingDlg(HWND parent);
#else
HGResult ShowSettingDlg(class QWidget *parent);
#endif
HGResult Start(HGSane_DeviceEventFunc eventFunc, HGPointer eventParam, HGResult Start(HGSane_DeviceEventFunc eventFunc, HGPointer eventParam,
HGSane_DeviceImageFunc imageFunc, HGPointer imageParam, HGChar* errInfo, HGUInt errInfoLen); HGSane_DeviceImageFunc imageFunc, HGPointer imageParam, HGChar* errInfo, HGUInt errInfoLen);
HGResult Stop(); HGResult Stop();
HGResult StartWithUI(HGWindow parent, HGSane_DeviceImageFunc imageFunc, HGPointer imageParam);
private: private:
HGUInt GetDpi(); HGUInt GetDpi();
static void HGAPI ThreadFunc(HGThread thread, HGPointer param); static void HGAPI ThreadFunc(HGThread thread, HGPointer param);
static void ShowScanImageCallback(const SANE_Parameters* imageFormat, const SANE_Byte* imageData, void* callbackParam);
private: private:
HGSaneManagerImpl* m_mgrImpl; HGSaneManagerImpl* m_mgrImpl;