QImage
QImage 是用于读写绘制图像的基本类库,显示图像,我们借助于QImage,重点是如何通过图像二维数组(或三维数组)构造一个QImage对象。
以下来至于QImage构造函数QImage::QImage(uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr) http://doc.qt.io/qt-5/qimage.html#QImage-3
QImage::QImage(uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = 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, 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.
从构造函数中可以看出几个需要注意的地方:
- uchar* data 的对象需要保持持久性,即QImage图像并未深度拷贝data中的数据,仅仅只是一个指针应用。因此,构建QImage之后的,data不能delete,否则会出错。
- QImage::Format 在此处我们制定RGB888的格式,即图像为24位,RGB各占8位。
- data中的数据是如何排列的?简而言之,data中的数据是按BIP方式排列的,但有个要求,就是每一行的图像数据,要按4字节对齐。(重点在此)
QImage CRSImage::toQImage() { //1. 为QImage准备一个Buffer,而且要求具有持久性,故而最佳的办法就是定义为CRSImage的成员变量 //unsigned char* m_pDataBuff; //2. 图像Buffer的大小 int iImageHeight = m_lLines; //图像的宽度 // 此处略有点难以理解,这是因为QImage的图像数据是按BIP方式排列的,但有个要求,就是在每一行数据排列完毕之后,数据是否为4字节的整数倍。m_lSamples是图像的宽度,*24代表,RGBRGBRGB...这样排列下来,一行图像的bit数。此时,我们要判断24*m_lSamples是否为4字节(32bit)的整数倍,比如:24*m_lSamples 为72,就要变为96. int iByteLines = (m_lSamples*24+31)/32*4; //图像每一行的字节数 // m_pDataBuff 的大小为 [m_lLines*iByteLines] //3. 准备QImage的Buffer int i, j, m=0, n; for (i=0; i<m_lLines; i++) //行遍历 { for (j=0; j<m_lSamples; j++) //列遍历 { m_pDataBuff[m++] = m_pppData[Red][i][j]; //R波段 m_pDataBuff[m++] = m_pppData[Grn][i][j]; //G波段 m_pDataBuff[m++] = m_pppData[Blu][i][j]; //B波段 } // 1行数据填充完毕之后,我们要检查m是否为4字节的整数倍,可以写为: while(k%4) {k++;} //还有一种写法 while(k%iByteLines) {k++;} } // 构造一个对象,返回 return QImage(m_pDataBuff, m_lSamples, m_lLines, QImage::Format_RGB888); }
来自:https://github.com/cugwhp/OOPCPP/issues/10
##############################
QQ 3087438119
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2021-12-23 IfcDistributionCircuit
2020-12-23 IfcNullStyle
2020-12-23 矩阵的乘方
2019-12-23 unity3d vive pro VR眼镜