UI只是简单实现了,主要为了学习QGraphicsView这方面绘图和二维码条形码的生成和解析。

一、上图

主界面:

Qt实现喷码器代码,实现二维码、条形码、图形的旋转、移动等-LMLPHP

文本、二维码、条形码、图片的添加:

Qt实现喷码器代码,实现二维码、条形码、图形的旋转、移动等-LMLPHP

二、代码部分

1、二维码实现

需要引入qxing库,链接:https://github.com/zxing/zxing

#include "./qzxing/include/QZXing.h"
#include "./qzxing/include/QZXing_global.h"

生成二维码:

    QString text = ui->lineEdit->text();
        if(text.isEmpty())
            return;
        QImage img = QZXing::encodeData(text
                                        ,QZXing::EncoderFormat::EncoderFormat_QR_CODE
                                        ,QSize(200,200)
                                        ,QZXing::EncodeErrorCorrectionLevel::EncodeErrorCorrectionLevel_H
                                        ,true
                                        ,false);
        //图片大小设置,与label大小适配
        //img = img.scaled(ui->label->width(), ui->label->height());
        mQrPixmap = QPixmap::fromImage(img);
        ui->label->setPixmap(mQrPixmap);

解析二维码

        QImage img;
        img =  mQrPixmap.toImage();//ui->label->pixmap()->toImage();
        if(img.isNull())
        {
            qDebug()<<"图片为空";
            return;
        }
        QZXing decode;
        decode.setDecoder(QZXing::DecoderFormat_QR_CODE);
        decode.setSourceFilterType(QZXing::TryHarderBehaviour_ThoroughScanning|QZXing::TryHarderBehaviour_Rotate);
        decode.setSourceFilterType(QZXing::SourceFilter_ImageNormal);
        QString info = decode.decodeImage(img);
        ui->lineEdit->setText(info);

2、条形码实现

 需要引入zint库

#include "./zint/include/png.h"
#include "./zint/include/zint.h"

生成条形码

    struct zint_symbol *my_symabol = ZBarcode_Create();
    my_symabol->symbology = BARCODE_CODE128;
    my_symabol->border_width = 1;  // 白边
    //my_symabol->scale = 10;  // 放大
    my_symabol->height = 30;  // 条码高度
    aFile_Barcode = QDir::currentPath() + "Barcode.png";
    QByteArray ba = aFile_Barcode.toLatin1();
    strcpy_s(my_symabol->outfile,ba.data());
    QByteArray ba1=Gcode.toLatin1();
    char *cGcode=ba1.data();
    int ret = ZBarcode_Encode(my_symabol,(unsigned char*)cGcode,0);
    if (ret != 0)
        return false;
    ZBarcode_Print(my_symabol,0);
    ZBarcode_Delete(my_symabol);

解析条形码(和二维码那个类似,用qxing)

3、绘制部分

1)绘制刻度尺

void CustomItem::drawVerticalRuler(QPainter *painter)
{
    //背景色
    QColor color = QColor(205, 205, 205);
    painter->setBrush(color);
    QPoint axisStartPointFill;
    QPoint axisXEndPointFill;
    axisStartPointFill.setX(0);
    axisStartPointFill.setY(0);
    axisXEndPointFill.setX(m_ptStartPos.x());
    axisXEndPointFill.setY(m_ptStartPos.y() + m_axisHeight);
    painter->fillRect(QRectF(axisStartPointFill, axisXEndPointFill), painter->brush()); // 填充背景色,不绘制边框

    //绘制坐标轴两条直线
    QPen pen;
    pen.setColor(Qt::black);
    painter->setPen(pen);
    QPoint axisStartPoint;
    QPoint axisYEndPoint; // y 轴终点

    axisStartPoint.setX(m_ptStartPos.x());
    axisStartPoint.setY(m_ptStartPos.y());

    axisYEndPoint.setX(m_ptStartPos.x());
    axisYEndPoint.setY(m_ptStartPos.y() + m_axisHeight);

    painter->drawLine(axisStartPoint, axisYEndPoint);
    //qDebug()<<"drawVerticalRuler " << axisStartPoint << axisYEndPoint << m_ptStartPos.x() << m_ptStartPos.y() << m_axisHeight;

    //刻度值
    int deltaY = m_axisHeight / 100; // Y 轴坐标刻度宽度   5px像素间隔
    //qDebug() << "m_ptStartPos.x() " << m_ptStartPos.x() << m_axisWidth;

    for (int i = m_ptStartPos.y() + deltaY; i <= m_axisHeight + m_ptStartPos.y(); i = i + deltaY)
    {
        // 坐标刻度起始点
        QPoint calibrationStartPoint;
        calibrationStartPoint.setX(m_ptStartPos.x()); // x 轴不变,y 轴变
        calibrationStartPoint.setY(i);

        // 坐标刻度结束点
        QPoint calibrationEndPoint;

        if(i % (5*deltaY) == m_ptStartPos.y()){
            calibrationEndPoint.setX(m_ptStartPos.x() - COORDINATE_CALIBRATION_LENGTH_2);
        }else{
            calibrationEndPoint.setX(m_ptStartPos.x() - COORDINATE_CALIBRATION_LENGTH);
        }
        calibrationEndPoint.setY(i);

        painter->drawLine(calibrationStartPoint, calibrationEndPoint);
    }

    //绘制坐标值
    deltaY = m_axisHeight / 100;  // X 轴坐标刻度宽度 19
    int value = 0;
    for (int i = m_ptStartPos.y() + deltaY; i <= m_axisHeight + m_ptStartPos.y(); i = i + deltaY)
    {
        QPoint temp;
        temp.setX(m_ptStartPos.x() - 18); // 左边移动的偏移量
        temp.setY(i - 5);
        if(i % (5 * deltaY) == m_ptStartPos.y()){
            value++;
            QString strAxisYValue = QString::number(value*5);
            painter->drawText(temp, strAxisYValue);
        }
    }
}

2)绘制文本

void CustomItem::drawText(QPainter *painter)
{
    if (hasFocus() || isAction) {
        qDebug()<< "drawText isAction " << isAction;
        painter->setPen(QPen(QColor(255,0,0), 1));
    } else {
        painter->setPen(QPen(QColor(255, 255, 255)));
    }
    painter->setFont(QFont("Arial", 20));
    painter->drawRect(boundingRect());

    painter->setPen(Qt::black);
    painter->drawText(boundingRect(), mText);
    qDebug()<< "drawText isAction2" << isAction;
    isAction = false;
}

3)绘制图片(二维码、条形码、图片用同一个方法实现)

void CustomItem::drawPixmap(QPainter *painter)
{
    if (hasFocus() || isAction) {
        painter->setPen(QPen(QColor(255,0,0), 1));
    } else {
        painter->setPen(QPen(QColor(255, 255, 255)));
    }
    painter->drawPixmap(boundingRect().toRect(), mPixmap);
    painter->drawRect(boundingRect());
    isAction = false;
}

完整代码链接:

https://download.csdn.net/download/Baikal_Lake/89236566

04-28 16:29