解码QR码时出错

时间:2015-01-04 21:43:08

标签: java qr-code zxing

我正在解码我正在使用Java JSANE API的Qr代码。当我在使用IMAGEIO.read()读取文件中从JSANE API中获取的图像时,解码过程正在运行,但是当我使用JSANE API直接提供的ilage对象时,无需将其写入在阅读之前的文件,我收到以下错误:

Exception in thread "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();

我正在将Image转换为BufferedImage,如下所示:

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

}

我甚至尝试在使用之前调整BufferedImage的大小,但是当我这样做时,即使从结果文件中解码也无法正常工作。

0 个答案:

没有答案