How to read a JPEG image into a BufferedImage object using Java

This is not a duplicate question because I have been searching for a solution on Google and StackOverflow for a long time and still cannot find a solution.

I have two images:

Larger Image

Smaller image

These are two images from the same website with the same prefix and the same format. The only difference is the size: the first is larger and the second is smaller.

I uploaded both images to a local folder and used Java to read them into BufferedImage objects. However, when I output BufferedImages to local files, I found that the first image was almost red and the second was normal (the same as the original). What is wrong with my code?

byte[] rawData = getRawBytesFromFile(imageFilePath); // some code to read raw bytes from image file ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData)); BufferedImage img = ImageIO.read(iis); FileOutputStream fos = new FileOutputStream(outputImagePath, false); ImageIO.write(img, "JPEG", fos); fos.flush(); fos.close(); 

PS: I used GIMP to open the first image and found that the Color Mode is 'sRGB', no alpha or other material.

+4
source share
2 answers

This is apparently a mistake, I saw several suggestions ( this ) that suggest using Toolkit#createImage instead, which apparently ignores the color model.

I tested this and it seems to be working fine.

 public class TestImageIO01 { public static void main(String[] args) { try { Image in = Toolkit.getDefaultToolkit().createImage("C:\\hold\\test\\13652375852388.jpg"); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(in)), "Yeah", JOptionPane.INFORMATION_MESSAGE); BufferedImage out = new BufferedImage(in.getWidth(null), in.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = out.createGraphics(); g2d.drawImage(in, 0, 0, null); g2d.dispose(); ImageIO.write(out, "jpg", new File("C:\\hold\\test\\Test01.jpg")); } catch (Exception ex) { ex.printStackTrace(); } } } 

nb- I used JOptionPane to check the incoming image. When using ImageIO it comes with a red tint, with Toolkit it looks normal.

Update

And explantation

+9
source

I checked your code in netbeans and ran into your problem, then I changed the code as shown below which has no problems:

  public class Test { public static void main(String args[]) throws IOException { byte[] rawData = getRawBytesFromFile(imageFilePath); // some code to read raw bytes from image file // ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData)); // BufferedImage img = ImageIO.read(iis); FileOutputStream fos = new FileOutputStream(outputImagePath, false); fos.write(rawData); // ImageIO.write(img, "JPEG", fos); fos.flush(); fos.close(); } private static byte[] getRawBytesFromFile(String path) throws FileNotFoundException, IOException { byte[] image; File file = new File(path); image = new byte[(int)file.length()]; FileInputStream fileInputStream = new FileInputStream(file); fileInputStream.read(image); return image; } } 

Please check it and let me know about the result;)

Luck

0
source

All Articles