Java Base 64编码QRGen二维码可以';无法解码

Java Base 64编码QRGen二维码可以';无法解码,java,swing,base64,qr-code,Java,Swing,Base64,Qr Code,我正在尝试生成一个二维码,使用Base64编码,并将其作为图像插入HTML字符串中。随后,HTML字符串被解码显示在一个窗格中(然后发送到打印机)。为此,ImageView类被扩展,并使用自定义视图工厂。这一切都很好。。。有时候。它完全取决于输入字符串。一些字符串工作正常,其他字符串失败导致解码过程失败,出现错误java.lang.IllegalArgumentException:Input byte数组的4字节结束单元错误 以下是编码过程: public BufferedImage gene

我正在尝试生成一个二维码,使用Base64编码,并将其作为图像插入HTML字符串中。随后,HTML字符串被解码显示在一个窗格中(然后发送到打印机)。为此,ImageView类被扩展,并使用自定义视图工厂。这一切都很好。。。有时候。它完全取决于输入字符串。一些字符串工作正常,其他字符串失败导致解码过程失败,出现错误
java.lang.IllegalArgumentException:Input byte数组的4字节结束单元错误

以下是编码过程:

 public BufferedImage generateQRCodeImage(String barcodeText) throws Exception {
        ByteArrayOutputStream stream = QRCode.from(barcodeText).to(ImageType.PNG).stream();
        ByteArrayInputStream bis = new ByteArrayInputStream(stream.toByteArray());
        return ImageIO.read(bis);
    }

    public static String encodeToString(BufferedImage image, String type) {
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();
            Base64.Encoder encoder = Base64.getEncoder();
            imageString = encoder.encodeToString(imageBytes);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageString;
    }
        

        private Image loadImage() {
            String b64 = getBASE64Image();
            BufferedImage newImage = null;
            ByteArrayInputStream bais = null;
            try {
                bais = new ByteArrayInputStream(Base64.getDecoder().decode(b64.getBytes())); //fails here
                newImage = ImageIO.read(bais);
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
            return newImage;
        }

        @Override
        public URL getImageURL() {
            String src = (String) getElement().getAttributes().getAttribute(HTML.Attribute.SRC);
            if (isBase64Encoded(src)) {

                this.url = BASE64ImageView.class.getProtectionDomain()
                        .getCodeSource().getLocation();

                return this.url;
            }
            return super.getImageURL();
        }

        private boolean isBase64Encoded(String src) {
            return src != null && src.contains("base64,");
        }

        private String getBASE64Image() {
            String src = (String) getElement().getAttributes().getAttribute(HTML.Attribute.SRC);
            if (!isBase64Encoded(src)) {
                return null;
            }
            return src.substring(src.indexOf("base64,") + 7, src.length() - 1);
        }
以及解码过程:

 public BufferedImage generateQRCodeImage(String barcodeText) throws Exception {
        ByteArrayOutputStream stream = QRCode.from(barcodeText).to(ImageType.PNG).stream();
        ByteArrayInputStream bis = new ByteArrayInputStream(stream.toByteArray());
        return ImageIO.read(bis);
    }

    public static String encodeToString(BufferedImage image, String type) {
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();
            Base64.Encoder encoder = Base64.getEncoder();
            imageString = encoder.encodeToString(imageBytes);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageString;
    }
        

        private Image loadImage() {
            String b64 = getBASE64Image();
            BufferedImage newImage = null;
            ByteArrayInputStream bais = null;
            try {
                bais = new ByteArrayInputStream(Base64.getDecoder().decode(b64.getBytes())); //fails here
                newImage = ImageIO.read(bais);
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
            return newImage;
        }

        @Override
        public URL getImageURL() {
            String src = (String) getElement().getAttributes().getAttribute(HTML.Attribute.SRC);
            if (isBase64Encoded(src)) {

                this.url = BASE64ImageView.class.getProtectionDomain()
                        .getCodeSource().getLocation();

                return this.url;
            }
            return super.getImageURL();
        }

        private boolean isBase64Encoded(String src) {
            return src != null && src.contains("base64,");
        }

        private String getBASE64Image() {
            String src = (String) getElement().getAttributes().getAttribute(HTML.Attribute.SRC);
            if (!isBase64Encoded(src)) {
                return null;
            }
            return src.substring(src.indexOf("base64,") + 7, src.length() - 1);
        }
这是无法解码的二维码

<img width='30' height='30' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH0AAAB9AQAAAACn+1GIAAAApklEQVR4Xu2UMQ4EMQgD/QP+/0vK6zjsvayUMmavWxQpMAUBkwS12wcveAAkgNSCD3rR5Lkgoai3GUCMgWqbAEYR3HxAkZlzU/0MyBisYRsgI1ERFfcpBpA+ze6k56Cj7KTdXNigFWZvSOpsgqLfd18i2aAukXh9TXBNmdWt5gzA/oqzWkkN8HtA7G8CNOwYAiZt3wZixUfkA32OHNQq7Bxs9oI/gC/9fV8AVCkPjQAAAABJRU5ErkJggg=='/>


我确实在浏览器(Chrome)中打开了上面的QR,它确实工作了,这肯定表明解码过程中出现了错误,而不是编码过程。

发现了问题。在getBASE64Image()中,我有

子字符串调用中的“-1”是我出现问题的原因。不确定为什么这只会在某些时候起作用,但移除似乎已经解决了这个问题