FULL PRODUCT VERSION :
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
This bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4776576
seems to describe what's happening in 1.7.0_45 again.
To see the output:
http://stackoverflow.com/questions/20789043/image-changes-color-when-saved-with-java
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
public class ImageSaver {
public static void main(final String url) {
URL u = null;
try {
u = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String file = url.substring(url.indexOf("//") + 2);
Path filePath = Paths.get("c:/").resolve(file);
try {
Files.createDirectories(filePath.getParent());
BufferedImage img = ImageIO.read(u);
ImageIO.write(img, "jpg", filePath.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
q&d:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
public class ImageSaverWorkAround {
public static void main(final String url) {
URL u = null;
try {
u = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String file = url.substring(url.indexOf("//") + 2);
Path filePath = Paths.get("c:/").resolve(file);
try {
Files.createDirectories(filePath.getParent());
Image img = Toolkit.getDefaultToolkit().createImage(url);
while (img.getWidth(null) == -1 || img.getHeight(null) == -1) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int w = img.getWidth(null);
int h = img.getHeight(null);
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage pic = new BufferedImage(w, h, type);
Graphics2D g2 = pic.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
ImageIO.write(pic, "jpg", filePath.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}