生成 .NET EAN-13 字符串

generating .NET EAN-13 string

有人可以推荐一个(免费用于商业用途)库来生成 EAN-13 条形码吗?

我不需要图片,只需要一个字符串。

我想传递产品代码、制造商代码和country/region(不是数字系统)作为参数。

它可以将 country/region 转换为数字系统,将它们连接在一起并计算校验和。

基本上: 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