【问题标题】:Decoding EAN 128解码 EAN 128
【发布时间】:2011-08-06 20:33:44
【问题描述】:

有很多组件可以创建/解析条形码图像,但我无法找到一个库来解析 EAN 128 条形码字符串并给我一个简单的 java-pojo 对象,如果他们可以从中获取 EAN128 组被包含在条形码中。

示例伪代码:

EAN128Pojo pojo = EAN128Pojo.parse(some string got from scanner);
Date dueDate = pojo.getDueDate();

Object dueDate = pojo.get(12 /*application identifier for due date*/);

有没有图书馆可以做到这一点?

干杯

【问题讨论】:

    标签: java barcode


    【解决方案1】:

    我不知道,Google CodeSearch 也不知道:http://www.google.com/codesearch?q=getAdditionalProductIdentification

    无论如何,编写自己的代码并不难。这个花了我不到一个小时:

    package so5685964;
    
    import java.util.Map;
    
    import org.joda.time.DateMidnight;
    
    import com.google.common.collect.Maps;
    
    public class GS1Code128Data {
    
      /** Maps the AI to the corresponding data from the barcode. */
      private final Map<String, String> data = Maps.newHashMap();
    
      private static final Map<String, AII> aiinfo = Maps.newHashMap();
    
      static class AII {
        final int minLength;
        final int maxLength;
    
        public AII(String id, int minLength, int maxLength) {
          this.minLength = minLength;
          this.maxLength = maxLength;
        }
      }
    
      private static void ai(String id, int minLength, int maxLength) {
        aiinfo.put(id, new AII(id, minLength, maxLength));
      }
    
      private static void ai(String id, int length) {
        aiinfo.put(id, new AII(id, length, length));
      }
    
      static {
        ai("00", 18, 18);
        ai("01", 14);
        ai("02", 14);
        ai("10", 1, 20);
        ai("11", 6);
        ai("12", 6);
        // TODO: continue according to http://en.wikipedia.org/wiki/GS1-128
      }
    
      /**
       * Decodes a Unicode string from a Code128-like encoding.
       *
       * @param fnc1 The character that represents FNC1.
       */
      public GS1Code128Data(String s, char fnc1) {
        StringBuilder ai = new StringBuilder();
        int index = 0;
        while (index < s.length()) {
          ai.append(s.charAt(index++));
          AII info = aiinfo.get(ai.toString());
          if (info != null) {
            StringBuilder value = new StringBuilder();
            for (int i = 0; i < info.maxLength && index < s.length(); i++) {
              char c = s.charAt(index++);
              if (c == fnc1) {
                break;
              }
              value.append(c);
            }
            if (value.length() < info.minLength) {
              throw new IllegalArgumentException("Short field for AI \"" + ai + "\": \"" + value + "\".");
            }
            data.put(ai.toString(), value.toString());
            ai.setLength(0);
          }
        }
        if (ai.length() > 0) {
          throw new IllegalArgumentException("Unknown AI \"" + ai + "\".");
        }
      }
    
      private static DateMidnight asDate(String s) {
        if (s == null) {
          return null;
        }
        String century = s.compareTo("500000") < 0 ? "20" : "19";
        return new DateMidnight(century + s);
      }
    
      public DateMidnight getDueDate() {
        return asDate(data.get("12"));
      }
    }
    

    还有一些演示代码:

    package so5685964;
    
    public class BarcodeDemo {
    
      public static void main(String[] args) {
        String barcode = "12110416";
    
        GS1Code128Data data = new GS1Code128Data(barcode, '\f');
    
        System.out.println(data.getDueDate());
      }
    }
    

    当您假设您的输入已经是String 时,请注意编码问题。 FNC1 代码没有对应的 Unicode 代码点,因此必须以其他方式对其进行编码。

    【讨论】:

    • @roland-illig 非常感谢,这对我有很大帮助,但知道并非所有条码扫描仪都提供 FNC1 字符,我们需要更复杂的算法。由barcode4j (barcode4j.cvs.sourceforge.net/viewvc/barcode4j/barcode4j/src/…) 提供的模板将是一个很好的起点。但是因为我相信应该有这样的图书馆,所以我不必重新发明轮子,我想在这里问。显然没有这样的图书馆。
    • 我不知道您的代码是否有效(我会尝试一下),但我只是想感谢您花时间编写此代码。
    • 我不知道您的代码是否有效(我会尝试一下),但我只是想感谢您花时间编写此代码。
    猜你喜欢
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2016-03-06
    • 2011-03-17
    • 2016-09-13
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多