在 C# 中创建 Code128 条形码

Mad*_*ads 3 c# barcode code128 asp.net-core

我真傻,我以为只要用条形码字体写一些文本,扫描仪就能读取它。看来我错了。

因此,在阅读了一些有关 code128 条形码的文档后,我了解到:

  1. 条形码以(103、104 或 105,取决于类型)开头
  2. 然后是字符串本身
  3. 然后是字符串中每个字符的计算总和乘以其位置的模 103
  4. 然后附加106

我的代码是:

public string Str = "MADS";
public string Barcode = null;

public void OnGet()
{

    int start = 104;
    int end = 106;
    int calc = start;
    Barcode = start.ToString();
    for (var i = 0; i < Str.Length; i++)
    {
        calc += (Convert.ToChar(Str[i]) - 32) * (i + 1);
        Barcode += Str[i];
    }

    double rem = calc % 103;
    Barcode += Convert.ToChar((int)rem + 32).ToString() + end;

    Console.WriteLine(Barcode);

}
Run Code Online (Sandbox Code Playgroud)

我不确定条形码字符串中应包含多少内容,以便扫描仪读取?:

  • 玛兹,
  • 104MADS,
  • 104MADS,106

还是我完全搞错了?

我的参考资料是:

链接 1 链接 2

特别是“Link 1”,因为我已经用扫描仪测试了该结果,并且它有效。遗憾的是我无法让我的输出看起来像那样。

结论

阅读评论和答案后,我认为对我来说最好的方法是使用现有的库。

我选择 NetBarcode GitHub 链接,因为它与 .Net Core 兼容。

Bri*_*son 5

如果您使用的是条形码字体,则需要在发送到输出的字符串中包含开始字符、数据字符串、校验和和停止字符,因此示例中的最后一个字符是正确的。该字体仅查找每个符号并为您绘制条形和空格。它对条形码符号系统和规则本身一无所知。

\n\n

\r\n
\r\n
var buttonGen = document.getElementById("btnGen");\r\nbuttonGen.onclick = function () {\r\n  var x = document.getElementById("textIn").value;\r\n  var i, j, intWeight, intLength, intWtProd = 0, arrayData = [], fs;\r\n  var arraySubst = [ "\xc3\x83", "\xc3\x84", "\xc3\x85", "\xc3\x86", "\xc3\x87", "\xc3\x88", "\xc3\x89", "\xc3\x8a" ];\r\n\r\n/*\r\n * Checksum Calculation for Code 128 B\r\n */\r\n  intLength = x.length;\r\n\tarrayData[0] = 104; // Assume Code 128B, Will revise to support A, C and switching.\r\n\tintWtProd = 104;\r\n\tfor (j = 0; j < intLength; j += 1) {\r\n\t\t\tarrayData[j + 1] = x.charCodeAt(j) - 32; // Have to convert to Code 128 encoding\r\n\t\t\tintWeight = j + 1; // to generate the checksum\r\n\t\t\tintWtProd += intWeight * arrayData[j + 1]; // Just a weighted sum\r\n\t}\r\n\tarrayData[j + 1] = intWtProd % 103; // Modulo 103 on weighted sum\r\n\tarrayData[j + 2] = 106; // Code 128 Stop character\r\n  chr = parseInt(arrayData[j + 1], 10); // Gotta convert from character to a number\r\n  if (chr > 94) {\r\n    chrString = arraySubst[chr - 95];\r\n  } else {\r\n    chrString = String.fromCharCode(chr + 32);\r\n  }\r\n  \r\n  // Change the font-size style to match the drop down\r\n  fs = document.getElementsByTagName("option")[document.getElementById("selList").selectedIndex].value;\r\n  document.getElementById("test").style.fontSize = fs  + \'px\';\r\n  \r\n  document.getElementById("check").innerHTML =\r\n    \'Checksum = \' + chr + \' or character \' + // Make It Visual\r\n    chrString + \', for text = "\' + x + \'"\';\r\n  \r\n  document.getElementById("test").innerHTML =\r\n    \'\xc3\x8c\' + // Start Code B\r\n    x + // The originally typed string\r\n    chrString + // The generated checksum\r\n    \'\xc3\x8e\'; // Stop Code\r\n}
Run Code Online (Sandbox Code Playgroud)\r\n
<html>\r\n  <head>\r\n    <link href="https://fonts.googleapis.com/css?family=Libre+Barcode+128+Text" rel="stylesheet">\r\n    <style>\r\n      td, th {\r\n        text-align: center;\r\n        padding: 6px;\r\n      }\r\n\r\n      .ss {\r\n        font-family: \'Libre Barcode 128 Text\', cursive;\r\n        font-size: 24px;\r\n      }\r\n    </style>\r\n  </head>\r\n  <body>\r\n    &nbsp;Font Size:&nbsp;\r\n    <select id="selList">\r\n      <option value="24" selected>24px</option>\r\n      <option value="30">30px</option>\r\n      <option value="36">36px</option>\r\n      <option value="42">42px</option>\r\n      <option value="48">48px</option>\r\n      <option value="54">54px</option>\r\n      <option value="60">60px</option>\r\n      <option value="66">66px</option>\r\n      <option value="72">72px</option>\r\n      <option value="78">78px</option>\r\n      <option value="84">84px</option>\r\n      <option value="90">90px</option>\r\n      <option value="96">96px</option>\r\n    </select>&nbsp;\r\n\r\n    <input type="text" id="textIn"></input>\r\n    <input type="button" id="btnGen" value="Generate Code 128 Checksum" tabindex=4/>\r\n    <div id="check"></div><br /><span id="test" class="ss">\xc3\x8cMaking the Web Beautiful!$\xc3\x8e</span><br />\r\n    <p>This is a demonstration of use of the Libre Barcode 128 Font.</p>\r\n    <p>Because the Libre Barcode Code 128 font does not generate checksums, you need this component to produce a scanning barcode.</p>\r\n    <p>To use, just enter the text you want to embed in the barcode and press the generate button. Happy barcoding!</p>\r\n    <p>By the way, Libre Barcode 128 Font uses the following high ASCII / unicode characters to implement the control codes symbols. (This is essentially adding 100 to the value, in other words \'\xc3\x83\' is U+00C3 (195) to \'\xc3\x8e\' is U+00CE (206).)</p>\r\n<table border="3">\r\n    <tr>\r\n    <th>Value</th>\r\n    <th>Encoding</th>\r\n    <th>Subst</th>\r\n  </tr>\r\n<tr><td> 95</td><td>A: US, B: DEL, C: 95</td><td>\xc3\x83</td></tr>\r\n<tr><td> 96</td><td>A: FNC 3, B: FNC 3, C: 96</td><td>\xc3\x84</td></tr>\r\n<tr><td> 97</td><td>A: FNC 2, B: FNC 2, C: 97</td><td>\xc3\x85</td></tr>\r\n<tr><td> 98</td><td>A: Shift B, B: Shift A, C: 98</td><td>\xc3\x86</td></tr>\r\n<tr><td> 99</td><td>A: Code C, B: Code C, C: 99</td><td>\xc3\x87</td></tr>\r\n<tr><td>100</td><td>A: Code B, B: FNC 4, C: Code B</td><td>\xc3\x88</td></tr>\r\n<tr><td>101</td><td>A: FNC 4, B: Code A, C: Code A</td><td>\xc3\x89</td></tr>\r\n<tr><td>102</td><td>A: FNC 1, B: FNC 1, C: FNC 1</td><td>\xc3\x8a</td></tr>\r\n<tr><td>103</td><td>Begin Code A</td><td>\xc3\x8b</td></tr>\r\n<tr><td>104</td><td>Begin Code B</td><td>\xc3\x8c</td></tr>\r\n<tr><td>105</td><td>Begin Code C</td><td>\xc3\x8d</td></tr>\r\n<tr><td>106</td><td>Stop Code</td><td>\xc3\x8e</td></tr></table>\r\n  </body>\r\n</html>
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

  • 谢谢你,布莱恩。虽然我最终使用了现有的库,但这是原始问题的答案,因此这将是正确的答案。 (2认同)