完善Native/file传输

This commit is contained in:
gb 2022-06-16 17:28:46 +08:00
parent e579975168
commit 1581c452ca
6 changed files with 38 additions and 19 deletions

View File

@ -115,6 +115,12 @@ enum multiout_value
MULTI_OUT_COLOR_BW, MULTI_OUT_COLOR_BW,
MULTI_OUT_GRAY_BW, MULTI_OUT_GRAY_BW,
}; };
enum twain_xfer
{
TWAIN_XFER_Native = 0, // BITMAPINFOHEADER + bits
TWAIN_XFER_File = 1, // BITMAPFILEHEADER + TWAIN_XFER_Native
TWAIN_XFER_Memory = 2, // to be implementing ...
};
typedef bool(__stdcall* set_opt_value)(void* val, value_role role, void* param); // return false to stop the callback typedef bool(__stdcall* set_opt_value)(void* val, value_role role, void* param); // return false to stop the callback
__declspec(novtable) struct IRef __declspec(novtable) struct IRef
@ -143,7 +149,7 @@ __declspec(novtable) struct ISaneInvoker : public IRef
COM_API_DECLARE(void, set_event_callback(void(*cb)(int ev_type, void* data, unsigned int* len, void* param), void* param)); COM_API_DECLARE(void, set_event_callback(void(*cb)(int ev_type, void* data, unsigned int* len, void* param), void* param));
COM_API_DECLARE(bool, wait_image(DWORD milliseconds = -1)); COM_API_DECLARE(bool, wait_image(DWORD milliseconds = -1));
COM_API_DECLARE(int, get_scanned_images(DWORD milliseconds = 0)); COM_API_DECLARE(int, get_scanned_images(DWORD milliseconds = 0));
COM_API_DECLARE(IScanImg*, take_first_image(void)); // call 'release' on returned value, plz COM_API_DECLARE(IScanImg*, take_first_image(twain_xfer xfer = TWAIN_XFER_Native)); // call 'release' on returned value, plz
COM_API_DECLARE(bool, get_first_image_header(SANE_Parameters* header)); COM_API_DECLARE(bool, get_first_image_header(SANE_Parameters* header));
COM_API_DECLARE(bool, is_online(void)); COM_API_DECLARE(bool, is_online(void));
COM_API_DECLARE(bool, is_paper_on(void)); COM_API_DECLARE(bool, is_paper_on(void));

View File

@ -304,10 +304,11 @@ unsigned int mapping_buf::mapped_bytes(void)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// class scanned_img // class scanned_img
scanned_img::scanned_img(SANE_Parameters head, SANE_Handle dev, int dpi, const wchar_t* tmp_file) : head_(head), dpi_(dpi) scanned_img::scanned_img(SANE_Parameters head, SANE_Handle dev, int dpi
, const wchar_t* tmp_file, twain_xfer xfer) : head_(head), dpi_(dpi)
{ {
size_t bytes = line_bytes() * height(); size_t bytes = line_bytes() * height();
std::string h(file_header(SANE_IMAGE_TYPE_BMP, dpi)); std::string h(file_header(SANE_IMAGE_TYPE_BMP, dpi, xfer));
unsigned char* dst = NULL; unsigned char* dst = NULL;
bool ok = false; bool ok = false;
@ -400,7 +401,7 @@ scanned_img::~scanned_img()
delete data_; delete data_;
} }
std::string scanned_img::file_header(SANE_ImageType type, float resolution) std::string scanned_img::file_header(SANE_ImageType type, float resolution, twain_xfer xfer)
{ {
std::string h(""); std::string h("");
@ -417,7 +418,17 @@ std::string scanned_img::file_header(SANE_ImageType type, float resolution)
bih.biCompression = BI_RGB; bih.biCompression = BI_RGB;
bih.biXPelsPerMeter = bih.biYPelsPerMeter = resolution * 39.37f + .5f; bih.biXPelsPerMeter = bih.biYPelsPerMeter = resolution * 39.37f + .5f;
h = std::string((char*)&bih, sizeof(bih)); if (xfer == TWAIN_XFER_File)
{
BITMAPFILEHEADER fh = { 0 };
fh.bfType = MAKEWORD('B', 'M');
fh.bfSize = sizeof(fh) + bih.biSizeImage + sizeof(bih);
fh.bfOffBits = sizeof(fh) + sizeof(bih);
h = std::string((char*)&fh, sizeof(fh));
}
h += std::string((char*)&bih, sizeof(bih));
} }
return h; return h;

View File

@ -55,10 +55,11 @@ class scanned_img : public IScanImg, virtual public refer
mapping_buf* data_; mapping_buf* data_;
int dpi_; int dpi_;
std::string file_header(SANE_ImageType type, float resolution); std::string file_header(SANE_ImageType type, float resolution, twain_xfer xfer);
public: public:
scanned_img(SANE_Parameters head, SANE_Handle dev, int dpi, const wchar_t* tmp_file); scanned_img(SANE_Parameters head, SANE_Handle dev, int dpi, const wchar_t* tmp_file
, twain_xfer xfer = TWAIN_XFER_Native);
protected: protected:

View File

@ -1409,7 +1409,7 @@ COM_API_IMPLEMENT(scanner, int, get_scanned_images(DWORD milliseconds))
return count; return count;
} }
COM_API_IMPLEMENT(scanner, IScanImg*, take_first_image(void)) COM_API_IMPLEMENT(scanner, IScanImg*, take_first_image(twain_xfer xfer))
{ {
scanned_img* img = NULL; scanned_img* img = NULL;
SANE_Parameters head; SANE_Parameters head;
@ -1418,7 +1418,7 @@ COM_API_IMPLEMENT(scanner, IScanImg*, take_first_image(void))
{ {
wchar_t name[40] = { 0 }; wchar_t name[40] = { 0 };
swprintf_s(name, _countof(name) - 1, L"img_%05u.bmp", ++img_ind_); swprintf_s(name, _countof(name) - 1, L"img_%05u.bmp", ++img_ind_);
img = new scanned_img(head, handle_, dpi_, (tmp_path_ + name).c_str()); img = new scanned_img(head, handle_, dpi_, (tmp_path_ + name).c_str(), xfer);
} }
return dynamic_cast<IScanImg*>(img); return dynamic_cast<IScanImg*>(img);

View File

@ -172,7 +172,7 @@ public:
COM_API_OVERRIDE(void, set_event_callback(void(*cb)(int ev_type, void* data, unsigned int* len, void* param), void* param)); COM_API_OVERRIDE(void, set_event_callback(void(*cb)(int ev_type, void* data, unsigned int* len, void* param), void* param));
COM_API_OVERRIDE(bool, wait_image(DWORD milliseconds = -1)); COM_API_OVERRIDE(bool, wait_image(DWORD milliseconds = -1));
COM_API_OVERRIDE(int, get_scanned_images(DWORD milliseconds = 0)); COM_API_OVERRIDE(int, get_scanned_images(DWORD milliseconds = 0));
COM_API_OVERRIDE(IScanImg*, take_first_image(void)); // call 'release' on returned value, plz COM_API_OVERRIDE(IScanImg*, take_first_image(twain_xfer xfer = TWAIN_XFER_Native)); // call 'release' on returned value, plz
COM_API_OVERRIDE(bool, get_first_image_header(SANE_Parameters* header)); COM_API_OVERRIDE(bool, get_first_image_header(SANE_Parameters* header));
COM_API_OVERRIDE(bool, is_online(void)); COM_API_OVERRIDE(bool, is_online(void));
COM_API_OVERRIDE(bool, is_paper_on(void)); COM_API_OVERRIDE(bool, is_paper_on(void));

View File

@ -803,9 +803,9 @@ Result huagao_ds::pendingXfersEnd(const Identity& id, PendingXfers& data)
} }
Result huagao_ds::pendingXfersReset(const Identity&, PendingXfers& data) Result huagao_ds::pendingXfersReset(const Identity&, PendingXfers& data)
{ {
data.setCount(0); data.setCount(scanner_->get_scanned_images(-1));
if (scanner_.get()) //if (scanner_.get())
scanner_->stop(); // scanner_->stop();
//if (scanner.get()) //if (scanner.get())
//{ //{
@ -825,9 +825,10 @@ Result huagao_ds::setupMemXferGet(const Identity&, SetupMemXfer& data)
if (scanner_->get_first_image_header(&head)) if (scanner_->get_first_image_header(&head))
{ {
int line_bytes = (head.bytes_per_line + 3) / 4 * 4;
data.setMinSize(head.bytes_per_line); data.setMinSize(head.bytes_per_line);
data.setPreferredSize(head.bytes_per_line * head.lines); data.setPreferredSize(line_bytes * head.lines);
data.setMaxSize(head.bytes_per_line * head.lines); data.setMaxSize(line_bytes * head.lines);
return success(); return success();
} }
@ -925,7 +926,7 @@ Result huagao_ds::imageMemXferGet(const Identity& origin, ImageMemXfer& data)
if (!scanner_.get() || scanner_->get_scanned_images() == 0) if (!scanner_.get() || scanner_->get_scanned_images() == 0)
return seqError(); return seqError();
IScanImg *img = scanner_->take_first_image(); IScanImg *img = scanner_->take_first_image(TWAIN_XFER_Memory);
unsigned long long off = 0; unsigned long long off = 0;
unsigned int total = img->bytes(); unsigned int total = img->bytes();
unsigned char *src = img->data(off, &total), unsigned char *src = img->data(off, &total),
@ -958,7 +959,7 @@ Result huagao_ds::imageNativeXferGet(const Identity& id, ImageNativeXfer& data)
if (!scanner_.get() || scanner_->get_scanned_images() == 0) if (!scanner_.get() || scanner_->get_scanned_images() == 0)
return seqError(); return seqError();
IScanImg* img = scanner_->take_first_image(); IScanImg* img = scanner_->take_first_image(TWAIN_XFER_Native);
if (!img) if (!img)
return seqError(); return seqError();
@ -996,7 +997,7 @@ Twpp::Result huagao_ds::imageFileXferGet(const Twpp::Identity& origin)
if (!scanner_.get() || scanner_->get_scanned_images() == 0) if (!scanner_.get() || scanner_->get_scanned_images() == 0)
return seqError(); return seqError();
IScanImg *img = scanner_->take_first_image(); IScanImg *img = scanner_->take_first_image(TWAIN_XFER_File);
Twpp::Result ret = seqError(); Twpp::Result ret = seqError();
FILE* dst = NULL; FILE* dst = NULL;
@ -1614,7 +1615,7 @@ void huagao_ds::init_support_caps(void)
i = std::distance(vals.begin(), std::find(vals.begin(), vals.end(), Init)); i = std::distance(vals.begin(), std::find(vals.begin(), vals.end(), Init));
n = std::distance(vals.begin(), std::find(vals.begin(), vals.end(), Now)); n = std::distance(vals.begin(), std::find(vals.begin(), vals.end(), Now));
return cap_get_enum_values<ImageFileFormat, CapType::IRotation>(msg, data, vals, Now, Init, n, i); return cap_get_enum_values<ImageFileFormat, CapType::IImageFileFormat>(msg, data, vals, Now, Init, n, i);
}; };
m_query[CapType::IAutomaticDeskew] = msgSupportGetAllSetReset; m_query[CapType::IAutomaticDeskew] = msgSupportGetAllSetReset;