java buffered image created with red mask

前端 未结 6 1691
忘了有多久
忘了有多久 2020-12-04 02:49

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\");
ImageInputSt         


        
6条回答
  •  再見小時候
    2020-12-04 03:30

    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

提交回复
热议问题