diff --git a/sdk/oldwebscan/demo/C++TestDemo/main.cpp b/sdk/oldwebscan/demo/C++TestDemo/main.cpp index 51dfbc48..0dfdb4c5 100644 --- a/sdk/oldwebscan/demo/C++TestDemo/main.cpp +++ b/sdk/oldwebscan/demo/C++TestDemo/main.cpp @@ -180,6 +180,12 @@ void on_aquireimage(HG_IMG *img, void *obj) imagedata.insert(imagedata.end(), img->pimgdata, img->pimgdata + img->bufferlength); writeimage(savepath, imagedata); + //bool b = G4TiffSave((char *)savepath.c_str()); + //if (!b) + //{ + // printf("Tiff G4 Conversion Fail \r\n"); + // return; + //} printf("--------------------savepath:%s-------------------------------------------\r\n", savepath.c_str()); /* ofstream o(savepath); diff --git a/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.cpp b/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.cpp index 3a1fe93b..25a960ec 100644 --- a/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.cpp +++ b/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.cpp @@ -12,25 +12,38 @@ #include #include #include "assert.h" +#include +#include using namespace std; HG_SCANPARAMS m_params; std::string img_type_ = ".jpg"; void* m_obj_usb = nullptr; void* m_obj_img = nullptr; -static const char* SDKVersion = "1.22.9.30"; +static const char* SDKVersion = "4.23.02.23"; char deviceList[60]; unsigned int compression_data = 80; HG_OnDevice_Event_Callback m_callback_OnDevcie_Event = nullptr; HG_OnImage_Callback m_callback_OnImage_Event = nullptr; unsigned int m_scannum = 0; + +void G4TiffExcept(bool condition, const std::string& message); +void* allocate_memory(size_t bytes, const char* log_msg) +{ + bytes += 7; + bytes /= 8; + bytes *= 8; + + return new char[bytes]; +} + void callback_USBEvent(int conditioncode, void* obj) { if (m_callback_OnDevcie_Event) m_callback_OnDevcie_Event((HG_STATUSCODE)conditioncode, m_obj_usb); } //有图事件回调 -static int indeximg = 10; +static int indeximg = 0; static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param) { if (image == NULL) @@ -948,3 +961,103 @@ DLL_HG_SCHANNER_API int HG_Create_MultiTiff(char** srcFiles, int srcnum, char* d printf("\nHGImgFmt_CloseTiffWriter %d", hgret); return 1; } + + +void G4TiffExcept(bool condition, const std::string& message) +{ + if (!condition) + throw std::runtime_error("Error " + message); +} + +DLL_HG_SCHANNER_API bool G4TiffSave(char* m_tmppath) +{ + if (!m_tmppath) + { + printf("filename is not find\r\n"); + return false; + } + cv::Mat mat = cv::imread(m_tmppath); + if (mat.empty()) + { + printf("opencv mat read image fial\r\n"); + return false; + } + if (mat.channels() == 3) + { + cvtColor(mat, mat, cv::COLOR_RGB2GRAY); + } + int compression = COMPRESSION_CCITT_T6; + + if (compression == COMPRESSION_CCITT_T6 && mat.channels() != 1) + throw std::runtime_error("mat channel error"); + + TIFF* pTiffHandle = TIFFOpen(m_tmppath, "w"); + if (!pTiffHandle) + { + printf("can't open TIFF descriptor\n"); + return false; + } + int width = mat.cols; + int height = mat.rows; + try + { + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width), "width"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height), "length"); + if (compression == COMPRESSION_CCITT_T6) + { + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, 1), "bits per sample"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, 1), "samples per pixel"); + } + else + { + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, /*mat.depth()*/8), "bits per sample"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, mat.channels()), "samples per pixel"); + } + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, height), "rows per strip"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression), "compression"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE), "photometric"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB), "photometric"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG), "planar config"); + // not necessary + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_XRESOLUTION, (float)m_params.Resolution), "res x"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_YRESOLUTION, (float)m_params.Resolution), "res y"); + G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH), "res unit"); + + if (compression == COMPRESSION_CCITT_T6) + { + std::vector _buffer(width / 8 + 8, 0); + //std::vector _buffer(width / 8 + 1, 0); + uchar* buffer = &_buffer[0]; + //int bytes = int(width / 8.0 + 0.5); + for (int y = 0; y < height; ++y) + { + uint8_t* src_row = mat.ptr(y); + for (int x = 0; x < width; ++x, ++src_row) + { + uint8_t eight_pixels = buffer[x / 8]; + eight_pixels = eight_pixels << 1; + if (*src_row < 127) + eight_pixels = eight_pixels | 1; // + buffer[x / 8] = eight_pixels; + } + G4TiffExcept(TIFFWriteScanline(pTiffHandle, buffer, y, 0) != -1, "write scanline"); + } + } + else + { + int l = mat.total() / mat.rows * mat.channels(); + for (int y = 0; y < height; ++y) + { + uint8_t* src = mat.ptr(y); + G4TiffExcept(TIFFWriteScanline(pTiffHandle, src, y, 0) != -1, "write scanline"); + } + } + } + catch (const std::runtime_error& e) + { + printf("TIFF writing: %s\n", e.what()); + // TIFFClose(pTiffHandle); + } + TIFFClose(pTiffHandle); + return true; +} diff --git a/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.h b/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.h index 3a764524..720dcdd8 100644 --- a/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.h +++ b/sdk/oldwebscan/hgscannersdk/HG_SCANNER_API.h @@ -448,6 +448,13 @@ extern "C" { /// /// 成功:true 失败:false DLL_HG_SCHANNER_API bool HG_Set_Img_Quality(HG_Scanner_Handle pScanner, bool compressiondata = true); + /// + /// 转换tiff G4无压缩图像 支持24位图和8位图 注意:此方法会随损失出图的效率 + /// m_tmppath:图像路径 + /// + /// 成功:true 失败:false + DLL_HG_SCHANNER_API bool G4TiffSave(char* m_tmppath); + #ifdef __cplusplus } #endif diff --git a/sdk/oldwebscan/lib/hgscannersdk.lib b/sdk/oldwebscan/lib/hgscannersdk.lib index 1b11efee..270f9935 100644 Binary files a/sdk/oldwebscan/lib/hgscannersdk.lib and b/sdk/oldwebscan/lib/hgscannersdk.lib differ diff --git a/sdk/oldwebscan/sln/hgscannersdk/hgscannersdk.vcxproj b/sdk/oldwebscan/sln/hgscannersdk/hgscannersdk.vcxproj index 5e0f867d..7b061797 100644 --- a/sdk/oldwebscan/sln/hgscannersdk/hgscannersdk.vcxproj +++ b/sdk/oldwebscan/sln/hgscannersdk/hgscannersdk.vcxproj @@ -66,7 +66,7 @@ true $(SolutionDir)x86\$(Configuration)\ - ..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;$(IncludePath) + ..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;..\..\..\..\..\code_device\hgdriver\3rdparty\tiff\include $(SolutionDir)x86\$(Configuration)\ $(ProjectDir)..\..\..\..\..\sdk\lib\win\$(PlatformTarget)\$(Configuration);$(ProjectDir)..\..\..\..\build\windows\Debug;$(LibraryPath) @@ -74,7 +74,7 @@ true $(SolutionDir)x86\$(Configuration)\ $(SolutionDir)x86\$(Configuration)\ - ..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;$(IncludePath) + ..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;..\..\..\..\..\code_device\hgdriver\3rdparty\tiff\include $(ProjectDir)..\..\..\..\..\sdk\lib\win\$(PlatformTarget)\$(Configuration);$(ProjectDir)..\..\..\..\build\windows\Release;$(LibraryPath)