【问题标题】:Creating Code128 barcode in C#在 C# 中创建 Code128 条形码
【发布时间】:2020-06-06 04:07:01
【问题描述】:

我以为我很愚蠢,只要用条形码字体写一些文本,就可以让扫描仪读取它。看来我错了。

所以在阅读了一些关于 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);

}

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

  • MADS,
  • 104MADS,
  • 104MADS,106

还是我都搞错了?

我的参考资料是:

Link 1 Link 2

尤其是“链接 1”,因为我已经使用扫描仪测试了该结果,并且它有效。 可悲的是,我无法让我的输出看起来像那样。

结论

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

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

【问题讨论】:

  • 我已经编写了一个 Code128 编码算法,它输出白/黑条的布尔数组 - 您缺少编码表。每个字符被编码为一组 11 个白/黑“块”,代表黑/白条的相对厚度。 Code128 还有一些额外的注意事项,例如切换集。有 3 个集合,A/B/C 可以更有效地编码不同的字符(集合 C 用于编码数字对,因此如果包含很多数字,可以大大缩短条形码长度)。
  • 您也不想要文字字符串“104”等 - 您选择具有第一个字节(103、104、105)的字符集,它们分别代表 Set A/B/C 的开始.应该在编码表中查找该字节以确定构成编码的条的宽度。例如,char 103 = StartA 表示使用字符集 A(小写 ascii)的条形码的开头。这被编码为11010000100,这意味着一条 2 宽的粗线,然后是 1 宽的粗间隙,然后是 1 宽的粗线,依此类推。
  • 另外,假设您仍然想自己执行此操作并且不使用外部库/服务,您可以在输出中使用 RLE 压缩来保持文件小而不需要任何外部库(您可以只写写入条形码数据时使用 RLE 编码到磁盘的位图)。输出具有 1 粗线的位图将很难扫描,将其放大倍数是可行的,但文件大小会很快变大,因此请压缩。这一切都假设您仍然无法使用其他方法,例如 Web 服务或库。
  • @Charleh 感谢您的意见。在阅读了您的 cmets 之后,我相信对我来说最好的方法是使用现有的库。
  • 没有问题,仅供参考,我别无选择,因为它是基于云的 ERP 系统,它不允许您与任何编程语言进行互操作。我本可以使用 Azure Functions,但我喜欢挑战,而且实现起来很有趣 - 另外,我想要一个版本,它在条形码至关重要的情况下没有任何外部依赖项,并且始终 100% 工作而无需外部服务100% 可用。

标签: c# asp.net-core barcode code128


【解决方案1】:

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

var buttonGen = document.getElementById("btnGen");
buttonGen.onclick = function () {
  var x = document.getElementById("textIn").value;
  var i, j, intWeight, intLength, intWtProd = 0, arrayData = [], fs;
  var arraySubst = [ "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê" ];

/*
 * Checksum Calculation for Code 128 B
 */
  intLength = x.length;
	arrayData[0] = 104; // Assume Code 128B, Will revise to support A, C and switching.
	intWtProd = 104;
	for (j = 0; j < intLength; j += 1) {
			arrayData[j + 1] = x.charCodeAt(j) - 32; // Have to convert to Code 128 encoding
			intWeight = j + 1; // to generate the checksum
			intWtProd += intWeight * arrayData[j + 1]; // Just a weighted sum
	}
	arrayData[j + 1] = intWtProd % 103; // Modulo 103 on weighted sum
	arrayData[j + 2] = 106; // Code 128 Stop character
  chr = parseInt(arrayData[j + 1], 10); // Gotta convert from character to a number
  if (chr > 94) {
    chrString = arraySubst[chr - 95];
  } else {
    chrString = String.fromCharCode(chr + 32);
  }
  
  // Change the font-size style to match the drop down
  fs = document.getElementsByTagName("option")[document.getElementById("selList").selectedIndex].value;
  document.getElementById("test").style.fontSize = fs  + 'px';
  
  document.getElementById("check").innerHTML =
    'Checksum = ' + chr + ' or character ' + // Make It Visual
    chrString + ', for text = "' + x + '"';
  
  document.getElementById("test").innerHTML =
    'Ì' + // Start Code B
    x + // The originally typed string
    chrString + // The generated checksum
    'Î'; // Stop Code
}
<html>
  <head>
    <link href="https://fonts.googleapis.com/css?family=Libre+Barcode+128+Text" rel="stylesheet">
    <style>
      td, th {
        text-align: center;
        padding: 6px;
      }

      .ss {
        font-family: 'Libre Barcode 128 Text', cursive;
        font-size: 24px;
      }
    </style>
  </head>
  <body>
    &nbsp;Font Size:&nbsp;
    <select id="selList">
      <option value="24" selected>24px</option>
      <option value="30">30px</option>
      <option value="36">36px</option>
      <option value="42">42px</option>
      <option value="48">48px</option>
      <option value="54">54px</option>
      <option value="60">60px</option>
      <option value="66">66px</option>
      <option value="72">72px</option>
      <option value="78">78px</option>
      <option value="84">84px</option>
      <option value="90">90px</option>
      <option value="96">96px</option>
    </select>&nbsp;

    <input type="text" id="textIn"></input>
    <input type="button" id="btnGen" value="Generate Code 128 Checksum" tabindex=4/>
    <div id="check"></div><br /><span id="test" class="ss">ÌMaking the Web Beautiful!$Î</span><br />
    <p>This is a demonstration of use of the Libre Barcode 128 Font.</p>
    <p>Because the Libre Barcode Code 128 font does not generate checksums, you need this component to produce a scanning barcode.</p>
    <p>To use, just enter the text you want to embed in the barcode and press the generate button. Happy barcoding!</p>
    <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 'Ã' is U+00C3 (195) to 'Î' is U+00CE (206).)</p>
<table border="3">
    <tr>
    <th>Value</th>
    <th>Encoding</th>
    <th>Subst</th>
  </tr>
<tr><td> 95</td><td>A: US, B: DEL, C: 95</td><td>Ã</td></tr>
<tr><td> 96</td><td>A: FNC 3, B: FNC 3, C: 96</td><td>Ä</td></tr>
<tr><td> 97</td><td>A: FNC 2, B: FNC 2, C: 97</td><td>Å</td></tr>
<tr><td> 98</td><td>A: Shift B, B: Shift A, C: 98</td><td>Æ</td></tr>
<tr><td> 99</td><td>A: Code C, B: Code C, C: 99</td><td>Ç</td></tr>
<tr><td>100</td><td>A: Code B, B: FNC 4, C: Code B</td><td>È</td></tr>
<tr><td>101</td><td>A: FNC 4, B: Code A, C: Code A</td><td>É</td></tr>
<tr><td>102</td><td>A: FNC 1, B: FNC 1, C: FNC 1</td><td>Ê</td></tr>
<tr><td>103</td><td>Begin Code A</td><td>Ë</td></tr>
<tr><td>104</td><td>Begin Code B</td><td>Ì</td></tr>
<tr><td>105</td><td>Begin Code C</td><td>Í</td></tr>
<tr><td>106</td><td>Stop Code</td><td>Î</td></tr></table>
  </body>
</html>

【讨论】:

  • 谢谢你,布赖恩。虽然我最终使用了现有的库,但这是原始问题的答案,所以这将是正确的答案。
  • 如果“只使用 X 库”是每个问题的答案,那么 Stack Overflow 就不需要存在了。由于法律和技术原因,有成千上万的程序员无法使用库。感谢您的支持。
【解决方案2】:

检查这个库:https://github.com/barnhill/barcodelib

在 Nuget 上:https://github.com/barnhill/barcodelib

它将生成一个包含条形码的图像,该条形码读取为输入中的文本。 使用示例:

BarcodeLib.Barcode b = new BarcodeLib.Barcode();
Image img = b.Encode(BarcodeLib.TYPE.CODE128, "038000356216", Color.Black, Color.White, 290, 120);

【讨论】:

  • 无论如何,Google 都可以轻松找到此类库。除此之外,您能否在答案中提供任何价值?
  • 用 Code 128 条码生成的基本用法示例更新了答案。
猜你喜欢
  • 2019-03-13
  • 2018-07-08
  • 2021-04-04
  • 2017-06-19
  • 2017-09-24
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
相关资源
最近更新 更多