Implementation idea and code of saving window screenshots into files in Android Simulators

Source: Internet
Author: User
Tags bmp image

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 each framebuffer update.:

CopyCode The Code is as follows: 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, for example, after the display in the OpenGL window:Copy codeThe Code is as follows: android_showopengleswindow (winhandle, drect. Pos. X, drect. Pos. y,
Drect. Size. W, drect. Size. H, disp-> Rotation *-90 .);
Android_setpostcallback (zjin_fb_update, null );

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 framebuffer is RGB and BGR is used in BMP format. 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.