【Qt开发】如何将内存图像数据封装成QImage V1

如何将内存图像数据封装成QImage
        当采用Qt开发相机数据采集软件时,势必会遇到采集内存图像并进行处理(如缩放、旋转)操作。如果能够将内存图像数据封装成QImage,则可以利用QImage强大的图像处理功能来进行图像处理,并能很好的进行显示。
       下面以灰度相机为例,介绍封装方法:
       第一步:首先根据相机的SDK内的读图像函数,获取图像数据imgData、宽度imgWidth和高度imHeight。
       第二步:申请QImage对象,注意类型是Format_RGB32.

       第三步:利用成员函数setPixel()设置QImage像素。由于相机输出的图像是灰度图像,每一位置的R、G、B分量相等且均等于当前位置的像素值。

       具体程序如下:


	QImage desImage = QImage(imgWidth,imgHeight,QImage::Format_RGB32); //RGB32

	//RGB分量值
	int b = 0;
	int g = 0;
	int r = 0; 

	//设置像素
	for (int i=0;i<imgHeight;i++)
	{
		for (int j=0;j<imgWidth;j++)
		{
			b = (int)*(imgDataNew+i*imgWidth+j);
			g = b;
			r = g;
			desImage.setPixel(j,i,qRgb(r,g,b));
		}
	}



对于灰度图像数据,如下封装方式是错误的。
QImage desImage = QImage(imgData, imgWidth, imgHeight, QImage::Format_Indexed8)
原因是QImage的构造函数中写道:
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.




原文地址:https://www.cnblogs.com/huty/p/8518260.html