编码 PDF417 时的 IndexOutOfBounds

IndexOutOfBounds when encoding a PDF417

我正在尝试对 PDF417 进行编码,但每当我将 PDF417_COMPACTION 设置为 Compaction.TEXT 时,我都会遇到此崩溃:

java.lang.ArrayIndexOutOfBoundsException: length=128; index=8226
    at com.google.zxing.pdf417.encoder.PDF417HighLevelEncoder.isMixed(PDF417HighLevelEncoder.java:456)
    at com.google.zxing.pdf417.encoder.PDF417HighLevelEncoder.encodeText(PDF417HighLevelEncoder.java:298)
    at com.google.zxing.pdf417.encoder.PDF417HighLevelEncoder.encodeHighLevel(PDF417HighLevelEncoder.java:185)
    at com.google.zxing.pdf417.encoder.PDF417.generateBarcodeLogic(PDF417.java:649)
    at com.google.zxing.pdf417.PDF417Writer.bitMatrixFromEncoder(PDF417Writer.java:107)
    at com.google.zxing.pdf417.PDF417Writer.encode(PDF417Writer.java:87)
    at com.google.zxing.MultiFormatWriter.encode(MultiFormatWriter.java:102)

这是我尝试创建条形码的代码:

var hints: MutableMap<EncodeHintType, Any>?

hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)

hints[EncodeHintType.CHARACTER_SET] = "UTF-8" 
hints[EncodeHintType.ERROR_CORRECTION] = 6
hints[EncodeHintType.PDF417_COMPACTION] = Compaction.TEXT

val writer = MultiFormatWriter()
val result: BitMatrix
try {
    result = writer.encode(contents, format, img_width, img_height, hints)
} catch (iae: WriterException) {
    // Unsupported format
    return null
}

这是一个错误还是我做错了什么?

你的img_width、img_height是什么? 这是我的解决方案,你需要使用 realWidth = bitMatrix.getWidth()....:[=​​11=]

BitMatrix bitMatrix = new MultiFormatWriter().encode(mContent, barcodeFormat, mWidth, mHeight, hintsMap);
    int realWidth = bitMatrix.getWidth();
    int realHeight = bitMatrix.getHeight();

    int[] pixels = new int[realWidth * realHeight];
    for (int i = 0; i < realHeight; i++) {
        for (int j = 0; j < realWidth; j++) {
            if (bitMatrix.get(j, i)) {
                pixels[i * realWidth + j] = 0x00000000;
            } else {
                pixels[i * realWidth + j] = 0xffffffff;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(pixels, 0, realWidth, realWidth, realHeight, Bitmap.Config.RGB_565);