好的,整个故事是这样的,我正在尝试在C++中使用Leptonica + Tesseract OCR来截取屏幕截图,将其保存为*.bmp文件,然后再加载它以进行OCR。我不需要经常这样做,但由于我似乎无法直接将屏幕截图数据复制到Leptonica PIX结构中,因此我需要先将其保存到一个文件中..实际上,找到这个问题的解决方案会更好一些。
以下是我在网上找到的一些代码,希望能帮助我解决问题。
屏幕捕获:
尝试编写函数:
无耻地抄袭别人多年来的文章。好吧!我面临的问题是,我似乎无法理解如何将Hbitmap GlobalAlloc到全局可访问的句柄中,该句柄可以转换或与LPBITMAPINFOHEADER一起使用。一旦创建了lpbi,它内部的每个字段都会在Visual Studio 2012调试中出现“无法读取内存”的错误。尽管被创建了,但它是不可访问的。
解决方案.. 直接从屏幕截图到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
}
尝试编写函数:
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;
}
无耻地抄袭别人多年来的文章。好吧!我面临的问题是,我似乎无法理解如何将Hbitmap GlobalAlloc到全局可访问的句柄中,该句柄可以转换或与LPBITMAPINFOHEADER一起使用。一旦创建了lpbi,它内部的每个字段都会在Visual Studio 2012调试中出现“无法读取内存”的错误。尽管被创建了,但它是不可访问的。
解决方案.. 直接从屏幕截图到PIX,在内存中进行。 找到一种保存为位图并定期创建以读取的方法。 找到另一种更合理的方式。
首选第一个,但我请求在第二个或第三个解决方案中提供一个解决方案。
如果需要更多信息,我可以尝试提供。这主要归结为“我以前从未编写过此类代码,并且在我的课程中没有教授过,所以我正在学习”。