1

Can someone recommend a (free for commercial use) library to generate EAN-13 barcode?

I do not need an image, only a string.

I would like to pass product code, manufacturer code and country/region (not number system) as a parameters.

It could translate country/region to number system, concat it all together and calculate checksum.

4

1 回答 1

4

基本上: Ean13是基于模型的校验和计算:

所以你有许多没有校验和的 12 位数字,然后添加校验和

校验和功能:

   public static string CalculateEan13(string country, string manufacturer, string product)
    {
        string temp = $"{country}{manufacturer}{product}";
        int sum = 0;
        int digit = 0;

        // Calculate the checksum digit here.
        for (int i = temp.Length; i >= 1; i--)
        {
            digit = Convert.ToInt32(temp.Substring(i - 1, 1));
            // This appears to be backwards but the 
            // EAN-13 checksum must be calculated
            // this way to be compatible with UPC-A.
            if (i % 2 == 0)
            { // odd  
                sum += digit * 3;
            }
            else
            { // even
                sum += digit * 1;
            }
        }
        int checkSum = (10 - (sum % 10)) % 10;
        return $"{temp}{checkSum}";
    }

如何自己计算校验位

条形码编号示例:501234576421

第 1 步:将所有备用数字从右侧开始相加

5 0 1 2 3 4 5 7 6 4 2 1

0 + 2 + 4 + 7 + 4 + 1 = 18

第 2 步:将答案乘以 3

18 x 3 = 54

第 3 步:现在将剩余的数字相加

5 0 1 2 3 4 5 7 6 4 2 1

5 + 1 + 3 + 5 + 6 + 2 = 22

第 4 步:将第 2 步和第 3 步相加

54 + 22 = 76

第5步:第4步与下一个10号的区别:

76 + 4 = 80

校验位 = 4

于 2018-02-16T22:04:05.383 回答