Programmed to save window screenshots to files in the Android Simulator

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 every framebuffer update: [cpp] 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;} 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: [cpp] android_showOpenglesWindow (winhandle, drect. pos. x, drect. pos. y, drect. size. w, drect. size. h, disp-> rotation *-90 .); android_setPostCallback (zjin_fb_update, NULL); 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, the gues T screen will be saved into 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.