use*_*714 2 c++ dib hbitmap leptonica
好吧,整个故事是,我试图在C++中使用Leptonica + Tesseract OCR截取屏幕截图,将其保存为*.bmp文件,然后将其加载回OCR.我不需要经常这样做,但由于我似乎无法将屏幕截图数据直接复制到Leptonica PIX结构中,我需要先将其保存到文件中......实际上,最好是解决这个问题.
这是我在网上找到的一些代码,试图帮助我.
屏幕上限:
HBITMAP ScreenCapture(){
int width=100;
int height=100;
// get the device context of the screen
HDC hScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
// and a device context to put it in
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
int x = GetDeviceCaps(hScreenDC, HORZRES);
int y = GetDeviceCaps(hScreenDC, VERTRES);
// maybe worth checking these are positive values
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
// get a new bitmap
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
//GlobalAlloc(GPTR, hBitmap)
WriteDIB(L"test.bmp", (HGLOBAL)hBitmap);
// clean up
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
return hBitmap;
// now your image is held in hBitmap. You can save it or do whatever with it
}
Run Code Online (Sandbox Code Playgroud)
尝试写功能:
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
cout<<endl<<"Running save function";
/*HANDLE hDIB=GlobalAlloc(GPTR, sizeof(hDIBtochange));//this doesn't work, the result is four. Also the HANDLE parameter's name would be changed to hDIBtochange, so that the rest of the function uses the old 'hDIB' throughout
cout<<endl<<sizeof(hDIBtochange);*/
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = 1 << lpbi->biBitCount;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize + nColors * sizeof(RGBQUAD));
// Write the file header
file.Write( &hdr, sizeof(hdr) );
// Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
多年来从人们的帖子中无耻地复制.好!我面临的问题是,我似乎无法理解如何将GlobalAlloc Hbitmap转换为全局可访问的Handle,可以转换或与LPBITMAPINFOHEADER一起使用.创建lpbi后,其中的每个字段都在Visual Studio 2012调试中"无法读取内存"错误.尽管被创造出来,它仍然无法进入.
解决方案..直接从screencap到PIX,内存.找到一种方法来保存到位图并定期创建它们来阅读..完全找到另一种方式更有意义..
首选,但是,我要求解决这个,第二个......或者第三个.
如果您需要更多信息,我可以尝试提供它.这主要归结为"我之前从未做过这样的代码,而且我的课程中没有教过它,所以我正努力学习".
将HBITMAP保存到文件的更简单方法是使用GDI +.这使您能够保存到Windows本身支持的任何格式,同时让您免受各种图像格式的困扰,甚至需要理解.
在下面的例子中,我刚刚使用LoadImage作为加载预先存在的图像的quik和脏方法 - 你可以简单地使用你已经捕获的HBITMAP.
这是一个加载位图并再次保存的示例.(我最初使用"image/png"作为输出类型,以及适当的输出文件名)
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
HBITMAP hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), "babe.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
Bitmap *image = new Bitmap(hBitmap, NULL);
CLSID myClsId;
int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
image->Save(L"output.bmp", &myClsId, NULL);
delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}
Run Code Online (Sandbox Code Playgroud)