简体   繁体   English

将帧缓冲区转储转换为图像(bmp、png 等)

[英]Convert framebuffer dump to image(bmp, png, etc.)

I have a server where I am dumping the framebuffer data using cat /dev/fb0 > fbdump.raw and sending this file's contents to web client to display as a screenshot.我有一个服务器,我在其中使用cat /dev/fb0 > fbdump.raw转储帧缓冲区数据,并将此文件的内容发送到 web 客户端以显示为屏幕截图。 Tried quite a few links before posting the question, but none helped in rendering an image on HTML/JS client side.在发布问题之前尝试了很多链接,但没有一个有助于在 HTML/JS 客户端呈现图像。

Is there any processing required on client side or is there any readymade API available for this in JavaScript?客户端是否需要任何处理,或者 JavaScript 中是否有现成的 API 可用? Any suggestions are appreciated.任何建议表示赞赏。 Thanks in advance.提前致谢。

Referred Links:参考链接:

You can put ImageData (raw RGBA image data stored in a Uint8ClampedArray ) on a canvas.您可以将ImageData (存储在Uint8ClampedArray中的原始 RGBA 图像数据)放在 canvas 上。

You have to convert the framebuffer dump (BGRA) to RGBA by swapping red and blue.您必须通过交换红色和蓝色将帧缓冲区转储 (BGRA) 转换为 RGBA。 Setting the alpha to 255 (opaque) is nececary as well, because the linux framebuffer doesn't use the alpha channel so it is usually 0 (transparent).将 alpha 设置为 255(不透明)也是必要的,因为 linux 帧缓冲区不使用 alpha 通道,因此通常为 0(透明)。

fetch("fbdump.raw") // load the dump
.then(response => response.arrayBuffer()) // convert the response to a ArraBuffer
.then(array_buffer => {
    let canvas = document.querySelector("canvas") // get the canvas
    let ctx = canvas.getContext("2d") // get the context
    let raw_data = new Uint8ClampedArray(array_buffer) // use the ArrayBuffer as a Uint8ClampedArray for easier acces and the constructor of ImageData needs a Uint8ClampedArray
    for (let i = 0; i < raw_data.length; i += 4) {
        // convert the BGRA dump to RGBA
        let b = raw_data[i + 0]
        raw_data[i + 0] = raw_data[i + 2]
        raw_data[i + 2] = b
        raw_data[i + 3] = 255 // set alpha to 255 to make it non transparent (the alpha ist set to 0 and is unused in the linux framebuffer)
    }

    let image_data = new ImageData(raw_data, 1920, 1080) // create a new ImageData object with a resolution of 1920x1080

    // set the canvas resolution to 1920x1080
    canvas.width = 1920
    canvas.height = 1080

    ctx.putImageData(image_data, 0, 0) // puts the image on the canvas, starting at (0,0)
})

This code assumes the framebuffer has a resolution of 1920x1080 and the BGRA pixel format.此代码假定帧缓冲区具有 1920x1080 的分辨率和 BGRA 像素格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM