iText BarcodeDataMatrix会针对某些输入抛出ArrayIndexOutofBounds异常

时间:2015-02-13 19:21:47

标签: java itext

在某些情况下能够生成条形码,而其他人则无法抛出ArrayIndexOutOfBounds异常。 请帮忙。提前致谢。

String barcode="10e5ae5147d20db19651f90247621699";
BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
datamatrix.generate(barcode);
datamatrix.setHeight(22);
img = datamatrix.createImage();
img.setAbsolutePosition(175, 750);      

当用于创建条形码十六进制字符串的输入字符串具有较少的字符数时,它正在工作。当我增加输入字符串中的字符数时,它会失败。这里给出的十六进制字符串失败了。 这是堆栈跟踪:

Barcode string is :10e5ae5147d20db19651f90247621699
Print barcode
java.lang.ArrayIndexOutOfBoundsException
    at com.lowagie.text.pdf.codec.CCITTG4Encoder.pixel([BII)I(Unknown Source)
    at com.lowagie.text.pdf.codec.CCITTG4Encoder.Fax3Encode2DRow()V(Unknown Source)
    at com.lowagie.text.pdf.codec.CCITTG4Encoder.fax4Encode([BII)V(Unknown Source)
    at com.lowagie.text.pdf.codec.CCITTG4Encoder.compress([BII)[B(Unknown Source)
    at com.lowagie.text.pdf.BarcodeDatamatrix.createImage()Lcom.lowagie.text.Image;(Unknown Source)
    at com.sample.common.util.ItextUtils.barcode(ItextUtils.java:294)

1 个答案:

答案 0 :(得分:0)

如果无法渲染数据矩阵代码,则应尝试以下方法:

System.out.println(datamatrix.generate("10e5ae5147d20db19651f90247621699"));

此方法返回以下值之一:

/**
 * No error.
 */
public static final int DM_NO_ERROR = 0;
/**
 * The text is too big for the symbology capabilities.
 */
public static final int DM_ERROR_TEXT_TOO_BIG = 1;
/**
 * The dimensions given for the symbol are illegal.
 */
public static final int DM_ERROR_INVALID_SQUARE = 3;
/**
 * An error while parsing an extension.
 */
public static final int DM_ERROR_EXTENSION = 5;

在您的情况下"10e5ae5147d20db19651f90247621699"返回3(DM_ERROR_INVALID_SQUARE),这意味着您为该符号定义的尺寸是非法的。

确实:如果您要移除行datamatrix.setHeight(22);(在这种情况下iText定义维度)或者您是否使用适当的数据高度(例如将22替换为20 ),那么你就不会得到例外。

我的代码中也看到了我的名字(我是Lowagie)。我告诉您,您使用的是至少5年半的iText版本,并且您无法再使用该版本:https://stackoverflow.com/questions/25696851/can-itext-2-1-7-or-earlier-can-be-used-commercially