简体   繁体   English

QT - 是否有任何类可以将几张图像合二为一?

[英]QT - Is there any class for combine few images into one?

I would like do some matrix of images, show preview in some widget and after all save it to - for example - jpg file.我想做一些图像矩阵,在一些小部件中显示预览,然后将其保存到 - 例如 - jpg文件。 I know I could copy every image pixel per pixel into big one, but it wasn't efficient method I suppose... Are there any better solution?我知道我可以将每个像素的每个图像像素复制到一个大的像素中,但我认为这不是有效的方法......有没有更好的解决方案?

Thanks in advice.谢谢指教。

Instead of copying individual pixels, I would just directly draw each individual image on a QPixmap large enough to hold all images combined.我不会复制单个像素,而是直接在足够大的QPixmap上绘制每个单独的图像,以将所有图像组合在一起。 The collage can then be generated by drawing each individual image on the collage as follows (untested code):然后可以通过在拼贴画上绘制每个单独的图像来生成拼贴画,如下所示(未经测试的代码):

QList<QPixmap> images;
QPixmap collage;

// Make sure to resize collage to be able to fit all images.
...

for (QList<QPixmap>::const_iterator it = images.begin(); it != images.end(); ++it)
{
    int x = 0;
    int y = 0;

    // Calculate x & y coordinates for the current image in the collage.
    ...
    QPainter painter(&collage);
    painter.drawPixmap(
            QRectF(x, y, (*it).width(), (*it).height()), *it,
            QRectF(0, 0, (*it).width(), (*it).height()));
}

Note that QImage can be used as well instead of QPixmap .请注意,也可以使用QImage代替QPixmap QPixmap is optimized for on screen display though. QPixmap已针对屏幕显示进行了优化。 See the Qt documentation for more details.有关更多详细信息,请参阅Qt 文档

The code above is not working for me, i can not figure out why.上面的代码对我不起作用,我不知道为什么。

What i needed was a collage like:我需要的是像这样的拼贴画:
PicA |图片 | PicB |图片B | Pic...|图片...| ... | ... |
I ve found something similar with QImage and this code works.我发现了与 QImage 类似的东西,这段代码有效。
(tested code): (测试代码):

const int S_iconSize = 80;     //The pictures are all quadratic.
QList<const QPixmap*> t_images;//list with all the Pictures, PicA, PicB, Pic...
QImage resultImage(S_iconSize*t_images.size(), S_iconSize, QImage::Format_ARGB32_Premultiplied);
QPainter painter;

painter.begin(&resultImage);
for(int i=0; i < t_images.size(); ++i)
{
    painter.drawImage(S_iconSize*i, 0, t_images.at(i)->toImage(), 0, 0, S_iconSize, S_iconSize, Qt::AutoColor);
}
painter.end();

QPixmap resultingCollagePixmap = QPixmap::fromImage(resultImage);

I know it is ugly cause the QImage is converted to QPixmap and vice versa, but it works.我知道这很丑陋,因为 QImage 被转换为 QPixmap,反之亦然,但它有效。 So if someone has an idea how to make the code above (from Ton van den Heuvel) run i would be glad.因此,如果有人知道如何使上面的代码(来自 Ton van den Heuvel)运行,我会很高兴。 (Maybe it is just a missing QPainter???) (也许这只是一个缺少的 QPainter ???)

Regards问候

I did the following:我做了以下事情:

// Load the images in order to have the sizes at hand.
QPixmap firstPixmap(":/images/first.png");
QPixmap secondPixmap(":/images/second.png");
const int gap = 25;

// Create an image with alpha channel large enough to contain them both and the gap between them.
QImage image(firstPixmap.width() + gap + secondPixmap.width(), firstPixmap.height(), QImage::Format_ARGB32_Premultiplied);
// I happen to need it transparent.
image.fill(QColor(Qt::transparent));

// Paint everything in a pixmap.
QPixmap pixmap = QPixmap::fromImage(image);
QPainter paint(&pixmap);
paint.drawPixmap(0, 0, firstPixmap);
paint.drawPixmap(firstPixmap.width() + gap, 0, secondPixmap);

// Use it.
QIcon icon(pixmap);
[...]

No, you don't want to do this pixal by pixel.不,您不想逐像素执行此像素操作。 QImage is a QPaintDevice . QImage是一个QPaintDevice So you can load them, render them into each other and save them in several formats as you like.因此,您可以加载它们,将它们相互渲染并根据需要以多种格式保存它们。 And of course display them on the screen.当然,在屏幕上显示它们。

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

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