简体   繁体   English

如何翻转QImage

[英]How to flip a QImage

What is the right way to flip/mirror a QImage? 翻转/镜像QImage的正确方法是什么? The following snippet does not work. 以下代码段不起作用。

 //allocate buffer
 BYTE* pRgb32Buffer = new BYTE[width*height* 4];
 //create paint device
 QImage img = QImage(pRgb32Buffer , width, height, getStride(width, pixelFormat), QImage::Format_RGB32);
 //do some drawing on image (works!) 
 QPainter painter(&img);
 painter.drawText(10, 50, QString("some text"));
 //mirrore image (doesn't mirror the orignal buffer!!!)
 img = img.mirrored(false,true);
 //doesn't work either
 //QImage mirrored = img.mirrored();
 //img = mirrored;
 //mirrored.detach(); 

使用QImage::mirrored()解决方案:

memmove(pRgb32Buffer, img.mirrored().bits(),img.byteCount());

I have this working code to mirror a QPixmap onto an QImage. 我有这个工作代码将QPixmap镜像到QImage上。

QPixmap* source = //... Getting my pixmap form somewhere...
QImage target(QSize(source->width(), source->height()), QImage::Format_ARGB32);
QPainter painter(&target);
QTransform transf = painter.transform();
transf.scale(-1, 1);
painter.setTransform(transf);
painter.drawPixmap(-source->width(), 0, *source);

The source contains the mirrored pixmap after that code. source包含该代码之后的镜像像素图。 You should be able to do the same with QImage and the QPainter::drawImage function as alternative. 您应该可以使用QImageQPainter::drawImage函数作为替代方法。

Optionally you can save the file if you want (make sure you have the imageformats dll's or it won't write): (可选)您可以根据需要保存文件(确保您具有图像格式dll或不会写入):

QImageWriter writer("c:\\theimage.tiff", "tiff");
writer.setCompression(1);
writer.write(target);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM