找回密码
 立即注册
收起左侧

一个关于QImage的保存问题

4
回复
7493
查看
[复制链接]
累计签到:21 天
连续签到:1 天
来源: 2018-4-23 00:38:35 显示全部楼层 |阅读模式
5Qter豆
我在这个项目里面,现在获取到了想要的像素数据了,然后要将它保存到新的QImage中,并保存。但是这样的代码,在save函数执行的时候会直接崩溃掉,是什么原因的?求教

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

累计签到:742 天
连续签到:1 天
2018-4-23 09:16:27 显示全部楼层
这个看着就感觉是数组越界导致的崩溃!【瞎说的】
回复

使用道具 举报

累计签到:436 天
连续签到:1 天
2018-4-23 09:29:18 显示全部楼层
你不能就这么把 data 给 delete 了。
文档
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.

你应该用 cleanupFunction 来清理它。
不过我更推荐你类似这么写
  1.     QImage img(width,height,QImage::Format_RGB32);
  2.     for (int y=0; y<height; ++y)
  3.     {
  4.         QRgb* line = reinterpret_cast<QRgb*>(img.scanLine(y));
  5.         for (int x=0; x<width; ++x)
  6.         {
  7.             // do something
  8.             r = ...;
  9.             g = ...;
  10.             b = ...;
  11.             line[x] = qRgb(r,g,b);
  12.         }
  13.     }
复制代码
回复

使用道具 举报

累计签到:21 天
连续签到:1 天
2018-4-23 23:06:50 显示全部楼层
miroox 发表于 2018-4-23 09:29
你不能就这么把 data 给 delete 了。
看文档:

我现在有个问题,就是我设定了rgb的值之后,存进去图片,然后save。但重新读出来的rgb值有变化?(变化大到明显影响我的结果)格式是RGB32

点评

我猜是因为jpg的有损压缩,你试试保存成png格式。  详情 回复 发表于 2018-4-24 12:32
回复

使用道具 举报

累计签到:436 天
连续签到:1 天
2018-4-24 12:32:22 显示全部楼层
yuanhong1995 发表于 2018-4-23 23:06
我现在有个问题,就是我设定了rgb的值之后,存进去图片,然后save。但重新读出来的rgb值有变化?(变化大 ...

我猜是因为jpg的有损压缩,你试试保存成png格式。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了