前言
今天在测试图片验证码的的显示问题时,想要模拟测试一下接口,我已写好的接口是将QByteArray转化为图片(QPixmap),然后在标签QLabel上显示。需要模拟的是入参,就是将图片(QPixmap)转化为QByteArray,本来以为一个接口的就能解决,可事情不是我们想象的那么简单。
在翻阅过源码后,清楚了两者的转换,所以在此记录一下。
代码和说明
本来我就是为了测试图片验证码 的数据,主要是将图片的数据提取出来,在翻阅帮助文档的过程中,我发现了
uchar *QImage::bits()
以为此函数可以直接获取到图片的数据,但是看帮助它只是给出了第一行的像素数:
Returns a pointer to the first pixel data. This is equivalent to scanLine(0).
Note that QImage uses implicit data sharing. This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.
故此函数是行不通的,而且我也不懂图片的