Address: http://blog.csdn.net/ariesjzj/article/details/8528212
The content of the android simulator is rendered using OpenGL, so the normal programming (such as printwindow () will be a black screen. This is because the painting is placed in framebuffer.
One way is to use ADB to reverse the guest framebuffer data/dev/graphics/fb0 to the host and convert it into an image. However, this process is slow.
Fortunately, in the android simulator, the guest framebuffer is uploaded to the host for display. Therefore, you only need to output the framebuffer to the file on the host side.
First, define the callback function for every framebuffer update:
void zjin_fb_update(void* context, int w, int h, int ydir, int format, int type, unsigned char* pixels){#define CHANNEL 4 BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; int width = w; int height = h; FILE *file = fopen("capture.bmp", "wb"); if( file!=NULL ) { memset( &bf, 0, sizeof( bf ) ); memset( &bi, 0, sizeof( bi ) ); bf.bfType = 'MB';//BM? bf.bfSize = sizeof(bf)+sizeof(bi)+width*height*CHANNEL; bf.bfOffBits = sizeof(bf)+sizeof(bi); bi.biSize = sizeof(bi); bi.biWidth = width; bi.biHeight = height; bi.biPlanes = 1; bi.biBitCount = 8 * CHANNEL; bi.biSizeImage = width*height*CHANNEL; fwrite( &bf, sizeof(bf), 1, file ); fwrite( &bi, sizeof(bi), 1, file ); fwrite( pixels, sizeof(unsigned char), height*width*CHANNEL, file ); fclose( file ); } return;}
Then register the callback function in the source code of emulator. For example, after the display in the OpenGL window:
android_showOpenglesWindow(winhandle, drect.pos.x, drect.pos.y, drect.size.w, drect.size.h, disp->rotation * -90.); android_setPostCallback(zjin_fb_update, NULL);
Recompile emulator. In this way, every time there is a framebuffer update, the guest screen will be saved as a BMP image, which is the same as/dev/graphics/fb0.
Note that there are two differences between the captured image and the source image using the above method. One is the blue and red channels interchange, because the framebuffer is RGB and the BMP format is BGR. In addition, the zero point of the Y axis is in the lower left corner, because the coordinate system of OpenGL is in framebuffer. That is to say, the original image must be converted from RGB to BGR and Y-inversion. We recommend that you do the processing before processing images. On the one hand, it will not slow down the simulator speed, and on the other hand, there are ready-made functions in opencv for calling.