I'm decoding an Qr Code that i'm getting throw the Java JSANE API. when i'm writing the image i got from the JSANE API in a file before reading it with IMAGEIO.read(), the decodong process is working but when i'm using the ilage Object given from the JSANE API directly without writing it in a file before reading, i' getting the following error :
Exception in thread "main" com.google.zxing.NotFoundException
The method i'm using are:
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();
}
I'm getting the image from the JSANE API like this:
Image image = dialog.openDialog();
I'm converting the Image to BufferedImage like this:
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;
}
This the main function of the class:
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));
}
I'have even tried to resize th BufferedImage before using it but when i'm doing that, even the decode from the resultant file is not working.