使用Qt渲染RGB数据

使用Qt渲染RGB数据_第1张图片

 

渲染 RGB 数据

TestRGB.h

#pragma once

#include 
#include "ui_TestRGB.h"

class TestRGB : public QWidget
{
    Q_OBJECT

public:
    TestRGB(QWidget *parent = Q_NULLPTR);
    void paintEvent(QPaintEvent* event) override;  // 绘制图形的函数

private:
    Ui::TestRGBClass ui;
};

TestRGB.cpp

#include "TestRGB.h"
#include 
#include 

static const int w = 1080;
static const int h = 720;

TestRGB::TestRGB(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    resize(w, h);
}

void TestRGB::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    QImage image(w, h, QImage::Format::Format_RGB888);  // RGB888 每个像素点的每个颜色使用8位表示,一个像素点占用3个字节
    unsigned char* data = image.bits();  // 图像所使用的内存

    painter.begin(this);

    /* 改变每个像素点的颜色 */
    for (int i = 0; i < h; i++)
    {
        int begin = i * w * 3;  // 每行的开始处

        for (int j = 0; j < w * 3; j += 3)
        {
            data[begin + j] = 255;    // R
            data[begin + j + 1] = 0;  // G
            data[begin + j + 2] = 0;  // B
        }
    }

    painter.drawImage(0, 0, image);

    painter.end();
}

我们将 1024 * 720 的图像中的每个像素点渲染成红色。

程序运行结果如下所示:

使用Qt渲染RGB数据_第2张图片

 

你可能感兴趣的:(音视频,音视频)