Salut, je code une application qui prend des screenshot de l ecran et les lit grace a tesseract. Pour avoir de meilleurs performances j'aimerais eviter d'avoir a enregistrer le screenshot dans un fichier avant de l envoyer a tesseract.

J'ai trouver un exemple sur le sujet et j'en suis arriver la:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#define _CRT_SECURE_NO_WARNINGS // for strncopy and fopen
 
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
 
 
//include leptonica headers
#include <allheaders.h>
 
//include tesseract headers
#include <baseapi.h>
 
using namespace std;
 
TCHAR CurDir[MAX_PATH];
tesseract::TessBaseAPI *api;
 
void init_tesseract_ocr();
PIX* tesseract_preprocess(PIX* pixs);
string tesseract_ocr(PIX* image);
PIX* ScreenShot(int left, int top, int sizex, int sizey);
 
int main()
{
    GetCurrentDirectory(MAX_PATH, CurDir);
    init_tesseract_ocr();
 
    PIX* sc = ScreenShot(0, 0, 500, 30);
    //PIX* pp = tesseract_preprocess(sc);
 
    //string text = tesseract_ocr(pp);
 
    //cout << text << endl;
 
    system("pause");
 
    return 0;
}
 
 
 
PIX* ScreenShot(int left, int top, int sizex, int sizey)
{
    // get the device context of the screen
    HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);
 
    // and a device context to put it in
    HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
 
 
    HBITMAP hBitmap;
    hBitmap = CreateCompatibleBitmap(hScreenDC, sizex, sizey);
 
    // get a new bitmap
    SelectObject(hMemoryDC, hBitmap);
 
    BitBlt(hMemoryDC, 0, 0, sizex, sizey, hScreenDC, left, top, SRCCOPY);
 
    BITMAP bmpScreen;
    GetObject(hBitmap, sizeof(BITMAP), &bmpScreen);
 
    BITMAPFILEHEADER   bmfHeader;
    BITMAPINFOHEADER   bi;
 
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bmpScreen.bmWidth;
    bi.biHeight = bmpScreen.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
 
    DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
 
    HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
    char *lpbitmap = (char *)GlobalLock(hDIB);
 
    GetDIBits(hScreenDC, hBitmap, 0,
        (UINT)bmpScreen.bmHeight,
        lpbitmap,
        (BITMAPINFO *)&bi, DIB_RGB_COLORS);
 
    // Add the size of the headers to the size of the bitmap to get the total file size
    DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
 
    //Offset to where the actual bitmap bits start.
    bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
 
    //Size of the file
    bmfHeader.bfSize = dwSizeofDIB;
 
    //bfType must always be BM for Bitmaps
    bmfHeader.bfType = 0x4D42; //BM  
 
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /*HANDLE hFile = CreateFile("UI.bmp",
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
 
    DWORD dwBytesWritten = 0;
    WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
 
    //Close the handle for the file that was created
    CloseHandle(hFile);*/
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
    std::vector<unsigned char> buffer(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmpSize);
    std::copy(reinterpret_cast<unsigned char*>(&bmfHeader), reinterpret_cast<unsigned char*>(&bmfHeader) + sizeof(BITMAPFILEHEADER), buffer.begin());
    std::copy(reinterpret_cast<unsigned char*>(&bi), reinterpret_cast<unsigned char*>(&bi) + sizeof(BITMAPINFOHEADER), buffer.begin() + sizeof(BITMAPFILEHEADER));
    std::copy(lpbitmap, lpbitmap + dwBmpSize, buffer.begin() + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
    PIX *mypix = pixReadMemBmp(&buffer[0], buffer.size());
 
    if (mypix == NULL)
    {
        cout << "mypix is NULL" << endl;
    }
    else
    {
        pixWriteImpliedFormat("preprocess.bmp", mypix, 0, 0);
    }
 
    DeleteDC(hMemoryDC);
    DeleteDC(hScreenDC);
    DeleteObject(hBitmap);
 
    GlobalUnlock(hDIB);
    GlobalFree(hDIB);
 
    /////////////////////////////////////
 
    return mypix;
}
Seulement "pixReadMemBm" retourne toujours un pointeur null.
Si je sauve le screenshot dans un fichier (avec la methode qui est commentee dans le code), le resultat est bon.
Le probleme vient donc de ces qq lignes:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
std::vector<unsigned char> buffer(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmpSize);
std::copy(reinterpret_cast<unsigned char*>(&bmfHeader), reinterpret_cast<unsigned char*>(&bmfHeader) + sizeof(BITMAPFILEHEADER), buffer.begin());
std::copy(reinterpret_cast<unsigned char*>(&bi), reinterpret_cast<unsigned char*>(&bi) + sizeof(BITMAPINFOHEADER), buffer.begin() + sizeof(BITMAPFILEHEADER));
std::copy(lpbitmap, lpbitmap + dwBmpSize, buffer.begin() + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
PIX *mypix = pixReadMemBmp(&buffer[0], buffer.size());
Quelqu'un pourrait m'expliquer ce qui est incorrecte ? Merci !