Qt:图片修改/缩放/旋转

不同的图片

QPixmap:主要用于绘图,针对平台进行优化,和平台相关(依赖于平台的绘图引擎)
QImage:和平台无关(使用Qt的绘图引擎),可以对图片像素修改(setPixpel()和pixel())。注意:可以在线程中绘制
QBitmap 继承Qpixmap,用于单色(单通道图像,占用内存小)
QPicture:保存绘图状态(使用begin()方法在QPicture上进行绘图,end()结束绘图,save()保存至档案,load()加载保存好的档案)
简单的:QImage用来加载图像,QPixmap用来绘图,QPicture用来保存需要反复绘制的(比如背景),QBitmap用来绘制黑白图像

图片转换

Qpixmap::toImage()
Qpixmap::fromImage()

图片修改

1. QPainter指定绘图设备,直接用QPainter在图片上绘图
QPixmap pixmap;
QPainter painter(&pixmap);
2. 使用指针遍历像素点
3. 修改像素点
setPixel(x, y, qRgb());
4. 保存图像
save()

图片缩放

QPixmap::scaled(QSize())
缩放策略:
IgnoreAspectRatio 矩形框有多大,图片就缩放成多大,不限制原图片的长宽比
KeepAspectRatio 保持原图片的长宽比,且不超过矩形框的大小
KeepAspectRatioByExpanding 根据矩形框的大小最大缩放图片

图片旋转

painter.translate(x0, y0); //旋转的中心设为原点
painter.rotate();  //坐标轴旋转度数
painter.restore(); //复位之前的坐标系

/**
 * @description: 保存jpg
 * @param {type} 
 * @return: 
 */
void save_pixmap(const QString &fileName, const QByteArray &data) {
    QPixmap pixmap;
    pixmap.loadFromData(data, "JPG");
    pixmap.save(fileName, "JPG", 100);
}

/**
 * @description: 遍历image元素
 * @param {type} 
 * @return: 
 */
void image_pix(const QString &imagePath)
{
    QImage image(imagePath);
    QSize size = image.size();
    int width = size.width();
    int height = size.height();
    int realWidth = image.bytesPerLine(); //图像每行字节数
    uchar *imageData = image.bits();      //可以获取图像的首地址
    uchar *src = imageData;

    for (int i = 0; i < height; i++)
    {
        imageData = src + realWidth * i; // 行首
        for (int j = 0; j < width; j++)
        {
            // *(imageData+j)
        }
    }
}

/**
 * @description: 图片旋转
 * @param {type} 
 * @return: 
 */
void picture_rotate(QPixmap &pixmap, const int &rotate) {
    QMatrix matrix;  
    matrix.rotate(rotate);  // 大于0则顺时针
    pixmap = pixmap.transformed(matrix, Qt::SmoothTransformation);
}
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承 QGraphicsItem 实现这个功能。以下是一个简单的示例: ```cpp #include <QGraphicsItem> #include <QPainter> class RectangleItem : public QGraphicsItem { public: RectangleItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent) , m_rect(0, 0, 100, 100) , m_rotationPoint(m_rect.center()) { setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges); setAcceptHoverEvents(true); } QRectF boundingRect() const override { return m_rect.adjusted(-5, -5, 5, 5); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override { Q_UNUSED(option); Q_UNUSED(widget); painter->setRenderHint(QPainter::Antialiasing); painter->setPen(Qt::black); painter->setBrush(Qt::lightGray); painter->drawRect(m_rect); // 画旋转点 painter->setBrush(Qt::black); painter->drawEllipse(m_rotationPoint, 5, 5); } QVariant itemChange(GraphicsItemChange change, const QVariant &value) override { if (change == ItemPositionChange || change == ItemRotationChange) { // 更新旋转点的位置 m_rotationPoint = mapToScene(m_rect.center()); } return QGraphicsItem::itemChange(change, value); } void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override { // 鼠标在旋转点上时,更改鼠标形状 if (QLineF(event->pos(), m_rotationPoint).length() < 10) { setCursor(Qt::PointingHandCursor); } else { setCursor(Qt::ArrowCursor); } } void mousePressEvent(QGraphicsSceneMouseEvent *event) override { if (QLineF(event->pos(), m_rotationPoint).length() < 10) { // 点击旋转点,记录下初始角度 QPointF center = mapToScene(m_rect.center()); m_initialAngle = QLineF(center, event->scenePos()).angle(); } else { QGraphicsItem::mousePressEvent(event); } } void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override { if (event->buttons() & Qt::LeftButton && isSelected()) { if (QLineF(event->pos(), m_rotationPoint).length() < 10) { // 拖动旋转点,计算角度差并旋转矩形 QPointF center = mapToScene(m_rect.center()); qreal angle = QLineF(center, event->scenePos()).angle(); setRotation(rotation() + angle - m_initialAngle); m_initialAngle = angle; } else { QGraphicsItem::mouseMoveEvent(event); } } } void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override { QGraphicsItem::mouseReleaseEvent(event); prepareGeometryChange(); } private: QRectF m_rect; // 矩形的区域 QPointF m_rotationPoint; // 旋转点的位置 qreal m_initialAngle = 0; // 拖拽旋转点时记录下来的初始角度 }; ``` 在 `QGraphicsView` 中添加该 item: ```cpp QGraphicsScene *scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); RectangleItem *item = new RectangleItem; scene->addItem(item); ``` 这样就可以通过鼠标拖拽、缩放旋转旋转来操作该矩形了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值