Java buffered image created with a red mask

I have a problem reading the image. If I do the following

URL url = new URL("http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg"); ImageInputStream stream = ImageIO.createImageInputStream(url.openStream()); ImageReader reader = ImageIO.getImageReaders(stream).next(); reader.setInput(stream, true, true); BufferedImage image = reader.read(0); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageWriter writer = ImageIO.getImageWritersByFormatName("JPEG").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(bos); writer.setOutput(ios); IIOImage ioImage = new IIOImage(image, null, null); writer.write(ioImage); ios.close(); FileOutputStream fos = new FileOutputStream("badimage.jpeg"); fos.write(bos.toByteArray()); fos.close(); 

The image is recorded in red. Is there any parameter that needs to be set to read this image correctly?

+3
source share
6 answers

The problem may be related to ImageIO.read , which ImageIO.read not correctly read JPG images. Here is a similar bug (Bug ID: 4881314) that may not be partially resolved.

Alternatively, you can try using Toolkit.createImage , which seems to handle the specified image correctly. For instance:

 import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; class TestImage { public static void main(String args[]) { try { URL imageUrl = new URL( "http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg"); BufferedImage ioImage = ImageIO.read(imageUrl); Image toolkitImage = Toolkit.getDefaultToolkit().createImage( imageUrl); JPanel panel = new JPanel(); panel.add(new JLabel(new ImageIcon(ioImage))); panel.add(new JLabel(new ImageIcon(toolkitImage))); JOptionPane.showMessageDialog(null, panel, "ImageIO vs Toolkit", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } } 

Here is the result:

enter image description here

+5
source

Unfortunately. I have no answer to the question of why there is a red tint.

So we read the images in our software. In our case, we use a scalar library to resize the image.

 URL url = new URL("http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg"); BufferedImage source = javax.imageio.ImageIO.read(url); BufferedImage manipulated = ... FileOutputStream fos = new FileOutputStream("badimage.jpeg"); javax.imageio.ImageIO.write(manipulated , "png", fos); 
+1
source
 BufferedImage bi = ImageIO.read( "http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg" ); ImageIO.write( bi, 'JPEG', new File( "badimage.jpeg" ); 
0
source
 ImageIcon mySourceImage = new ImageIcon(sourceImageFile.getAbsolutePath()); BufferedImage sourceImage = new BufferedImage(mySourceImage.getIconWidth(), mySourceImage.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g2d2 = (Graphics2D) sourceImage.getGraphics(); mySourceImage.paintIcon(null, g2d2, 0, 0); g2d2.dispose(); 

The code above does NOT use Image.read, which (quite likely) contains an error. It does not create red images. But I'm not sure about the color space in the third parameter of BufferedImage.

0
source

As mentioned in other answers, this is a known bug in the standard JPEG plugin that comes with ImageIO and Oracle JRE.

However, you can continue to use ImageIO as in the OP source code, replacing the JPEG plugin with the TwelveMonkeys ImageIO JPEG plugin. You only need to add the JAR and its dependencies to the path of the execution path. No code changes are required (I tested the OP files).

The plugin is specifically designed to work or fix many problems with the standard JPEG plugin. It supports CMYK JPEG, broken ICC profiles, Exif data, and more. The plugin was developed by me and is freely distributed under the BSD open source license.

0
source

Here is an example Java servlet for a workaround without using imageIO:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //TestImage String testImage = "UNC Path or URL.jpg";//Load your image here... //Get the streams FileInputStream inStream = new FileInputStream(testImage); ServletOutputStream outStream = response.getOutputStream(); //Create the buffers BufferedInputStream inBuf = new BufferedInputStream(inStream); BufferedOutputStream outBuf = new BufferedOutputStream(outStream); //Write input into output int ch =0; ; while((ch=inBuf.read())!=-1) outBuf.write(ch); inBuf.close(); inStream.close(); outBuf.close(); outStream.close(); } 
0
source

All Articles