GS1 128 PHP或Jquery中的条形码解码器(AI)


GS1 128 Barcode Decoder (AI) in PHP or Jquery

我正在进行一个项目,该项目需要读取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中还有其他库可以控制所有这些情况吗??

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

综上所述,您需要解码GS1 128条形码中的应用程序标识符(AI),该条形码缺少不可打印的FNCx代码。因为有些AI的长度可变,并且应该由FNC1代码分隔,所以您能做的最好的事情就是猜测。(我不是条形码专家,所以如果我的分析不正确,请毫不犹豫地评论这个答案。)

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

下面是我解决这个问题的尝试。

我已经包含了最初在Bark.js中定义的所有人工智能代码,以及迄今为止您似乎遇到的额外人工智能代码(使用本页描述的格式)。

该算法将字符串'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);
});