GS1 128条形码解码器(AI),用于PHP或Jquery

时间:2016-08-05 10:54:31

标签: javascript php jquery barcode

我正在开发一个需要读取条形码(如GS1 128)的项目,我想将它们分成应用程序标识符(AI)。我正在使用名为Bark.js的图书馆。

使用AI,如01,02,15,17,10,它可以正常使用一些条形码;但现在我找到的条形码如下:

(02)98428844551697 (37)0100(3103)022700 (15)180205(10)05165

好的,我们不考虑括号,因为它们只出现在人类可读的部分。

我假设您知道某些AI具有可变长度,在这种情况下(37)和(10)分别具有2-8到2-20位数字。我对(10)没问题,因为它几乎在所有情况下都是结束。但是(37)可以出现在中间或最后。

我修补了Bark.js图书馆,但我没有采用可变长度的这种控制。

我搜索了几个星期而且我只找到了生成条形码或读取图像的库,但他们没有读取条形码并处理它将所有AI分开。

知道这一切,Javascript / Jquery或PHP中是否还有其他库来控制所有这些情况?

PS:对不起我的英语,我很乐意回答任何问题。

1 个答案:

答案 0 :(得分:1)

总而言之,事实证明您需要在GS1 128条形码内解码应用程序标识符(AI),而条形码缺少其不可打印的FNCx代码。因为一些AI具有可变长度并且应该由FNC1代码分隔,所以你能做的最好就是猜测。 (我不是条形码专家,如果我的分析不正确,请不要犹豫,不要对此答案发表评论。)

这给我们留下了一个相当有趣的问题:在给定输入字符串和可能遇到的AI定义列表的情况下,找到所有可能的有效解码结果。

以下是我尝试解决此问题。

我已经包含了最初在Bark.js中定义的所有AI代码,并且使用了您目前遇到的额外AI代码(使用this page中描述的格式)。

此算法将字符串'02984288445516973701003103022700151802051005165'作为输入,并以递归方式生成以下解法列表:

Solution #1: (02)98428844551697(37)0(10)0(310)3022700(15)180205(10)05165
Solution #2: (02)98428844551697(37)0(10)03(10)302270(01)51802051005165
Solution #3: (02)98428844551697(37)0(10)03(10)3022700(15)180205(10)05165
Solution #4: (02)98428844551697(37)0(10)03(10)302270015180205(10)05165
Solution #5: (02)98428844551697(37)0(10)0310302270(01)51802051005165
Solution #6: (02)98428844551697(37)0(10)03103022700(15)180205(10)05165
Solution #7: (02)98428844551697(37)0(10)0310302270015180205(10)05165
Solution #8: (02)98428844551697(37)01(00)310302270015180205(10)05165
Solution #9: (02)98428844551697(37)0100(310)3022700(15)180205(10)05165
Solution #10: (02)98428844551697(37)01003(10)302270(01)51802051005165
Solution #11: (02)98428844551697(37)01003(10)3022700(15)180205(10)05165
Solution #12: (02)98428844551697(37)01003(10)302270015180205(10)05165

不幸的是,这是一个相当长的清单。您案例中的正确解决方案似乎是#9。



var aiDef = {        // the AI data format is encoded as either:
  '00' : 18,         //  - fixed length   : N
  '01' : 14,         //  - variable length: [ MIN, MAX ] 
  '02' : 14,
  '10' : [ 1, 20 ],
  '11' : 6,
  '12' : 6,
  '13' : 6,
  '15' : 6,
  '16' : 6,
  '17' : 6,
  '310': 7,
  '37' : [ 1, 8 ]
};

function decode(str) {
  var res = [];

  recurse(str, '', res);
  return res;
}

function recurse(str, path, res) {
  // push solution path if we've successfully
  // made it to the end of the string
  if(str == '') {
    res.push(path);
    return;
  }

  var i, j, ai, code;

  // find next AI code
  for(
    i = 0, ai = void(0);
    i < str.length && (ai = aiDef[code = str.substr(0, i)]) === undefined;
    i++
  ) {}

  if(ai !== undefined) {
    // recode AI definition to unique format [ MIN, MAX ]
    ai = typeof ai == 'object' ? ai : [ ai, ai ];

    // iterate on all possible lengths and perform recursive call
    for(j = ai[0]; j <= ai[1]; j++) {
      if(i + j <= str.length) {
        recurse(str.substr(i + j), path + '(' + code + ')' + str.substr(i, j), res);
      }
    }
  }
}

var res = decode('02984288445516973701003103022700151802051005165');

res.forEach(function(r, i) {
  console.log('Solution #' + (i + 1) + ': ' + r);
});
&#13;
&#13;
&#13;