C++ QImage padding-有可用的函数吗?

C++ QImage padding-有可用的函数吗?,c++,qt,image-processing,qimage,C++,Qt,Image Processing,Qimage,有什么功能可以用来填充我的QImage对象吗? 我试图在网上搜索,但没有成功。 Thx提前 这是我的密码: #include "mainwindow.h" #include <QApplication> #include "qimage.h" #include <QImage> #include <QLabel> #include <QColor> #include "qcolor.h" #include <Qdebug> #inclu

有什么功能可以用来填充我的QImage对象吗? 我试图在网上搜索,但没有成功。 Thx提前

这是我的密码:

#include "mainwindow.h"
#include <QApplication>
#include "qimage.h"
#include <QImage>
#include <QLabel>
#include <QColor>
#include "qcolor.h"
#include <Qdebug>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsView>


int main(int argc, char *argv[])
{
    printf("Init!");
    qDebug() << "C++ Style Debug Message";
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    int height;
    int width;
    unsigned char *p, *p_begin;
    QImage img("C:\\Users\\Owner\\Pictures\\2013-09-26\\IMG_0836.JPG");
    height = img.height();
    width = img.width();
    p = (unsigned char *)malloc(height * width * sizeof(unsigned char));
    p_begin = p;

    qDebug() << "Begin For Loop";
    for (int row = 0; row < height; ++row)
    {
    for (int col = 0; col < width; ++col)
    {
        QColor clrCurrent( img.pixel( col, row ));
        *p = (unsigned char)((clrCurrent.green() * 0.587) + (clrCurrent.blue() * 0.114) + (clrCurrent.red() * 0.299));
        p++;
    }
}

qDebug() << "Finished First Loop!";
p = p_begin;
for (int row = 0; row < height; ++row)
{
    for (int col = 0; col < width; ++col)
    {
        QColor clrCurrent(img.pixel(col, row));
        clrCurrent.setBlue((int)(*p));
        clrCurrent.setGreen((int)(*p));
        clrCurrent.setRed((int)(*p));
        img.setPixel(col, row, clrCurrent.rgba());
        p++;
    }
}

QPixmap pixmap = QPixmap::fromImage(img);
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
scene.addItem(item);
view.show();

return a.exec();
 }
#包括“mainwindow.h”
#包括
#包括“qimage.h”
#包括
#包括
#包括
#包括“qcolor.h”
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
printf(“Init!”);

qDebug()QImage
无法更改其大小。您需要创建一个新的更大的图像,擦除其内容,在其上启动一个
QPainter
,然后在新图像的中心绘制源图像。这样您就可以进行填充

下面是一个函数,它返回图像的填充版本,具有用于填充的给定颜色,以及用于填充的测试线束

// https://github.com/KubaO/stackoverflown/tree/master/questions/image-pad-35968431
#include <QtGui>

template <typename T>
QImage paddedImage(const QImage & source, int padWidth, T padValue) {
  QImage padded{source.width() + 2*padWidth, source.height() + 2*padWidth, source.format()};
  padded.fill(padValue);
  QPainter p{&padded};
  p.drawImage(QPoint(padWidth, padWidth), source);
  return padded;
}

int main() {
   QImage source{64, 64, QImage::Format_ARGB32_Premultiplied};
   source.fill(Qt::red);
   auto padded = paddedImage(source, 16, Qt::blue);
   padded.save("test.png");
}
//https://github.com/KubaO/stackoverflown/tree/master/questions/image-pad-35968431
#包括
模板
QImage paddedImage(常量QImage和源代码、int焊盘宽度、T焊盘值){
QImage填充{source.width()+2*padWidth,source.height()+2*padWidth,source.format()};
填充(padValue);
QPainter p{&padded};
p、 drawImage(QPoint(焊盘宽度,焊盘宽度),源);
返回填充;
}
int main(){
QImage源代码{64,64,QImage::Format_ARGB32_Premultiplied};
源。填充(Qt::红色);
自动填充=填充图像(源,16,Qt::蓝色);
padded.save(“test.png”);
}
输出:


你到底想实现什么?我想用相同的边界像素填充我的qimage。这会成功吗?我的意思是,你想用图像的填充物做什么?你想把它画成什么,在场景、标签上使用还是什么?或者你想保存它?添加填充物的目的是什么?@Danielitan-只需使用图像编辑器并发布即可所需结果的示例。