QT生成一维条形码

QT生成一维条形码

最近在做一款收银系统,需要使用条形码,商品的条形码一般采用EAN13规则。新手一枚,看不懂大神写的关于zint和QZXing的使用方法,很多也是识别条形码和二维码的方法,并没有太好的帖子写生成条形码。参考https://blog.csdn.net/abcpanpeng/article/details/8130392写了几十行代码,可以制作13位的EAN13规则的条形码,不满13位补足13位。

下面是原始代码

void MainWindow::Code13(QString codein)
{
    int codeinLenth = codein.length();
    while (codeinLenth < 13) {//补足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];
                y++;
            }
            x=10;//跳出
        }
    }
    x=0;y=0;
    QString codeout[15];//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];
                }
            }else if (num >7 && num < 14)
            {
                int inx = codein.mid(num-1,1).toInt();
                codeout[num] = LR[inx][2];
            }
        }
        num++;
    }
    x=0;
    //QString Codeout;
    Codeout = "";
    while (x < 15) {
        Codeout = Codeout + codeout[x];
        x++;
    }
}

使用方法很简单,在.h文件中声明QString Codeout;复制上面代码到.cpp文件中。然后使用QPainter绘制条形码。codein 是13位的条码数字,Codeout是生成的95位数字,只有0和1,使用QPainter绘图时,1画矩形,0不画,就行了。

void MainWindow::paintEvent(QPaintEvent *)
{
        QPainter painter(this);
        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("6901028024969");
        while (i < 95) {
           int x = Codeout.mid(i,1).toInt();
            if (x == 1)
            {
                painter.drawRect(i*2+25, 250, 2, 100); //绘制矩形
            }
            i++;
        }
}

Bingo

你可能感兴趣的:(QT,条形码)