当前位置: 首页 > 工具软件 > Zint > 使用案例 >

qt+zint实现打印一维条形码

廉雅惠
2023-12-01

1.需求

需要将小票号打印成条形码,退换货时通过扫描枪直接扫描不用人工输入进行退货和换货,加快速度

2.实现

这里通过zint生成条形码图片,通过qt将图片绘制到打印纸上实现,也可以直接在打印纸上进行绘制线条,因人而异

3.代码片段

打印小票号
void mytestimage::printBarcode(const QString &strTitle)
{
    //打印部分
    //这里先生成图片
    QString strImagePath("");
    strImagePath = QCoreApplication::applicationDirPath() + "//test.png";
    struct zint_symbol * my_symbol = ZBarcode_Create();
    my_symbol->symbology = BARCODE_CODE93;//BARCODE_CODE11;
    strcpy(my_symbol->outfile, strImagePath.toLocal8Bit().toStdString().c_str());
    ZBarcode_Encode(my_symbol, (unsigned char*)strTitle.toLocal8Bit().toStdString().c_str(), 0);
    ZBarcode_Print(my_symbol, 0); 
    ZBarcode_Delete(my_symbol);
    //绘制
    QPrinter printer;
    QPrintDialog printdialog(&printer, this);
    if (printdialog.exec())
    {
        //这里我如何保证不进行拉伸呢
        //QString strname = printer.printerName();
        int dpi = printer.logicalDpiX();
        int nmm = printer.paperRect(QPrinter::Millimeter).width();
        int width = std::min(nmm, 80)*(dpi*1.0/25.4);     //这里选择80mm纸张
        QPainter painter(&printer);
        QImage image(strImagePath, "PNG");
        QRect rt1 = image.rect();
        QRect rtwindow = painter.window();
        QRect rtview = painter.viewport();
        int nmax = 0;
        if (image.width() + 20 < width)
            nmax = std::max(image.width() + 20, width);
        else
            nmax = std::min(image.width() + 20, width);

        QRect rtimage(0, 0, nmax, rtwindow.height());
        QRect rtimageview(0, 0, nmax, rtview.height());
        int nmin = std::min(image.width(), nmax - 80);
        QRectF rtdes(40, 10, nmin, 100);
        QRectF rtsrc(0, 0, image.width(), image.height());
        painter.setWindow(rtimage);
        painter.setViewport(rtimageview);
        painter.drawImage(rtdes, image, rtsrc);  
        painter.setWindow(rtwindow);
        painter.setViewport(rtview);
    }

    //这里再删除图片
    //QFile file(strImagePath);
    //QFile::remove(strImagePath);
}

4.备注

1.测试项目地址http://download.csdn.net/detail/zhang_ruiqiang/9164955
2.可编译zint部分http://download.csdn.net/detail/zhang_ruiqiang/9165027
3.积分不足邮件发我,我单独发你1139760522@qq.com
 类似资料: