【问题标题】:Barcode Code 39 javascript encoding条码代码 39 javascript 编码
【发布时间】:2016-05-03 12:51:17
【问题描述】:

我正在尝试通过修改一些 D3 javascript 来对代码 39 条码进行编码。

问题是我不明白 code 39 条码是如何编码的。看起来他们使用 12 位二进制对字符进行编码。

如何将字符串(如 '598649')解码为 12 位二进制等价物,最好使用 D3.js?

characters = {
'100100100101': '$',
'100100101001': '/',
'100101001001': '+',
'100101011011': '-',
'100101101011': 'X',
'100101101101': '*',
'100110101011': 'V',
'100110101101': ' ',
'100110110101': 'Z',
'101001001001': '%',
'101001011011': '7',
'101001101011': '4',
'101001101101': '0',
'101010011011': 'G',
'101010110011': 'Q',
'101011001011': 'D',
'101011001101': 'J',
'101011010011': 'N',
'101011011001': 'T',
'101100101011': '2',
'101100101101': '9',
'101100110101': '6',
'101101001011': 'B',
'101101001101': 'I',
'101101010011': 'L',
'101101011001': 'S',
'101101100101': 'F',
'101101101001': 'P',
'110010101011': 'U',
'110010101101': '0',
'110010110101': 'Y',
'110011010101': 'W',
'110100101011': '1',
'110100101101': '8',
'110100110101': '5',
'110101001011': 'A',
'110101001101': 'H',
'110101010011': 'K',
'110101011001': 'R',
'110101100101': 'E',
'110101101001': 'O',
'110110010101': '3',
'110110100101': 'C',
'110110101001': 'M',
}

这是迄今为止我一直在使用的一些代码。前面的代码生成随机二进制然后将键与值匹配。我想做相反的事情,所以我不确定我是否应该只创建第二个键/值反转的数组。

    //var num= prompt("Enter num")
    //console.log(parseInt(num, 10).toString(2))

    //var decnum=892938
    //alert(decnum.toString(2))

    // var b = parseInt( a, 2 )




    // var str = '34566'
    // var found = str.match(/[01]{12}/g).map(['110100101101','101100101101', '101100101011', '101100101101', '110110010101', '110100101101'])

    //str.match(/[01]{12}/g).map(characters)
    //console.log(characters[v])
    //console.log(found)


    var str = ['110100101101','101100101101', '101100101011', '101100101101', '110110010101', '110100101101']
    //var barcode_array = str.match(/[01]{12}/g).map(Object.keys(characters))

    //console.log(barcode_array)

    //split into array
    var barcode_input = ['110100101101','101100101101', '101100101011', '101100101101', '110110010101', '110100101101']

    //map to binary characters
    //console.log(barcode_input.range(Object.keys(characters)))

这是小提琴 https://jsfiddle.net/6szmwkgm/6/

代码修改自 Christopher Manning http://bl.ocks.org/christophermanning/4324236 作为参考,这里是另一个代码 39 脚本http://notionovus.com/blog/code-39-barcode-biscuits/

【问题讨论】:

    标签: javascript d3.js binary barcode code39


    【解决方案1】:

    您正在构建的编码可以通过使用characters 对象作为简单的查找表来完成。获取每个字符并查找它的线条图案,构建一个数组,然后处理它们。

    您正在处理的代码比您似乎需要的要多得多。我用它作为起点,然后将其削减到最低限度。

    注意,我没有方便的扫描仪来测试这个,所以如果它给你带来任何麻烦,请告诉我。

    这是一个注释代码sn-p:

    <!DOCTYPE html>
    <html>
    
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
      <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    
      <style type="text/css">
        body {
          display: block;
          margin: 0;
          background-color: #FFF;
        }
        
        text {
          font-family: monospace;
          font-size: 1.5em;
          text-anchor: middle;
        }
        
        path {
          fill-opacity: 0;
        }
      </style>
    </head>
    
    <body>
      <script type="text/javascript">
    
    		var svg = d3.select("body")
    		  .append("svg")
    		  .attr("width", 300)
    		  .attr("height", 300);
    
            // input barcode
    		var barcode = "598649";
    
            // some configuration items
    		var config = {
    		  "height": 100,
    		  "x": 10,
    		  "margin": 20
    		};
        
            // map each character to it's pattern
            // note I reversed it from the sample
            // to make lookups on each letter easier
    		var characters = {
    		  "0": "110010101101",
    		  " ": "100110101101",
    		  "1": "110100101011",
    		  "2": "101100101011",
    		  "3": "110110010101",
    		  "4": "101001101011",
    		  "5": "110100110101",
    		  "6": "101100110101",
    		  "7": "101001011011",
    		  "8": "110100101101",
    		  "9": "101100101101",
    		  "$": "100100100101",
    		  "%": "101001001001",
    		  "*": "100101101101",
    		  "+": "100101001001",
    		  "-": "100101011011",
    		  "/": "100100101001",
    		  "A": "110101001011",
    		  "B": "101101001011",
    		  "C": "110110100101",
    		  "D": "101011001011",
    		  "E": "110101100101",
    		  "F": "101101100101",
    		  "G": "101010011011",
    		  "H": "110101001101",
    		  "I": "101101001101",
    		  "J": "101011001101",
    		  "K": "110101010011",
    		  "L": "101101010011",
    		  "M": "110110101001",
    		  "N": "101011010011",
    		  "O": "110101101001",
    		  "P": "101101101001",
    		  "Q": "101010110011",
    		  "R": "110101011001",
    		  "S": "101101011001",
    		  "T": "101011011001",
    		  "U": "110010101011",
    		  "V": "100110101011",
    		  "W": "110011010101",
    		  "X": "100101101011",
    		  "Y": "110010110101",
    		  "Z": "100110110101"
    		};
    
            // build you data array
    		var data = [];
    		// Code 39 barcodes start with a *
    		data.push('100101101101');
    		barcode.split("").forEach(function(d, i) {
              // for each character, append the patter to our array
    		  data.push(characters[d]); //<-- look up for each character
    		});
    		// Code 39 barcodes end with a *
    		data.push('100101101101');
    		
            // set up line function
    		var line = d3.svg.line()
    		  .x(function(d) {
    			return d.cx = d.x
    		  })
    		  .y(function(d) {
    			return d.cy = d.y
    		  })
    		  
            // group all the barcode paths
    		var g = svg.append('g')
    		  .attr('class', 'barcode');
    
            // set up the data attributes necessary
    		var path = g.selectAll('path')
    		  .data(data.map(function(c) {
    			  return c + '0' // put a space between each character
    			}).join('').split('')
    			.map(function(d, i) {
    			  return [{
    				c: d,
    				x: config['margin']+ (i * 1.2),
    				y: config['margin']
    			  }, {
    				c: d,
    				x: config['margin'] + (i * 1.2),
    				y: config['margin'] + config['height']
    			  }]
    			})
    		  );
    
            
    		path.enter()
    		  .append('path');
    
            // draw the barcode
    		path
    		  .style("stroke", function(d) {
    			return d[0].c == '1' ? '#000' : '#FFF'
    		  })
    		  .style("stroke-width", 1.2)
    		  .attr("d", line);
    
    		path.exit().remove();
    		
            // add the text underneath
    		var text = svg.selectAll('text')
    		  .data([barcode])
    		
    		text.exit().remove();
    		
    		text.enter()
    		  .append('text');
    		
    		text
    		  .text(barcode)
    		  .style('fill', 'black')
    		  .attr('y', config.margin + config.height + 15)
          .attr('x', g.node().getBBox().width/2 + config.margin);
    
      </script>
    </body>
    
    </html>

    【讨论】:

    • 我认为反转数组中的键/值是一种方法,因为它不再生成代码。我还在学习D3,谢谢你的帮助!
    • 我还发现你在二进制值中将零换成了一段时间。
    • @michael,期间为零?请详细说明。
    • 基本上您只是不小心将"." = "110010101101" 的二进制值切换为"0" = "101001101101" — 我在使用条形码扫描仪扫描时发现了这一点。不用担心。
    • 这是我的 JSFiddle,编码正确。 jsfiddle.net/michaelreynolds/661rzty8
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2018-01-09
    相关资源
    最近更新 更多