Something wrong with my barcode (Code 128)

后端 未结 2 1484
清酒与你
清酒与你 2021-02-16 00:29

It was easy to generate a 3 of 9 barcode using Font()

Font f = new Font(\"Free 3 of 9\", 80);
this.Font = f;

Label l = new Label();
l.Text = \"*STA         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-16 00:59

    Code 128 can be represented with a font totally fine. It's trickier than 3 of 9 though because you have to include a checksum character at the end which needs to be dynamically computed based on the content of the barcode. There are also 3 different versions which each have a different start char.

    In other words the barcode needs to be laid out like:

    [start char][barcode][checksum][stop char]
    

    The benefit of code128 is that it is much more concise than 3 of 9.

    This page helped me work out the algorithm to compute my checksums.

    A very general overview of the algorithm is:

    1. Each character of the barcode gets a specific value assigned to it depending on what the character is and where it is located in the barcode.

    2. All of the values in 1) above are added together.

    3. Get the modulus 103 value of the total in 2) above.

    4. In most cases, the checksum char will be the ASCII code for: (modulus value plus 32) as determined in 3) above.

    There were some nuances, I ended up needing to create this algorithm in javascript of all things (no questions). For my own future reference and to show some of the nuances this is what it looked like:

    /*
     * This is the variable part of my barcode, I append this to a 
     * static prefix later, but I need to perform logic to compute the 
     * checksum for this variable. There is logic earlier that enforces 
     * this variable as a 9 character string containing only digits.   
     */ 
    var formIncrement = // a 9 char "digit" string variable
    
    /*
     * Dynamically compute the total checksum value (before modulus) 
     * for the variable part of my barcode, I will need to get a modulus 
     * from this total when I am done. If you need a variable number of 
     * characters in your barcodes or if they are not all digits 
     * obviously something different would have to be done here.  
     */ 
    var incrementCS = ((parseInt(formIncrement.charAt(0)) + 16) * 7) +
                      ((parseInt(formIncrement.charAt(1)) + 16) * 8) +
                      ((parseInt(formIncrement.charAt(2)) + 16) * 9) +
                      ((parseInt(formIncrement.charAt(3)) + 16) * 10) +
                      ((parseInt(formIncrement.charAt(4)) + 16) * 11) +
                      ((parseInt(formIncrement.charAt(5)) + 16) * 12) +
                      ((parseInt(formIncrement.charAt(6)) + 16) * 13) +
                      ((parseInt(formIncrement.charAt(7)) + 16) * 14) + 
                      ((parseInt(formIncrement.charAt(8)) + 16) * 15);
    
    /*
     * 452 is the total checksum for my barcodes static prefix (600001), 
     * so it doesn't need to be computed dynamically, I just add it to 
     * the variable checksum total determined above and then get the 
     * modulus of that sum:  
     */ 
    var checksum = (452 + incrementCS) % 103
    
    
    var barcode = "š600001" + formIncrement
    
    /*
     * The 0 and the 95 - 102 cases had to be defined explicitly because 
     * their checksum figures do not line up with the javascript char 
     * codes for some reason (see the code 128 definition table in the 
     * linked page) otherwise we simply need to get the charCode of the 
     * checksum + 32. I also tack on the stop char here. 
     */ 
    switch (checksum) {
        case 0 :
        barcode += "€œ";
        break;
        case 95 :
        barcode += "‘œ";
        break;
        case 96 :
        barcode += "’œ";
        break;
        case 97 :
        barcode += "“œ";
        break;
        case 98 :
        barcode += "”œ";
        break;
        case 99 :
        barcode += "•œ";
        break;
        case 100 :
        barcode += "–œ";
        break;
        case 101 :
        barcode += "—œ";
        break;
        case 102 :
        barcode += "˜œ";
        break;
        default :
        barcode += String.fromCharCode(checksum + 32) + "œ";
    }
    
    return barcode;
    

    You may notice that my start and stop chars in my example (š, œ) don't seem to match up with the ones shown on the linked page. If I remember I think it was because I had some non-standard code128 font and these chars translated to the correct ones.

    EDIT

    I checked back in my documentation. It looks like I got the font from right here. With that font specifically and using the algorithm above, I just made a code128b barcode for test which came out to štestwœ, it scanned fine. Your checksum algorithm seems to be working fine because we both have w but it looks like your start and stop codes are off if you are getting: ÌtestwÎ.

    I made a point to look for the same barcode font that I am using because I have a feeling that different brands of code128 fonts may implement different characters to represent the start and stop barcodes.

提交回复
热议问题