Programmed to save window screenshots to files in the android Simulator

Source: Internet
Author: User
Tags bmp image

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.