Tesseract OCR: How can I get text directly from a window handle without first saving a file

StackOverflow https://stackoverflow.com/questions/21268555

  •  30-09-2022
  •  | 
  •  

Question

here is what I am ABLE to do:

Use the CaptureAnImage function example from msdn in order to save an image from a window handle to my computer, then use tesseract with this code:

STRING text_out;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
    fprintf(stderr, "Could not initialize tesseract.\n");
    exit(1);
}
Pix *image = pixRead("captureqwsx.bmp");
api->SetImage(image);
api->SetRectangle(830, 95, 85, 25);
text_out = api->GetUTF8Text();
printf("OCR output:\n%s", text_out.string());

In order to retrieve the text from the window handle specified. And it works. Problem is: I feel like saving an image to my computer is not only useless, but probably not really good for the performance of my application...

The solution I found is, use SetImage with it's second definition:

  void SetImage(const unsigned char* imagedata, int width, int height,
                int bytes_per_pixel, int bytes_per_line);

And here is how I hope you guys can help me... I have no idea how to retrieve the image data, bytes per pixel, and bytes per line of a window... I feel like the original CaptureAnImage function should be able to do that easily with just little bit of modifications, but I don't know how and my searches returned nothing good...

Was it helpful?

Solution

CaptureAnImage has this line

DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;

which contains all the parameters you need.

bmWidth and bmHeight are the width and height. The number of bits per pixel is bi.biBitCount which is set to 32, then there are 32/8 = 4 bytes per pixel. The expression multiplied by bmHeight is the number of bytes per scan line, but because we know the color depth is 32 bits, it is just bmWidth * 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top