生成 .NET EAN-13 字符串

Del*_*ela 2 .net barcode ean-13

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

我不需要图像,只需要字符串。

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

它可以将国家/地区转换为数字系统,将其连接在一起并计算校验和。

Jes*_*ter 5

基本上: 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}";
    }
Run Code Online (Sandbox Code Playgroud)

如何自己计算校验位

条形码编号示例: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

第三步:现在将剩余的数字加在一起

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