根据uchar *data构建QImage保存后图片损坏的问题,bytesPerLine很重要

构造QImage的时候
方法一:
**QImage::QImage(const uchar data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void cleanupInfo = Q_NULLPTR)
Constructs an image with the given width, height and format, that uses an existing read-only memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.
Unlike the similar QImage constructor that takes a non-const data buffer, this version will never alter the contents of the buffer. For example, calling QImage::bits() will return a deep copy of the image, rather than the buffer passed to the constructor. This allows for the efficiency of constructing a QImage from raw data, without the possibility of the raw data being changed.

构造具有给定宽度,高度和格式的图像,该图像使用现有的只读内存缓冲区数据。宽度和高度必须以像素为单位指定,数据必须是32位对齐的,并且图像中数据的每条扫描线也必须是32位对齐的。
缓冲区在QImage的整个生命周期内必须保持有效,并且所有未经修改或以其他方式与原始缓冲区分离的副本都必须保持有效。映像不会删除销毁的缓冲区。您可以提供一个函数指针cleanupFunction以及一个额外的指针cleanupInfo,该指针将在销毁最后一个副本时被调用。
如果format是索引颜色格式,则图像颜色表最初为空,必须在使用图像之前使用setColorCount()或setColorTable()进行充分扩展。
与采用非常量数据缓冲区的类似QImage构造函数不同,此版本永远不会更改缓冲区的内容。例如,调用QImage :: bits()将返回图像的深层副本,而不是传递给构造函数的缓冲区。这允许从原始数据构造QImage的效率,而不必更改原始数据。

方法二:
**QImage::QImage(uchar data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void cleanupInfo = Q_NULLPTR)
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.
构造具有给定宽度,高度和格式的图像,该图像使用现有的内存缓冲区数据。 宽度和高度必须以像素为单位指定。 bytesPerLine指定每行的字节数(跨度)。
缓冲区在QImage的整个生命周期内必须保持有效,并且所有未经修改或以其他方式与原始缓冲区分离的副本都必须保持有效。 映像不会删除销毁的缓冲区。 您可以提供一个函数指针cleanupFunction以及一个额外的指针cleanupInfo,该指针将在销毁最后一个副本时被调用。
如果format是索引颜色格式,则图像颜色表最初为空,必须在使用图像之前使用setColorCount()或setColorTable()进行充分扩展。

一开始我用方法一来生成图像,结果生成的qimage通过方法QImage::save方法保存之后的图片是损坏的,无法打开
之后我用方法而来生成图像,也就是规定了bytesPerLine(每一行的字节数),这样生成的qimage就正常了,也能正常保存。

猜你喜欢

转载自blog.csdn.net/weixin_43935474/article/details/113117853