【问题标题】:How can I generate an EAN 13 barcode shorter in length?如何生成长度更短的 EAN 13 条码?
【发布时间】:2021-08-27 01:18:30
【问题描述】:

我使用 IDAutomationHC39M 字体生成了 EAN 13 条形码。我发现我生成的这个条码很长,所以每个条都很细。但是,我发现有人的 EAN 13 条码长度更短,每个条码的密度更高。我怎样才能达到相同的结果?谢谢。

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: barcode


【解决方案1】:

图片将帮助我们了解您想要做什么。

EAN 13 不使用 IDAutomationHC39M 生成的代码 39 符号系统。取而代之的是,EAN 13 使用类似于 UPC 符号的二进制编码方案,该方案对一系列条形和空格进行编码,这些条形和空格取决于符号中的位置和所表示的数字。

这是更复杂的符号之一。您可以利用以下代码使用客户端 JavaScript 创建您自己的 EAN 13。

// The MIT License (MIT)

// Copyright (c) 2017, Notionovus, LLC.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// Generic arrays for drawing 5-bit graphics. Building blocks for all barcode symbologies
// Painstakingly derived gobblety-goop, but essentially the two middle sections of image data unique to each graphic
var array5bit_A = new Array ( 'f//AAAAAAAAAAAAAAAAAAAA', 'f//AAAAAAAAAAAAAAAAAAAB', 'f//AAAAAAAAAAAAAAEAAAD/',
 'f//AAAAAAAAAAAAAAEAAAAA', 'f//AAAAAAAAAQAAAP8AAAAA', 'f//AAAAAAAAAQAAAP8AAAAB', 'f//AAAAAAAAAQAAAAAAAAD/',
 'f//AAAAAAAAAQAAAAAAAAAA', 'f//AAABAAAA/wAAAAAAAAAA', 'f//AAABAAAA/wAAAAAAAAAB', 'f//AAABAAAA/wAAAAEAAAD/',
 'f//AAABAAAA/wAAAAEAAAAA', 'f//AAABAAAAAAAAAP8AAAAA', 'f//AAABAAAAAAAAAP8AAAAB', 'f//AAABAAAAAAAAAAAAAAD/',
 'f//AAABAAAAAAAAAAAAAAAA', 'QD/AAD/AAAAAAAAAAAAAAAA', 'QD/AAD/AAAAAAAAAAAAAAAB', 'QD/AAD/AAAAAAAAAAEAAAD/',
 'QD/AAD/AAAAAAAAAAEAAAAA', 'QD/AAD/AAAAAQAAAP8AAAAA', 'QD/AAD/AAAAAQAAAP8AAAAB', 'QD/AAD/AAAAAQAAAAAAAAD/',
 'QD/AAD/AAAAAQAAAAAAAAAA', 'QD/AAAAAAAA/wAAAAAAAAAA', 'QD/AAAAAAAA/wAAAAAAAAAB', 'SL/AADeAAAA/gAAAAIAAAD+',
 'QD/AAAAAAAA/wAAAAEAAAAA', 'QD/AAAAAAAAAAAAAP8AAAAA', 'QD/AAAAAAAAAAAAAP8AAAAB', 'QD/AAAAAAAAAAAAAAAAAAD/',
 'QD/AAAAAAAAAAAAAAAAAAAA');
var array5bit_B = new Array ( 'US0CAuSD38g', 'UUYCA7QBErs', 'ajEDAm49ReY', 'UUoCA+juogg', 'bjEDAjQrOn0', 'bkoDA3iPVH4',
 'ajUDAt82atY', 'UU4CA1nljTg', 'cjEDAghkmFU', 'ckoDA0TA9lY', 'izUEAhrxcbg', 'ck4DAxY8F10', 'bjUDAlvFFR8', 'bk4DAxdhexw',
 'ajkDAr7LFAw', 'UVICAyQ+UJI', 'TTECAq7UnEM', 'TUoCA+Jw8kA', 'ZjUDAmZGozo', 'TU4CA7CME0s', 'ajUDAvnk9E4', 'ak4DA7VAmk0',
 'ZjkDAtle3bI', 'TVICAxOyzrM', 'STUCAqHeHtM', 'SU4CA+16cNA', 'h6QEAZKdo54', 'SVICA62zYxM', 'RTkCAqx1lb4', 'RVICA/z3WM0',
 'QT0CAkdoxRU', 'KFYBA46vJCA');

// Painstakingly derived gobblety-goop, but essentially the front, back and mid-matter common to all barcode images...
var stringStart = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/A';
var stringMid = 'AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAA';
var stringEnd = 'AAAAASUVORK5CYII=" width="';

function genBarcode(inputString,intWidth,intHeight) { // Input is a long string of 1's and 0's, output is the HTML <img> stack
// Pads to the last character to ensure length is divisible by 5
   var intRawmod = inputString.length % 5; // Modulo 5 remainder
   if (intRawmod > 0) for (var i = 0; i < 5 - intRawmod; i++) inputString += "0"; // If not evenly divisible, pad with zeroes
   var arraySeq = new Array (intChunks = inputString.length / 5); // Create array for as many chunks as are now in input string

   for (var i = 0; i < intChunks; i++) arraySeq[i] = parseInt(inputString.substr(i * 5, 5), 2); // Converts string of 1's and 0's to integer array

// Takes integer array and converts to "<img ...>" graphics for display
   var resultString = "";
   for (var i = 0; i < arraySeq.length; i++) {
    resultString += stringStart + array5bit_A[arraySeq[i]] + stringMid + array5bit_B[arraySeq[i]] + stringEnd + intWidth + '" height="' + intHeight + '">';
   }
   return resultString;
}
///////////////////////////////////////////
// Symbology-specific arrays

// UPC Specific Arrays
var arrayCodeEANBin, arrayStructEAN;
arrayCodeEANBin = [ [ '0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011' ], [ '0100111', '0110011', '0011011', '0100001', '0011101', '0111001', '0000101', '0010001', '0001001', '0010111' ], [ '1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100' ] ];
arrayStructEAN = ['000000', '001011', '001101', '001110', '010011', '011001', '011100', '010101', '010110', '011010'];

///////////////////////////////////////////
// Global Variables
var strRaw = "";
var strText = "";

///////////////////////////////////////////
// Symbology-specific functions
function funcEAN() { // EAN-13
var intSumOdd = 0, intSumEven = 0, intCheck, i, j, strStruct;

// Compute check digit and add it to raw string
 for (i = 0; i < 12; i += 2) {
  intSumEven += parseInt(strText[i]);
  intSumOdd += parseInt(strText[i+1]);
 }
 intCheck = ((intSumOdd * 3) + intSumEven) % 10;
 if (intCheck > 0) {
    intCheck = 10 - intCheck;
 }
 strText += intCheck;

// Converts Code EAN array into string of 1's and 0's
 strRaw = "101";
// First six bar sequences
 strStruct = arrayStructEAN[strText[0]];
 for (i = 1; i < 7; i += 1) {
   strRaw += arrayCodeEANBin[strStruct[i-1]][strText[i]];
 }
// Middle sequence
 strRaw += "01010";
// Last six bar sequences, including check digit
 for (i = 0; i < 6; i += 1) {
  strRaw += arrayCodeEANBin[2][strText[i+7]];
 }
 strRaw += "101";
} // End EAN-13


var buttonBarcode = document.getElementById("btnGenBar");
buttonBarcode.onclick = function () {
 var intHt = intWd = 0;
 var strImages = "";
 document.getElementById("textImages").value = strImages;
 intWd = document.getElementById("textWidth").value;
 intHt = document.getElementById("textHeight").value;
 strText = document.getElementById("textBarcode").value;
 funcEAN();
 document.getElementById("result").innerHTML = strImages = genBarcode(strRaw,intWd,intHt);
 document.getElementById("textImages").value = strImages;
 document.getElementById("textRaw").value = strRaw;
 document.getElementById("textImages").select();
}
<head>
<title>EAN-13 Barcodes in vanilla JavaScript</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>
  <h1>EAN-13 Barcodes written in vanilla JavaScript</h1>
  <h4>Enter text (a 12-digit number), enter a non-ridiculous height in pixels, enter a width between 4 (so small) and 40 (gigantor) and press the button. Magic will ensue. Print page or copy HTML out of box and paste it where it will do some good.</h4>  
 <div id="inputForm">
    Enter Text:&nbsp;<input type="text" id="textBarcode" placeholder="12 Digit Number" tabindex=1/>
    &nbsp;Height:&nbsp;<input type="text" id="textHeight" size="3" placeholder="40-100" maxlength="5" tabindex=2/>
    &nbsp;Width:&nbsp;<input type="text" id="textWidth" size="3" placeholder="4.0-40.9" maxlength="5" tabindex=3/>
    &nbsp;
    <input type="button" id="btnGenBar" value="Generate Barcode" tabindex=4/>
 </div>
 <p></p>
 <div id="result"></div>
 <p></p>
 <textarea rows="30" cols="110" id="textImages" tabindex=0></textarea>
 <p></p>
 <textarea rows="3" cols="110" id="textRaw" tabindex=0></textarea>
<script type="text/javascript" src="./JS-EAN-13.js"></script>
</body>
</html>

【讨论】:

  • 嗨,布赖恩,非常感谢您的帮助!对不起,我不是 IT 人,我不知道编码。无论如何,非常感谢您的帮助。
猜你喜欢
  • 2018-06-29
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多