问题
Better way to do it (without QImage
)?:
QImage image(width, height, QImage::Format_RGB888);
memcpy(image.bits(), m_frameRGB->data[0], height * width * 3);
QPixmap pixmap = QPixmap::fromImage(image);
I don't see any reason to use QImage
as intermediate buffer, but QPixmap::loadFromData
don't load data with this context:
pixmap.loadFromData(m_frameRGB->data[0], height * width * 3); // Need pixmap resize?
回答1:
The documentation says: "If the format is not specified (which is the default), the loader probes the file for a header to guess the file format". You only provide the pointer to the raw image bytes, but you have to provide a header at the start of the buffer, e.g. for an uncompressed PPM format.
Edit: You could also test the suggestion of Roku to use the QImage constructor which takes the image data as a parameter, but see the remark in the documentation: "The buffer must remain valid throughout the life of the QImage."
回答2:
followed hmuelner hint and ... it's really little faster than QPixmap pixmap = QPixmap::fromImage(image);
QPixmap pixmap(height, width);
QByteArray pixData;
pixData.append(QString("P6 %1 %2 255 ").arg(width).arg(height));
pixData.append((char *)m_frameRGB->data[0], width * height * 3);
pixmap.loadFromData((uchar *)pixData.data(), pixData.size());
来源:https://stackoverflow.com/questions/11210884/better-way-to-load-qpixmap-data