如何生成条形码校验和

时间:2018-08-06 16:11:06

标签: barcode barcode-scanner checksum

我有一个带有字符的条形码; 34166110860346bNq(另请参见下图)。
我希望能够:
1.识别条形码的类型和
2.使用什么公式生成校验和数字。 (最后3位数字是校验和bNq)

image of barcode

2 个答案:

答案 0 :(得分:1)

我在此代码段中复制了上面的Code 128条码,并且所生成的校验和为整数98。尾随字符都不是校验和的一部分。它们将全部由扫描仪生成为扫描文本。

<html>
<head>
<title>Plain Vanilla JS Code 128B Barcodes</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>
<div id="result"></div>
<script type="text/javascript">
// The MIT License (MIT)
// Copyright (c) 2017, Notionovus, LLC.

var arrayCode128Bin = [ '11011001100', '11001101100', '11001100110', '10010011000', '10010001100', '10001001100', '10011001000', '10011000100', '10001100100', '11001001000', '11001000100', '11000100100', '10110011100', '10011011100', '10011001110', '10111001100', '10011101100', '10011100110', '11001110010', '11001011100', '11001001110', '11011100100', '11001110100', '11101101110', '11101001100', '11100101100', '11100100110', '11101100100', '11100110100', '11100110010', '11011011000', '11011000110', '11000110110', '10100011000', '10001011000', '10001000110', '10110001000', '10001101000', '10001100010', '11010001000', '11000101000', '11000100010', '10110111000', '10110001110', '10001101110', '10111011000', '10111000110', '10001110110', '11101110110', '11010001110', '11000101110', '11011101000', '11011100010', '11011101110', '11101011000', '11101000110', '11100010110', '11101101000', '11101100010', '11100011010', '11101111010', '11001000010', '11110001010', '10100110000', '10100001100', '10010110000', '10010000110', '10000101100', '10000100110', '10110010000', '10110000100', '10011010000', '10011000010', '10000110100', '10000110010', '11000010010', '11001010000', '11110111010', '11000010100', '10001111010', '10100111100', '10010111100', '10010011110', '10111100100', '10011110100', '10011110010', '11110100100', '11110010100', '11110010010', '11011011110', '11011110110', '11110110110', '10101111000', '10100011110', '10001011110', '10111101000', '10111100010', '11110101000', '11110100010', '10111011110', '10111101110', '11101011110', '11110101110', '11010000100', '11010010000', '11010011100', '1100011101011', '11010111000'];
var array5bit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VICAyQ+UJI', 'TTECAq7UnEM', 'TUoCA+Jw8kA', 'ZjUDAmZGozo', 'TU4CA7CME0s', 'ajUDAvnk9E4', 'ak4DA7VAmk0', 'ZjkDAtle3bI', 'TVICAxOyzrM', 'STUCAqHeHtM', 'SU4CA+16cNA', 'h6QEAZKdo54', 'SVICA62zYxM', 'RTkCAqx1lb4', 'RVICA/z3WM0', 'QT0CAkdoxRU', 'KFYBA46vJCA'];
var stringStart = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/A';
var stringMid = 'AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAA';
var stringEnd = 'AAAAASUVORK5CYII=" style="width:';

function genBarcode(inputString, intWidth, intHeight) {
var arraySeq = [], i, intChunks, resultString;
var intRawmod = inputString.length % 5;
  for (i = 0; i < 5 - intRawmod; i += 1) {
    inputString += "0";
  }
  intChunks = inputString.length / 5;
  for (i = 0; i < intChunks; i += 1) {
    arraySeq[i] = parseInt(inputString.substr(i * 5, 5), 2);
  }
  resultString = "";
  for (i = 0; i < arraySeq.length; i += 1) {
    resultString += stringStart + array5bit_A[arraySeq[i]] + stringMid + array5bit_B[arraySeq[i]] + stringEnd + intWidth + 'px;height:' + intHeight + 'px;">';
  }
  return resultString;
}

function funcCode128C(strText) {
  'use strict';
let i, j, intWeight, intLength, intWtProd = 0;
let arrayData = [];
let strRaw = [];

	intLength = strText.length;
	intWtProd = arrayData[0] = 105; // Start with Code 128C
	console.log(`Value ${arrayData[0]}, Multiplier 1, Cumulative ${intWtProd}`);
	for (i = 0, j = 0; i < intLength; i += 2, j += 1) {
			arrayData[j + 1] = parseInt(strText.substr(i, 2), 10);
			intWeight = j + 1;
			intWtProd += intWeight * arrayData[j + 1];
    	console.log(`Value ${arrayData[j + 1]}, Multiplier ${j + 1}, Cumulative ${intWtProd}`);
	}
	arrayData[++j] = 100; // Switch to Code 128B
			intWtProd += j * arrayData[j];
    	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
	arrayData[++j] = 66; // Lowercase 'b'
			intWtProd += j * arrayData[j];
    	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
	arrayData[++j] = 46; // Uppercase 'N'
			intWtProd += j * arrayData[j];
    	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
	arrayData[++j] = 81; // Lowercase 'q'
			intWtProd += j * arrayData[j];
    	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
	arrayData[++j] = intWtProd % 103; // Checksum will be the integer 98 for this example
	console.log("Checksum = ", arrayData[j]);
	arrayData[++j] = 106;
	strRaw = "";
	for (i = 0; i < arrayData.length; i += 1) {
		strRaw += arrayCode128Bin[arrayData[i]];
	}
	return strRaw;
} // End Code 128C

document.getElementById("result").innerHTML = genBarcode(funcCode128C("34166110860346"), 18.3, 50);
</script>
</body>
</html>

如果您对如何计算校验和感到好奇,请将“字符”乘以它们在代码128C / B图表here中出现的加权总和,然后将数字的余数除以103.我已将过程记录到控制台。

答案 1 :(得分:0)

您可以使用条形码阅读器的硬件进行读取。 大多数条形码阅读器可以选择通知表示条形码类型的字母。 请参阅条形码阅读器的手册来使用。

根据每种条形码类型,校验位计算方法不同。另外,根据条形码的类型来确定是否存在校验位和是否存在向应用程序的校验位通知。但是,在某些情况下,选项是由用户决定的。

如果您想了解它们,请参阅ISO等标准文档。