Java 解码二维码时出错

Java 解码二维码时出错,java,qr-code,zxing,Java,Qr Code,Zxing,我正在解码通过JavaJSANEAPI得到的Qr码。当我在使用IMAGEIO.read()读取之前将从JSANE API获取的图像写入文件时,decodong进程正在工作,但当我在读取之前直接使用JSANE API提供的ilage对象而不将其写入文件时,我得到以下错误: 线程“main”com.google.zxing.NotFoundException中的异常 我使用的方法是: public String decode_QrCode(String filePath, String charse

我正在解码通过JavaJSANEAPI得到的Qr码。当我在使用IMAGEIO.read()读取之前将从JSANE API获取的图像写入文件时,decodong进程正在工作,但当我在读取之前直接使用JSANE API提供的ilage对象而不将其写入文件时,我得到以下错误:

线程“main”com.google.zxing.NotFoundException中的异常

我使用的方法是:

public String decode_QrCode(String filePath, String charset) throws FileNotFoundException, IOException, NotFoundException{
    System.out.println("ICI ERREUR");
        BinaryBitmap binaryBitmap = new BinaryBitmap(
                new HybridBinarizer(
                        new BufferedImageLuminanceSource(
                                ImageIO.read(new FileInputStream(filePath))
                        )
                )
        );

        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);

        return qrCodeResult.getText();

}

public String decode_QrCode_buffImg(BufferedImage bufferedImage) throws NotFoundException{

    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap binaryBitmap = new BinaryBitmap(
            new HybridBinarizer(
                    source
            )
    );

    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);

    return qrCodeResult.getText();

}
我从JSANE API获取的图像如下:

Image image = dialog.openDialog();
public static BufferedImage toBufferedImage(Image image)
{
     if (image instanceof BufferedImage)
            return (BufferedImage)image;

            // This code ensures that all the pixels in the image are loaded
            image = new ImageIcon(image).getImage();

            // Determine if the image has transparent pixels
            boolean hasAlpha = hasAlpha(image);

            // Create a buffered image with a format that's compatible with the screen
            BufferedImage bimage = null;

            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

            try {
                // Determine the type of transparency of the new buffered image
                int transparency = Transparency.OPAQUE;

                if (hasAlpha == true)
                    transparency = Transparency.BITMASK;

                // Create the buffered image
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gs.getDefaultConfiguration();

                bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
            } catch (HeadlessException e) { } //No screen

            if (bimage == null) {
                // Create a buffered image using the default color model
                int type = BufferedImage.TYPE_INT_RGB;

                if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
                    bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
            }

            // Copy image to buffered image
            Graphics g = bimage.createGraphics();

            // Paint the image onto the buffered image
            g.drawImage(image, 0, 0, null);
            g.dispose();

            return bimage;
}
我正在将图像转换为BuffereImage,如下所示:

Image image = dialog.openDialog();
public static BufferedImage toBufferedImage(Image image)
{
     if (image instanceof BufferedImage)
            return (BufferedImage)image;

            // This code ensures that all the pixels in the image are loaded
            image = new ImageIcon(image).getImage();

            // Determine if the image has transparent pixels
            boolean hasAlpha = hasAlpha(image);

            // Create a buffered image with a format that's compatible with the screen
            BufferedImage bimage = null;

            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

            try {
                // Determine the type of transparency of the new buffered image
                int transparency = Transparency.OPAQUE;

                if (hasAlpha == true)
                    transparency = Transparency.BITMASK;

                // Create the buffered image
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gs.getDefaultConfiguration();

                bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
            } catch (HeadlessException e) { } //No screen

            if (bimage == null) {
                // Create a buffered image using the default color model
                int type = BufferedImage.TYPE_INT_RGB;

                if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
                    bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
            }

            // Copy image to buffered image
            Graphics g = bimage.createGraphics();

            // Paint the image onto the buffered image
            g.drawImage(image, 0, 0, null);
            g.dispose();

            return bimage;
}
这是该类的主要功能:

public static void main(String[] args) throws IOException, cf, NotFoundException {
    Frame frame = null;
    // TODO Auto-generated method stub

    JSaneDialog dialog = new JSaneDialog( JSaneDialog.CP_START_SANED_LOCALHOST, 
            frame, "JSaneDialog", true, null);

    Image image = dialog.openDialog();


    BufferedImage buffImg = toBufferedImage(image);

    //BufferedImage buff = resize(buffImg, BufferedImage.TYPE_INT_RGB, buffImg.getWidth()/2, buffImg.getHeight()/2, 0.5, 0.5);

    ImageIO.write(buffImg, "png", new File("/home/michaelyamsi/Bureau/Mémoires/test/test.png"));


    QrCode New_Qr = new QrCode();
    System.out.println("QR Code decompressed : " +  New_Qr.decode_QrCode("/home/michaelyamsi/Bureau/Mémoires/test/test.png", "UTF-8"));
    System.out.println("QR Code decompressed : " +  New_Qr.decode_QrCode_buffImg(buffImg));
}


在使用BuffereImage之前,我甚至尝试过调整它的大小,但当我这样做时,即使结果文件中的解码也不起作用。

您得到的错误还不够,什么是错误stacktrace?问题是taht是我在执行代码时得到的唯一错误行。这是主要功能:您是否尝试将test.png图像放在路径不包含“é”字符的目录中?包含空格或非ascii字符的路径是一个巨大的错误源。谢谢StephaneM的回答,但是从“test.png”图像读取不会导致任何问题,但是它直接从BuffereImage解码,而不将其写入导致问题的test.png文件。