Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOutOfBounds when encoding a PDF417 #1163

Closed
Doophie opened this issue May 3, 2019 · 9 comments
Closed

IndexOutOfBounds when encoding a PDF417 #1163

Doophie opened this issue May 3, 2019 · 9 comments
Labels

Comments

@Doophie
Copy link

Doophie commented May 3, 2019

I'm trying to encode a PDF417 and whenever I set the PDF417_COMPACTION to Compaction.TEXT I get this crash:

    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)

Here is the code for how I am trying to create the barcode:

        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
        }

Is this a bug or am i doing something wrong?

@srowen
Copy link
Contributor

srowen commented May 3, 2019

I suspect it's a bug, and I unfortunately have no idea what. I think there are a few latent bugs in the PDF417 parts. If you figure it out, feel free to open a PR. What language is this BTW?

@Doophie
Copy link
Author

Doophie commented May 3, 2019

@srowen will do, the language is kotlin

@Doophie
Copy link
Author

Doophie commented May 9, 2019

@srowen After looking into it a bit more, my data contained a • character and it was checking that character that it crashed and got the index out of bounds. If i have time later i'll see if i can fix the crash.

@srowen srowen added the bug label May 16, 2019
@hidethatbug
Copy link

I suspect it's a bug, and I unfortunately have no idea what. I think there are a few latent bugs in the PDF417 parts. If you figure it out, feel free to open a PR. What language is this BTW?

i use java ,i work for android

@Doophie
Copy link
Author

Doophie commented Jun 16, 2019

@hidethatbug did you by chance try using the • character in what your contents?

@khavnu
Copy link

khavnu commented Sep 29, 2020

What is your img_width, img_height?
This is my soluttion, you need to use realWidth = bitMatrix.getWidth()....:

        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);

@wvieed
Copy link

wvieed commented Dec 22, 2020

G

@srowen
Copy link
Contributor

srowen commented May 1, 2022

Likely fixed by #1514

@srowen srowen closed this as completed May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

7 participants
@srowen @khavnu @sanjaykumarm @Doophie @hidethatbug @wvieed and others