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);
}
// ...
}
Asked
Active
Viewed 2,341 times
2
-
1http://stackoverflow.com/a/6075213/571816 – JIV Jul 31 '12 at 09:37
-
ImageIO in javax.imageio.ImageIO – Peter Jul 31 '12 at 10:08
-
@Peter ImageIO does not offer resize/rescale feature. What is ImageUtil.resizeImage? Are you using `Image.getScaledInstance(width, height, hints)`? – Guillaume Polet Jul 31 '12 at 10:47
-
resizeImage(BufferedImage orig, int width, int height, boolean shrinkOnly, int resizeMode) in org.saladn.web.util.ImageUtil – Peter Jul 31 '12 at 11:14
3 Answers
2
The problem must lie in your resizeImage
method. I have no issue with your code:
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();
}
}
});
}
}
Result scaled image displayed in JFrame:
Result scaled image (displayed by your browser):
Original image:

Guillaume Polet
- 47,259
- 4
- 83
- 117
-
-
@Peter I am puzzled as to how you get a pink image while my screenshots obviously proves the contrary. What do you mean by "Try google ...", give me a straight link if you want to show me something. – Guillaume Polet Jul 31 '12 at 12:41
-
3@Peter If you are getting a pink effect, it is likely because you use a BufferedImage with an alpha-channel while JPG does not support that. Make sure you use a `BufferedImage.TYPE_INT_RGB` and not `BufferedImage.TYPE_INT_ARGB` – Guillaume Polet Jul 31 '12 at 13:26
-
sorry, If you upload image with main image of Google, it will come out in pink color. Other images don't have an issue. Just main image of Google is an issue. Please test it after downloading directly. And it still shows pink color although I have tested in two ways - BufferedImage.TYPE_INT_RGB and BufferedImage.TYPE_INT_ARGB. – Peter Aug 01 '12 at 01:43
-
@Peter Do you mean Google main logo? Just provide exact links instead of using words, it would be a lot simpler. I tried one of the logos of Google and it did not cause any issue. Maybe there is some issues with your implementation of ImageIO. What JVM are you using? – Guillaume Polet Aug 01 '12 at 08:21
-
sorry, I can not English, My JVM is java version 1.7.0_02 Java(TM) SE Runtime Environment (build 1.7.0_02-b13) Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode) – Peter Aug 01 '12 at 09:04
-
@Peter Then I don't understand your problem. I have absolutely no issues executing my code with that same JVM. Edit my code to reproduce your issue and then edit your post by adding your edited code. – Guillaume Polet Aug 01 '12 at 09:18
-
that is My sourcecode. same source of Guillaume Polet. but my sourcecode have pink color – Peter Aug 01 '12 at 10:42
-
File filePath = new File("C:\\Users\\chang\\Desktop\\05036877.jpg"); com.sun.image.codec.jpeg.JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder (new FileInputStream(filePath)); BufferedImage image = jpegDecoder.decodeAsBufferedImage(); -> no problem, thank you Guillaume Polet^^ – Peter Aug 02 '12 at 10:19
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);
}
}

Peter
- 91
- 1
- 5
-
that is My sourcecode. same source of Guillaume Polet. but my sourcecode have pink color – Peter Aug 01 '12 at 10:42
0
This answer looks pretty much what you are looking for (even though I didn't test it myself):