HGScannerLib库增加设置设备参数和获取设备参数的接口

This commit is contained in:
luoliangyi 2022-07-27 15:56:10 +08:00
parent 180741194e
commit 85929e9e01
11 changed files with 345 additions and 12 deletions

View File

@ -12,8 +12,11 @@ HGLib_ReleaseDeviceNameList
HGLib_OpenDevice
HGLib_CloseDevice
HGLib_SetDeviceParam
HGLib_SetDeviceParamEx
HGLib_GetDeviceParamGroupList
HGLib_GetDeviceParam
HGLib_ReleaseDeviceParamGroupList
HGLib_ReleaseDeviceParam
HGLib_ResetDeviceParam
HGLib_StartDeviceScan
HGLib_StopDeviceScan

View File

@ -26,6 +26,7 @@
<ItemGroup>
<ClInclude Include="..\..\..\sdk\scannerlib\HGLibDeviceImpl.hpp" />
<ClInclude Include="..\..\..\sdk\scannerlib\HGScannerLib.h" />
<ClInclude Include="..\..\..\sdk\scannerlib\HGScannerLibOption.h" />
<ClInclude Include="..\..\..\utility\HGString.h" />
</ItemGroup>
<ItemGroup>

View File

@ -48,6 +48,7 @@
#endif
/* type defines */
typedef void HGVoid;
typedef char HGChar;
typedef unsigned char HGByte;
typedef short HGShort;

View File

@ -214,8 +214,13 @@ HGBool HGLibDeviceImpl::SetParam(const HGLibDeviceSetParam* param, HGUInt count)
HGBool ret = HGTRUE;
for (HGUInt i = 0; i < count; ++i)
{
bool find = false;
if (NULL == param[i].title)
{
ret = HGFALSE;
continue;
}
bool find = false;
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int j = 1; j < num_dev_options; ++j)
@ -232,25 +237,30 @@ HGBool HGLibDeviceImpl::SetParam(const HGLibDeviceSetParam* param, HGUInt count)
{
if (SANE_TYPE_STRING == desp->type)
{
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, (void*)param[i].stringValue, NULL))
SANE_Char* value = param[i].stringValue;
if (param[i].type != HGLIB_DEVPARAM_TYPE_STRING
|| SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, value, NULL))
ret = HGFALSE;
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = param[i].intValue;
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL))
if (param[i].type != HGLIB_DEVPARAM_TYPE_INT
|| SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL))
ret = HGFALSE;
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(param[i].doubleValue);
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL))
if (param[i].type != HGLIB_DEVPARAM_TYPE_DOUBLE
|| SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL))
ret = HGFALSE;
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)param[i].boolValue;
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL))
if (param[i].type != HGLIB_DEVPARAM_TYPE_BOOL
|| SANE_STATUS_GOOD != sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL))
ret = HGFALSE;
}
@ -266,6 +276,59 @@ HGBool HGLibDeviceImpl::SetParam(const HGLibDeviceSetParam* param, HGUInt count)
return ret;
}
HGBool HGLibDeviceImpl::SetParamEx(const HGChar* option, const HGVoid* data)
{
assert(NULL != m_devHandle);
if (NULL == option || NULL == data)
return HGFALSE;
HGBool ret = HGFALSE;
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* title = desp->title;
while (' ' == *title)
++title;
if (0 == strcmp(option, title))
{
if (SANE_TYPE_STRING == desp->type)
{
SANE_Char* value = (SANE_Char*)(HGChar*)data;
if (SANE_STATUS_GOOD == sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, value, NULL))
ret = HGTRUE;
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = (SANE_Int)(*(HGInt*)data);
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL))
ret = HGTRUE;
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(*(HGDouble*)data);
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL))
ret = HGTRUE;
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)(*(HGBool*)data);
if (SANE_STATUS_GOOD != sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL))
ret = HGTRUE;
}
break;
}
}
return ret;
}
HGLibDeviceGetParamGroup* HGLibDeviceImpl::GetParamGroupList(HGUInt* count)
{
assert(NULL != m_devHandle);
@ -484,6 +547,186 @@ HGLibDeviceGetParamGroup* HGLibDeviceImpl::GetParamGroupList(HGUInt* count)
return paramGroup;
}
HGLibDeviceGetParam* HGLibDeviceImpl::GetParam(const HGChar* option)
{
assert(NULL != m_devHandle);
if (NULL == option)
return NULL;
HGLibDeviceGetParam* param = NULL;
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* title = desp->title;
while (' ' == *title)
++title;
if (0 == strcmp(option, title))
{
DeviceParam devParam;
devParam.title = title;
if (SANE_TYPE_STRING == desp->type)
{
char value[256] = { 0 };
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, value, NULL);
devParam.valueType = HGLIB_DEVPARAM_TYPE_STRING;
devParam.stringValue = value;
assert(SANE_CONSTRAINT_STRING_LIST == desp->constraint_type);
devParam.rangeType = HGLIB_DEVPARAM_RANGETYPE_STRINGLIST;
const SANE_String_Const* p = desp->constraint.string_list;
while (NULL != *p)
{
devParam.stringValueList.push_back(*p);
++p;
}
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
devParam.valueType = HGLIB_DEVPARAM_TYPE_INT;
devParam.intValue = (int)value;
if (SANE_CONSTRAINT_WORD_LIST == desp->constraint_type)
{
devParam.rangeType = HGLIB_DEVPARAM_RANGETYPE_INTLIST;
const SANE_Word* p = desp->constraint.word_list;
for (SANE_Int i = 0; i < p[0]; ++i)
{
devParam.intValueList.push_back(p[i + 1]);
}
}
else if (SANE_CONSTRAINT_RANGE == desp->constraint_type)
{
devParam.rangeType = HGLIB_DEVPARAM_RANGETYPE_INTRANGE;
devParam.intValueMin = desp->constraint.range->min;
devParam.intValueMax = desp->constraint.range->max;
}
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Word value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
devParam.valueType = HGLIB_DEVPARAM_TYPE_DOUBLE;
devParam.doubleValue = SANE_UNFIX(value);
if (SANE_CONSTRAINT_WORD_LIST == desp->constraint_type)
{
devParam.rangeType = HGLIB_DEVPARAM_RANGETYPE_DOUBLELIST;
const SANE_Word* p = desp->constraint.word_list;
for (SANE_Int i = 0; i < p[0]; ++i)
{
devParam.doubleValueList.push_back(SANE_UNFIX(p[i + 1]));
}
}
else if (SANE_CONSTRAINT_RANGE == desp->constraint_type)
{
devParam.rangeType = HGLIB_DEVPARAM_RANGETYPE_DOUBLERANGE;
devParam.doubleValueMin = SANE_UNFIX(desp->constraint.range->min);
devParam.doubleValueMax = SANE_UNFIX(desp->constraint.range->max);
}
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
devParam.valueType = HGLIB_DEVPARAM_TYPE_BOOL;
devParam.boolValue = (bool)value;
devParam.rangeType = HGLIB_DEVPARAM_RANGETYPE_NULL;
}
assert(0 != devParam.valueType);
param = (HGLibDeviceGetParam*)malloc(sizeof(HGLibDeviceGetParam));
if (NULL != param)
{
param->param.title = (HGChar*)malloc(devParam.title.size() + 1);
if (NULL != param->param.title)
strcpy(param->param.title, devParam.title.c_str());
param->param.type = devParam.valueType;
if (HGLIB_DEVPARAM_TYPE_INT == param->param.type)
param->param.intValue = devParam.intValue;
else if (HGLIB_DEVPARAM_TYPE_DOUBLE == param->param.type)
param->param.doubleValue = devParam.doubleValue;
else if (HGLIB_DEVPARAM_TYPE_BOOL == param->param.type)
param->param.boolValue = (HGBool)devParam.boolValue;
else if (HGLIB_DEVPARAM_TYPE_STRING == param->param.type)
{
param->param.stringValue = (HGChar*)malloc(devParam.stringValue.size() + 1);
if (NULL != param->param.stringValue)
strcpy(param->param.stringValue, devParam.stringValue.c_str());
}
param->rangeType = devParam.rangeType;
if (HGLIB_DEVPARAM_RANGETYPE_INTLIST == param->rangeType)
{
param->intValueList.count = (HGUInt)devParam.intValueList.size();
param->intValueList.value = (HGInt*)malloc(param->intValueList.count * sizeof(HGInt));
if (NULL != param->intValueList.value)
{
for (int k = 0; k < (int)devParam.intValueList.size(); ++k)
{
param->intValueList.value[k] = devParam.intValueList[k];
}
}
}
else if (HGLIB_DEVPARAM_RANGETYPE_DOUBLELIST == param->rangeType)
{
param->doubleValueList.count = (HGUInt)devParam.doubleValueList.size();
param->doubleValueList.value = (HGDouble*)malloc(param->doubleValueList.count * sizeof(HGDouble));
if (NULL != param->doubleValueList.value)
{
for (int k = 0; k < (int)devParam.doubleValueList.size(); ++k)
{
param->doubleValueList.value[k] = devParam.doubleValueList[k];
}
}
}
else if (HGLIB_DEVPARAM_RANGETYPE_INTRANGE == param->rangeType)
{
param->intValueRange.minValue = devParam.intValueMin;
param->intValueRange.maxValue = devParam.intValueMax;
}
else if (HGLIB_DEVPARAM_RANGETYPE_DOUBLERANGE == param->rangeType)
{
param->doubleValueRange.minValue = devParam.doubleValueMin;
param->doubleValueRange.maxValue = devParam.doubleValueMax;
}
else if (HGLIB_DEVPARAM_RANGETYPE_STRINGLIST == param->rangeType)
{
param->stringValueList.count = (HGUInt)devParam.stringValueList.size();
param->stringValueList.value = (HGChar**)malloc(param->stringValueList.count * sizeof(HGChar*));
if (NULL != param->stringValueList.value)
{
for (int k = 0; k < (int)devParam.stringValueList.size(); ++k)
{
param->stringValueList.value[k] = (HGChar*)malloc(devParam.stringValueList[k].size() + 1);
if (NULL != param->stringValueList.value[k])
strcpy(param->stringValueList.value[k], devParam.stringValueList[k].c_str());
}
}
}
}
break;
}
}
return param;
}
HGBool HGLibDeviceImpl::ReleaseParamGroupList(HGLibDeviceGetParamGroup* paramGroup, HGUInt count)
{
if (NULL == paramGroup || 0 == count)
@ -519,6 +762,32 @@ HGBool HGLibDeviceImpl::ReleaseParamGroupList(HGLibDeviceGetParamGroup* paramGro
return HGTRUE;
}
HGBool HGLibDeviceImpl::ReleaseParam(HGLibDeviceGetParam* param)
{
if (NULL == param)
{
return HGFALSE;
}
free(param->param.title);
if (HGLIB_DEVPARAM_TYPE_STRING == param->param.type)
free(param->param.stringValue);
if (HGLIB_DEVPARAM_RANGETYPE_INTLIST == param->rangeType)
free(param->intValueList.value);
else if (HGLIB_DEVPARAM_RANGETYPE_DOUBLELIST == param->rangeType)
free(param->doubleValueList.value);
else if (HGLIB_DEVPARAM_RANGETYPE_STRINGLIST == param->rangeType)
{
for (HGUInt k = 0; k < param->stringValueList.count; ++k)
free(param->stringValueList.value[k]);
free(param->stringValueList.value);
}
free(param);
return HGTRUE;
}
HGBool HGLibDeviceImpl::ResetParam()
{
assert(NULL != m_devHandle);

View File

@ -24,8 +24,11 @@ public:
static HGLibDeviceImpl* Open(const HGChar* deviceName);
HGBool Close();
HGBool SetParam(const HGLibDeviceSetParam* param, HGUInt count);
HGBool SetParamEx(const HGChar* option, const HGVoid* data);
HGLibDeviceGetParamGroup* GetParamGroupList(HGUInt* count);
HGLibDeviceGetParam* GetParam(const HGChar* option);
static HGBool ReleaseParamGroupList(HGLibDeviceGetParamGroup* paramGroup, HGUInt count);
static HGBool ReleaseParam(HGLibDeviceGetParam* param);
HGBool ResetParam();
HGBool StartScan(HGLibDeviceScanEventFunc eventFunc, HGPointer eventParam,
HGLibDeviceScanImageFunc imageFunc, HGPointer imageParam);

View File

@ -140,6 +140,17 @@ HGBool HGAPI HGLib_SetDeviceParam(HGLibDevice device, const HGLibDeviceSetParam*
return deviceImpl->SetParam(param, count);
}
HGBool HGAPI HGLib_SetDeviceParamEx(HGLibDevice device, const HGChar* option, const HGVoid* data)
{
if (NULL == device)
{
return HGFALSE;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->SetParamEx(option, data);
}
HGLibDeviceGetParamGroup* HGAPI HGLib_GetDeviceParamGroupList(HGLibDevice device, HGUInt* count)
{
if (NULL == device)
@ -151,11 +162,27 @@ HGLibDeviceGetParamGroup* HGAPI HGLib_GetDeviceParamGroupList(HGLibDevice device
return deviceImpl->GetParamGroupList(count);
}
HGLibDeviceGetParam* HGAPI HGLib_GetDeviceParam(HGLibDevice device, const HGChar* option)
{
if (NULL == device)
{
return NULL;
}
HGLibDeviceImpl* deviceImpl = (HGLibDeviceImpl*)device;
return deviceImpl->GetParam(option);
}
HGBool HGAPI HGLib_ReleaseDeviceParamGroupList(HGLibDeviceGetParamGroup* paramGroup, HGUInt count)
{
return HGLibDeviceImpl::ReleaseParamGroupList(paramGroup, count);
}
HGBool HGAPI HGLib_ReleaseDeviceParam(HGLibDeviceGetParam* param)
{
return HGLibDeviceImpl::ReleaseParam(param);
}
HGBool HGAPI HGLib_ResetDeviceParam(HGLibDevice device)
{
if (NULL == device)

View File

@ -2,6 +2,7 @@
#define __HGSCANNERLIB_H__
#include "HGDef.h"
#include "HGScannerLibOption.h"
HG_DECLARE_HANDLE(HGLibImage);
HG_DECLARE_HANDLE(HGLibDevice);
@ -160,12 +161,15 @@ HGEXPORT HGBool HGAPI HGLib_CloseDevice(HGLibDevice device);
/* 设置设备参数 */
HGEXPORT HGBool HGAPI HGLib_SetDeviceParam(HGLibDevice device, const HGLibDeviceSetParam *param, HGUInt count);
HGEXPORT HGBool HGAPI HGLib_SetDeviceParamEx(HGLibDevice device, const HGChar *option, const HGVoid *data);
/* 获取设备参数 */
HGEXPORT HGLibDeviceGetParamGroup* HGAPI HGLib_GetDeviceParamGroupList(HGLibDevice device, HGUInt *count);
HGEXPORT HGLibDeviceGetParam* HGAPI HGLib_GetDeviceParam(HGLibDevice device, const HGChar* option);
/* 销毁设备参数 */
HGEXPORT HGBool HGAPI HGLib_ReleaseDeviceParamGroupList(HGLibDeviceGetParamGroup* paramGroup, HGUInt count);
HGEXPORT HGBool HGAPI HGLib_ReleaseDeviceParam(HGLibDeviceGetParam* param);
/* 重置设备参数 */
HGEXPORT HGBool HGAPI HGLib_ResetDeviceParam(HGLibDevice device);

View File

@ -0,0 +1,4 @@
#ifndef __HGSCANNERLIBOPTION_H__
#define __HGSCANNERLIBOPTION_H__
#endif /* __HGSCANNERLIBOPTION_H__ */

View File

@ -147,11 +147,11 @@ namespace ver_2
int DeleteLocalFile(const std::string& filePath, std::string& errInfo);
// 清理全局文件保存目录
int ClearGlobalFileSavePath(std::string& errInfo);
// 上传文件
int UploadLocalFile(const std::string& filePath, const std::string& remoteFilePath, const std::string& uploadMode,
const std::string& httpHost, int httpPort, const std::string& httpPath, const std::string& ftpUser, const std::string& ftpPassword,
const std::string& ftpHost, int ftpPort, std::string& errInfo);
// 合成本地图像
int MergeLocalImage(const std::vector<std::string>& imagePathList, const std::string& mode,
const std::string& align, int interval, bool temp, std::string& outImagePath, std::string& errInfo);

View File

@ -289,9 +289,9 @@ namespace ver_2
{
ImageClip(json, func);
}
else if ("upload_local_file" == func)
else if ("upload_image" == func || "upload_local_file" == func || "upload_image_base64" == func)
{
UploadLocalFile(json);
UploadFile(json, func);
}
else if ("init_device" == func)
{
@ -1273,11 +1273,27 @@ namespace ver_2
}
}
void WSUser::UploadLocalFile(cJSON* json)
void WSUser::UploadFile(cJSON* json, const std::string& func)
{
assert(NULL != json);
std::string filePath = Utf8ToStdString(GetJsonStringValue(json, "file_path"));
std::string filePath;
if ("upload_image" == func)
{
int imageIndex = GetJsonIntValue(json, "image_index");
std::string errInfo2;
GetManager()->SaveImage(imageIndex, true, filePath, errInfo2);
}
else if ("upload_local_file" == func)
{
filePath = Utf8ToStdString(GetJsonStringValue(json, "file_path"));
}
else if ("upload_image_base64" == func)
{
std::string imageBase64 = GetJsonStringValue(json, "image_base64");
std::string errInfo2;
GetManager()->SaveLocalImage(imageBase64, true, filePath, errInfo2);
}
bool find = false;
std::string remoteFilePath = Utf8ToStdString(GetJsonStringValue(json, "remote_file_path", &find));
@ -1310,13 +1326,18 @@ namespace ver_2
int ret = GetManager()->UploadLocalFile(filePath, remoteFilePath, uploadMode, httpHost, httpPort, httpPath,
ftpUser, ftpPassword, ftpHost, ftpPort, errInfo);
if ("upload_image" == func || "upload_image_base64" == func)
{
HGBase_DeleteFile(filePath.c_str());
}
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
cJSON* retJson = cJSON_CreateObject();
if (NULL != retJson)
{
cJSON_AddItemToObject(retJson, "func", cJSON_CreateString("upload_local_file"));
cJSON_AddItemToObject(retJson, "func", cJSON_CreateString(func.c_str()));
if (findIden)
cJSON_AddItemToObject(retJson, "iden", cJSON_CreateString(iden.c_str()));
cJSON_AddItemToObject(retJson, "ret", cJSON_CreateNumber(ret));

View File

@ -38,8 +38,8 @@ namespace ver_2
void SaveLocalImage(cJSON* json);
void DeleteLocalFile(cJSON* json);
void ClearGlobalFileSavePath(cJSON* json);
void UploadLocalFile(cJSON* json);
void UploadFile(cJSON* json, const std::string& func);
void MergeImage(cJSON* json, const std::string& func);
void MakeMultiImage(cJSON* json, const std::string& func);
void SplitImage(cJSON* json, const std::string& func);