Qt 如何使用 QImage 设置指定的颜色为透明色?
2018-02-12 18:56
591 查看
Qt 如何使用 QImage 设置指定的颜色为透明色?
需求背景:使用华大身份证读卡器模块读取身份证信息,通过模块读取的图片为 *.BMP 格式,无透明色,故绘制到身份证上无法美观的显示。通过查询身份证拍摄要求,得知不得穿白色的领子拍摄,采用白底背景。通过工具得知图片底色颜色值QColor(254,254,254,255)。故只需遍历图片把颜色值等于图片底色的值设置为
QColor(254,254,254,0)。
void IDCard::Bmp2Png(QImage &bmp,QString &path) { //BMP颜色格式转换成RGBA颜色格式 bmp = bmp.convertToFormat(QImage::Format_RGBA8888_Premultiplied,Qt::NoFormatConversion); int bmpWidth = bmp.width(); int bmpHeight = bmp.height(); //透明颜色 QColor bmpBack(254,254,254); QColor bmpBackA(254,254,254,0); for(int i=0;i< bmpWidth;++i) { for(int j=0;j<bmpHeight;++j) { //如果身份证背景色等于 Color(254,254,254,255),则设置为透明色 Color(254,254,254,0) if(bmp.pixelColor(i,j)==bmpBack) { bmp.setPixelColor(i,j,bmpBackA); } } } //保存转换的图片 bmp.save(path); return; }
Qt QImage图片透明设置(Thinkvd开发日志)
开发Thinkvd中的player,设置透明度用的是sdl来实现的,转换中的水印用的是png, 如何设置水印的透明度,实际上要求把图片转换成带alpha的32位即可。实现代码://! [8]void ImageComposer::recalculateResult(){
int alpha = 100;
QPainter::CompositionMode mode = currentMode(); QPainter painter(&resultImage);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(resultImage.rect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
//painter.drawImage(0, 0, destinationImage);
//painter.setCompositionMode(mode);
painter.drawImage(0, 0, sourceImage);
//painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
//painter.fillRect(resultImage.rect(), Qt::white);
//painter.fillRect(resultImage.rect(), QColor(0, 0, 0, alpha));
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
painter.fillRect(resultImage.rect(), QColor(0, 0, 0, alpha)); painter.end();
resultImage.save("f:/tmp/dddd.png"); resultLabel->setPixmap(QPixmap::fromImage(resultImage)); }
注意:1. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);不能少2. painter.drawImage与 painter.fillRect(resultImage.rect(), QColor(0, 0, 0, alpha));顺序.
Qt处理图片背景为透明色
Qt界面开发程序中,需要将背景为白色的图片改为透明
[cpp] view plain copyvoid ConvertImageToTransparent(QPixmap qPixmap){
QImage image = qPixmap.toImage();
image = image.convertToFormat(QImage::Format_ARGB32);
union myrgb
{
uint rgba;
uchar rgba_bits[4];
};
myrgb* mybits =(myrgb*) image.bits();
int len = image.width()*image.height();
while(len --> 0)
{
mybits->rgba_bits[3] = (mybits->rgba== 0xFFFFFFFF)?0:255;
mybits++;
}
}
Qt 中如何实现对图片设置透明色显示
咋看题目可能不是很明白是什么意思,用下面的一个简单的例子来简单的解释下。
比如说下面的一张图1:
图 1
我们通过设置透明色为分周围的粉红色,就得到下面图2这个效果。
图 2
当然,也可以不必这么麻烦,可以直接通过ps等图片处理的工具,把这个图片处理成透明背景的图片然后保存成png格式也可,我这里只是想说说怎么用Qt实现。怎么写过文章,写的不好大家别扔砖哈,错误之处还请大家指出,共同进步!呵呵。
好啦,不说多的,直接上代码吧!
[cpp] view plain copy QImage TestDialog::getTransImage(QImage image , QPoint ponit)
{
//判断所取的点是否在图片内部
if( image.valid( ponit ) )
{
QRgb rgb = image.pixel( ponit );
QVector<QRgb> rgbVector = image.colorTable () ;
for (int i = 0; i < rgbVector.size(); ++i)
{
if( rgbVector.at(i) == rgb )
{
/*
注意这里如果要变成黑色背景的话就是QRgb rgb2 = QColor(0, 0, 0, 0).rgb() ;
留意 rgb()与rgba()的区别
*/
QRgb rgb2 = QColor(0, 0, 0, 0).rgba() ;
image.setColor( i,rgb2 );
return image ;
}
}
}
return image ;
}
上面用到的两个重要的函数就是:
QRgb QImage::pixel ( const QPoint & position ) const
这个得到图片中某点的色素的rgb的值。
void QImage::setColor ( int index, QRgb colorValue )
设定图片的color table的索引值index所对应的颜色为 colorValue 。呵呵,英语很菜的,还是直接看文档吧,我把我的整个测试程序放到这个下面,感兴趣的可以看看,最后程序运行的结果如下:
测试程序的下载地址:
http://download.csdn.net/detail/xj178926426/3893256
相关文章推荐
- 如何使用Qt4,设置QLabel中字体的颜色。
- 使用Qt 4如何设置QLabe中字体的颜色
- 使用Qt 4如何设置QLabe中字体的颜色
- 使用Qt 4如何设置QLabe中字体的颜色
- 使用Qt 4如何设置QLabe中字体的颜色
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt QImage图片透明设置(Thinkvd开发日志)
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt 中如何实现对图片设置透明色显示 .
- [重点是调透明度]Panel设置透明属性 C#中颜色的使用
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- 如何使用qtp检查网页中显示的文字颜色为指定的颜色
- 如何使用代码动态的获取和设置ImageView的宽度和高度?
- 如何使用ASP设置指定站点CPU最大使用程度
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- 如何在Delphi TImageList 中使用 透明 png 图标
- 使用自定义Effect让指定颜色透明
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- 关于如何在iframe中设置背景颜色为透明