# Qt使用EAN-13制作实时生成条码工具,那些坑

Qt使用EAN-13制作实时生成条码工具,那些坑

突发奇想,做一个基于EAN-13实时生成条码的生成器,搜到了一条参考
(https://blog.csdn.net/weixin_43193739/article/details/87894450?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase)
首先感谢参考文献作者分享的代码
实践后发现,并不是所有的ID号生成的条码都能扫出来
检测后发现,参考文中代码并没有写明如何生成校验码
于是乎,本喵mod了一番

QString tiaoMa::Code13(QString codein)
{
    int codeinLenth = codein.length();
    while (codeinLenth < 12) {//补足13位
        codein = "0" + codein;//前位补0
        codeinLenth = codein.length();
    }
    //奇偶性表0为奇,1为偶,x为纵,y为横
    int P[10][6]={0,0,0,0,0,0,
                  0,0,1,0,1,1,
                  0,0,1,1,0,1,
                  0,0,1,1,1,0,
                  0,1,0,0,1,1,
                  0,1,1,0,0,1,
                  0,1,1,1,0,0,
                  0,1,0,1,0,1,
                  0,1,0,1,1,0,
                  0,1,1,0,1,0};
    int x=0,y=0;
    QString pp="";
    while (x<10) {
        y=0;
        pp="";
        while (y<6) {
            pp = pp+QString("%1").arg(P[x][y]);
            y++;
        }
        x++;
    }

    //字符集表
    QString LR[10][3]={"0001101","0100111","1110010",
                       "0011001","0110011","1100110",
                       "0010011","0011011","1101100",
                       "0111101","0100001","1000010",
                       "0100011","0011101","1011100",
                       "0110001","0111001","1001110",
                       "0101111","0000101","1010000",
                       "0111011","0010001","1000100",
                       "0110111","0001001","1001000",
                       "0001011","0010111","1110100"};
    x=0;y=0;
    while (x<10) {
        y=0;
        pp="";
        while (y<3) {
            pp=pp+ "  |  "+LR[x][y];
            y++;
        }
        x++;
    }

    QString l0 = "101";//左手警戒条,固定不变
    int in1 = codein.mid(0,1).toInt();//第一个字符
    x=0;
    int EN[6];//判断采用哪一行的规则
    while (x<10) {
        if(in1 != x)
        {
            x++;
        }else{
            y=0;
            while (y < 6) {
                EN[y] = P[x][y];
                qDebug()<<EN[y];
                y++;
            }
            x=10;//跳出
        }
    }
    qDebug()<<in1<<","<<EN[y];
    x=0;y=0;
    QString codeout[16];//0开始到14
    int num = 0;
    while (num < 15) {
        if (num == 0)
        {
            codeout[num] = "101";//左手警戒条固定不变
        }else if (num == 7)
        {
            codeout[num] = "01010";//中间警戒条固定不变
        }else if (num == 14)
        {
            codeout[num] = "101";//右手警戒条固定不变
        }else{
            if (num >0 && num < 7)
            {
                int inx = codein.mid(num,1).toInt();

                if (EN[num - 1] == 0)
                {
                    codeout[num] = LR[inx][0];
                }else{
                    codeout[num] = LR[inx][1];
                }
                qDebug()<<inx<<","<<EN[num]<<","<<codeout[num];
            }else if (num >7 && num < 13)
            {
                int inx = codein.mid(num-1,1).toInt();
                codeout[num] = LR[inx][2];
                qDebug()<<inx<<","<<codeout[num];
            }else if(num == 13){
                int inx1=codein.mid(0,1).toInt()+codein.mid(2,1).toInt()+codein.mid(4,1).toInt()+codein.mid(6,1).toInt()+codein.mid(8,1).toInt()+codein.mid(10,1).toInt();
                int inx2=codein.mid(1,1).toInt()+codein.mid(3,1).toInt()+codein.mid(5,1).toInt()+codein.mid(7,1).toInt()+codein.mid(9,1).toInt()+codein.mid(11,1).toInt();
                int inx=inx1+inx2*3;
                int iny=inx%10;
                codeout[num] = LR[iny][2];
                qDebug()<<inx1<<","<<inx2<<","<<inx<<","<<iny<<","<<codeout[num];
            }
        }
        num++;
    }
    x=0;
    Codeout = "";
    while (x < 15) {
        Codeout = Codeout + codeout[x];
        x++;
    }
    return Codeout;
}
void tiaoMa::tiaoMaPrint(QString ID)
{
    //打印设置

    QPrinter printer(QPrinter::ScreenResolution);//
    printer.setPageSize(QPrinter::A4);  //设置纸张大小为A4

    QPrintDialog printDialog(&printer);      //弹出打印对话框
    if (printDialog.exec() == QDialog::Accepted){
        QPainter painter(&printer);
        QPen pen; //画笔
        pen.setColor(QColor(0, 0, 0));
        QBrush brush(QColor(0, 0, 0, 255)); //画刷
        painter.setPen(pen); //添加画笔
        painter.setBrush(brush); //添加画刷
        //painter.drawRect(50, 50, 200, 100); //绘制矩形
        int i = 0;
        this->Code13(ID);
        while (i < Codeout.length()) {
            int x = Codeout.mid(i,1).toInt();
            if (x == 1)
            {
                painter.drawRect(i*2+25, 20, 2, 50); //绘制矩形
            }
            i++;
            QFont font("Times", 14);                //设置字体字号
            painter.setFont(font);
            painter.drawText(50,90 ,ID);   //ID
        }
    }
}

嗯,这一下所有码都能扫出来了,但问题也来了
当你的码是如612345678,那么你扫出来的码就会变成00000612345678
此外,最后一位数由于必须是校验码,表面上是13位数,实际是12位数传达信息,
比如1221451422145,最后一个数由于前面12位,最后一位必然是校验码"5"如果改成其他的数就扫不出来啦.因此ID只能输入12位有效数字.
因此如果想要完整把码信息直接变成EAN13码,总是需要扫码方处理掉最后一位校验和前面的零.
简直是大坑啊!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA是一种用于Microsoft Office应用程序的编程语言,可以用于处理和操作Excel、Access、Word等文档和数据。而EAN-13条码(European Article Number 13)是一种全球通用的商品条码编码系统,用于唯一标识和跟踪商品。 在VBA中,可以使用一些内置的函数和方法来处理和生成EAN-13条码。下面是一个简单的示例: 首先,我们需要在Excel中创建一个单元格,用于输入要生成条码的商品编号。然后,在VBA代码中,可以使用如下函数来生成EAN-13条码: ```vba Function GenerateEAN13Barcode(ByVal productCode As String) As String Dim checkDigit As Integer Dim encodedDigits As String ' 检查输入的商品编号是否为12位数字 If Not IsNumeric(productCode) Or Len(productCode) <> 12 Then GenerateEAN13Barcode = "输入的商品编号无效!" Exit Function End If ' 计算校验位 checkDigit = CalculateEAN13CheckDigit(productCode) ' 编码商品编号和校验位 encodedDigits = productCode & checkDigit GenerateEAN13Barcode = encodedDigits End Function Function CalculateEAN13CheckDigit(ByVal code As String) As Integer Dim i As Integer Dim digit As Integer Dim total As Integer ' 遍历商品编号的每一位数字 For i = 1 To 12 digit = Mid(code, i, 1) ' 偶数位数字乘以3,奇数位数字乘以1 If i Mod 2 = 0 Then total = total + digit * 3 Else total = total + digit End If Next i ' 计算校验位 CalculateEAN13CheckDigit = (10 - total Mod 10) Mod 10 End Function ``` 以上代码中,`GenerateEAN13Barcode`函数会接收一个12位的商品编号,并根据校验位计算生成13位的EAN-13条码。`CalculateEAN13CheckDigit`函数用于计算校验位。 在Excel的单元格中,可以使用VBA的`=GenerateEAN13Barcode(A1)`函数来生成EAN-13条码,其中A1为输入的商品编号单元格。执行该函数后,会在相应的单元格中显示生成EAN-13条码。 通过以上的VBA代码,我们可以在Excel中使用VBA函数来生成EAN-13条码,方便实现商品条码生成和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值