【问题标题】:Saving a QImage to QBuffer将 QImage 保存到 QBuffer
【发布时间】:2015-07-10 15:20:39
【问题描述】:

我正在做这样的事情:

QImage image(width, height, QImage::Format_RGB32);
frame.fill(QColor(255, 255, 255).rgb());

QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);

选项 1:

out << image;

选项 2:

out.writeRawData((char *) image.constBits(), image.byteCount()) ;

选项 1 很慢,我不确定选项 2 是否正确?

【问题讨论】:

    标签: c++ qt qt4 qimage qdatastream


    【解决方案1】:

    您可以使用QImage::save 直接写入QIODevice,无论是缓冲区还是文件。

    image.save(buffer);
    

    与选项 1 相比,选项 2 看起来很糟糕;从美学上讲,我当然更喜欢选项 1。但我更喜欢我提到的 API,而不是你提供的两个选项。

    您可以阅读更多关于图像读/写的信息here

    【讨论】:

      【解决方案2】:

      我刚刚做了类似的事情

      QByteArray byteArray;
      QBuffer buffer(&byteArray);
      image.save(&buffer, "PNG"); // writes the image in PNG format inside the buffer
      

      要输出缓冲区,我会使用 QString,我用它来转换为 base64

      QString imgBase64 = QString::fromLatin1(byteArray.toBase64().data()); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-19
        • 1970-01-01
        相关资源
        最近更新 更多