如何将原始 RGB 帧缓冲区文件转换为可查看格式



我知道这个问题以前有人问过。原始线程在这里如何将 16 位 RGB 帧缓冲区转换为可查看的格式?但我没有得到我的愿望输出。

目前我正在处理帧缓冲区。确切的图片是我正在访问安卓手机的帧缓冲区(/dev/graphics/fb0)(通过"adb"外壳)。我使用"dd"来获取帧缓冲区。

cd /dev/graphics
dd if=fb0 of=/sdcard/fb0.raw bs=1 count=width*height*3 //this width and height are based on mobile screen specification. And 3 is for RGB888

我使用了这段代码——

#include <stdio.h>
#include <stdlib.h>
#include <bmpfile.h>
int main(int argc, char **argv)
{
bmpfile_t *bmp;
int i, j;
char* infilename;
FILE* infile;
char* outfile;
int width;
int height;
int depth;
unsigned char red, green, blue; // 8-bits each
unsigned short pixel; // 16-bits per pixel
//rgb_pixel_t bpixel = {128, 64, 0, 0};
//make && ./raw565tobmp fb.rgb565 720 480 32 fb.bmp && gnome-open fb.bmp
if (argc < 6) {
printf("Usage: %s infile width height depth outfile.n", argv[0]);
exit(EXIT_FAILURE);
}
infilename = argv[1];
outfile = argv[5];
infile = fopen(infilename, "rb");
if (NULL == infile) {
perror("Couldn't read infile");
exit(EXIT_FAILURE);
}
width = atoi(argv[2]);
height = atoi(argv[3]);
depth = atoi(argv[4]);
// should be depth/8 at 16-bit depth, but 32-bit depth works better
short buffer[height*width*(depth/16)];
printf("depth: %d", depth);
if (fread(&buffer, 1, height*width*(depth/16), infile) != height*width*(depth/16)) {
 fputs("infile dimensions don't match the size you suppliedn", stderr);
}
printf("depth: %d", depth);
if ((bmp = bmp_create(width, height, depth)) == NULL) {
printf("Invalid depth value: '%d'. Try 1, 4, 8, 16, 24, or 32.n", depth);
exit(EXIT_FAILURE);
}
for (i = 0; i < width; ++i) { // 720
 for (j = 0; j < height; ++j ) { // 480
  pixel = buffer[width*j+i];
  red = (unsigned short)((pixel & 0xFF0000) >> 16);  // 8
  green = (unsigned short)((pixel & 0x00FF00) >> 8); // 8
  blue = (unsigned short)(pixel & 0x0000FF);         // 8
  rgb_pixel_t bpixel = {blue, green, red, 0};
  bmp_set_pixel(bmp, i, j, bpixel);
  }
}
bmp_save(bmp, outfile);
bmp_destroy(bmp);
return 0;
}

该程序的输入 - ./a.out fb0.raw 480 854 24/data/new.bmp

argv[1]=输入文件

argv[2]=宽度

argv[3]=高度

argv[4]=深度

argv[5]=输出文件

最初这里给出的代码如何将 16 位 RGB 帧缓冲区转换为可查看的格式?

现在打开 bmp 文件后,我得到的一切都是黑色的。为什么会这样?

以及如何显示帧缓冲区?

为了不重新发明轮子,我尝试重用现有工具:

  1. 获取帧缓冲:

    adb pull /dev/graphics/fb0 androidFB
    
  2. 查找我们的帧缓冲文件中使用的格式。我为此目的使用实用程序avconv(但您也可以使用ffmpeg),与自制脚本相关联:

    #!/bin/bash
    #Change FB_RESOLUTION value by the android device's resolution
    FB_RESOLUTION=240x320
    OUTPUT_DIR=fbresult
    #format come from: avconv -pix_fmts| cut -f 2 -d " "
    format=(yuv420p yuyv422 rgb24 bgr24 yuv422p yuv444p yuv410p yuv411p gray monow monob pal8 yuvj420p yuvj422p yuvj444p xvmcmc xvmcidct uyvy422 uyyvyy411 bgr8 bgr4 bgr4_byte rgb8 rgb4 rgb4_byte nv12 nv21 argb rgba abgr bgra gray16be gray16le yuv440p yuvj440p yuva420p vdpau_h264 vdpau_mpeg1 vdpau_mpeg2 vdpau_wmv3 vdpau_vc1 rgb48be rgb48le rgb565be rgb565le rgb555be rgb555le bgr565be bgr565le bgr555be bgr555le vaapi_moco vaapi_idct vaapi_vld yuv420p16le yuv420p16be yuv422p16le yuv422p16be yuv444p16le yuv444p16be vdpau_mpeg4 dxva2_vld rgb444le rgb444be bgr444le bgr444be y400a bgr48be bgr48le yuv420p9be yuv420p9le yuv420p10be yuv420p10le yuv422p10be yuv422p10le yuv444p9be yuv444p9le yuv444p10be yuv444p10le yuv422p9be yuv422p9le vda_vld gbrp gbrp9be gbrp9le gbrp10be gbrp10le gbrp16be gbrp16le)
    
    mkdir $OUTPUT_DIR
    for item in ${format[*]}
    do
        #Loop through operation   
        avconv -vframes 1 -f rawvideo -pix_fmt $item -s $FB_RESOLUTION -i androidFB $OUTPUT_DIR/$item.png
    done
    
  3. 浏览OUTPUT_DIR目录以查找具有正确外观的图像。文件的名称是要使用的格式:XXXX.png要使用的格式将是XXXX

  4. 您现在可以使用以下命令转换 PNG 格式的帧缓冲(不要忘记替换分辨率FB_RESOLUTION并按正确的值格式化 XXXX)

    avconv -vframes 1 -f rawvideo -pix_fmt XXXX -s FB_RESOLUTION -i androidFB androidFB.png
    

我一直在尝试捕获Android 2.3.4手机屏幕图像,但遇到了同样的问题。 首先,我尝试使用一些工具,但我使一切复杂化。

Joe 给了我有关填充的线索,所以我只是尝试读取文件中的原始帧缓冲,以使用 GIMP 加载它(目前我对为此制作软件不感兴趣,也许以后)。

使用 GIMP 原始导入参数,我发现填充时 240x320 屏幕的宽度实际上是 256x320,右侧有透明填充。 此外,它不是RGB,而是"RGB Alpha"原始类型。

由于我正在编写的是一本手册,因此以下内容与 GIMP 一起足以在旧的 Android 版本上工作:

adb shell "cat/dev/graphics/fb0>/mnt/sdcard/documents/devimages/$1.data"adb pull/mnt/sdcard/documents/devimages/$1.dataadb shell "rm/mnt/sdcard/documents/devimages/$1.data"

我希望这对某人有所帮助:-)

为了获得很多便利性和功能性,您可以使用适用于Android的OpenCV端口。 那么这将是最简单的事情之一。我认为这不会有太大问题,因为您在项目中使用 natice 代码。

如果我没记错的话,该函数名为 imwrite() 并位于 HighGui.hpp

你可以在这里找到OpenCV的android端口:http://opencv.org/downloads.html

相关的"黑度"问题:我在处理pgm图像时遇到了这个问题。PGM 标头必须指定图像的"最大"深度。例如 255,8 位。假设您有 8 位数据,但最大分辨率为 16 位,65565 个值。图片将是"黑色"的,因为所有值都很低。我希望你明白我的意思。

  rgb_pixel_t bpixel = {blue, green, red, 0};

可能将 alpha 值设置为 0,而 alpha 是透明度值。尝试 255 而不是看看你是否得到任何结果。如果你的像素方程式弄错了,你至少应该得到一些东西。

图像的另一个"陷阱"是有时数据不会存储为 RGBRGBRGB......但BGRBGRBGR就像在Microsoft/IBM DIB中一样。这是一个简单的检查,因为你会得到图像,但它就像你在LSD上一样。

height*width*(depth/16)

也应该看。我还建议将其放入变量中一次,以便您更容易维护。

另外,关于 RAW DIB 需要注意的一件事是:有时它们每行都有固定的填充。在 MS/IBM DIB 中,要求每条扫描线必须以 4 字节为增量存储。因此,如果您的输出经常向一个方向移动,则可能是该方向。在某些情况下,图像是倒置存储的

最新更新