java buffered image created with red mask
Asked Answered
U

6

3

I am having trouble reading an 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 written with a red tint. Is there some option that needs to be set to read this image correctly?

Unexceptional answered 18/10, 2012 at 20:59 Comment(1)
Have a look at Odd coloured JPEGs in Java with ImageIO. I've hear of this a couple of times, but never had the problem, I use PNGsVillosity
B
5

The problem maybe related to ImageIO.read that fails to correctly read some JPG images. Here is a similar bug (Bug ID: 4881314) that may still be partially unresolved.

As an alternative you can try using Toolkit.createImage that seems to handle the specified image correctly. For example:

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

Brom answered 19/10, 2012 at 3:45 Comment(0)
G
1

Sorry. I don't have the answer to why there is a red tint.

This is how we read images in our software. In our case we are using the 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);
Giles answered 18/10, 2012 at 21:20 Comment(0)
A
0
BufferedImage bi =
   ImageIO.read(
      "http://tctechcrunch2011.files.wordpress.com/2012/10/gmm.jpg" );
ImageIO.write( bi, 'JPEG', new File( "badimage.jpeg" );
Aposematic answered 18/10, 2012 at 21:15 Comment(0)
T
0
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 probably) contains a bug. It doesn't produce red images. But I'm not sure about colorspace in BufferedImage third parameter.

Thrombocyte answered 1/4, 2015 at 11:6 Comment(0)
N
0

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

However, it's possible to continue to use ImageIO as in the OP's original code, by replacing the JPEG plugin with the TwelveMonkeys ImageIO JPEG plugin. You only need to add the JAR and its dependencies to the runtime classpath. No code changes necessary (I tested with the OP's test files).

The plugin is especially made to work around or fix the many issues with the standard JPEG plugin. It supports CMYK JPEGs, broken ICC profiles, Exif data and more. The plugin is developed by me, and is freely distributable under the open source BSD license.

Narial answered 19/5, 2016 at 9:26 Comment(0)
C
0

Here is a Java Servlet Example for a Workaround whitout 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();
}
Cantabrigian answered 5/12, 2017 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.