ImageIO jpg resizeImage后图像变为粉红色

时间:2012-07-31 09:32:28

标签: java image swing image-processing javax.imageio

public static void main(String argsp[]){

    File src = new File("C:\\Users\\chang\\Desktop\\shinichi_hoshi-2011-hp.jpg");
    try {
        FileInputStream fis = new FileInputStream(src);
        BufferedImage bi = ImageIO.read(fis);
        FileOutputStream fos = new FileOutputStream("C:\\Users\\chang\\Desktop\\www1.jpg");
        BufferedImage bsi = ImageUtil.resizeImage(bi,100,100,true,0);

        ImageIO.write(bsi, "jpg", fos);
    }
    // ...
}

3 个答案:

答案 0 :(得分:2)

问题必须在于resizeImage方法。我对您的代码没有任何问题:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestImageResize {

    protected void initUI() throws MalformedURLException, IOException {
        final JFrame frame = new JFrame(TestImageResize.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BufferedImage bi = ImageIO.read(new URL("http://noupe.com/img/wallpaper-4.jpg"));
        File file = new File(System.getProperty("user.home"), "test.jpg");
        FileOutputStream fos = new FileOutputStream(file);
        Image image = bi.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
        BufferedImage bsi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        bsi.getGraphics().drawImage(image, 0, 0, null);
        ImageIO.write(bsi, "jpg", fos);
        JPanel panel = new JPanel();
        JLabel label = new JLabel(new ImageIcon(file.getAbsolutePath()));
        panel.add(label);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new TestImageResize().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

JFrame中显示的结果缩放图像: Result scaled image

结果缩放图像(由浏览器显示): Result image uploaded from disk

原始图片: Original image

答案 1 :(得分:0)

public class ImageTest {

public static void main(String argsp[]) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                new ImageTest().initUI();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

}

protected void initUI() throws MalformedURLException, IOException {
    final JFrame frame = new JFrame(ImageTest.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BufferedImage bi = ImageIO.read(new URL(
            "http://www.google.co.kr/logos/2012/field_hockey-2012-hp.jpg"));
    File file = new File(System.getProperty("user.home"), "test1.jpg");
    FileOutputStream fos = new FileOutputStream(file);
    Image image = bi.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
    BufferedImage bsi = new BufferedImage(image.getWidth(null),
            image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bsi.getGraphics().drawImage(image, 0, 0, null);
    ImageIO.write(bi, "jpg", fos);
    JPanel panel = new JPanel();
    JLabel label = new JLabel(new ImageIcon(file.getAbsolutePath()));
    panel.add(label);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

}

答案 2 :(得分:0)

这个答案看起来很像你正在寻找的东西(尽管我自己没有测试过):

Unable to read JPEG image using ImageIO.read(File file)