我使用下面的代码调整图像大小并将它们存储在服务器上。但是,对于某些JPG,调整大小会在图像上产生淡淡的色调。任何想法可能导致这种情况?这是原始图像:http://www.unctv.org/tWxkBbq_10M6wKRRL/PNCWWD01.jpg以下是它后来生成的内容:http://www.unctv.org/tWxkBbq_10M6wKRRL/PABDFC01__1289231445291.jpg在某些JPG上的Java图像调整大小
Mac上的Photoshop CS5用于为此应用程序生成JPG输入。谢谢。
public BufferedImage resizeVeryHigh(InputStream inputStream, File resizedFile) throws IOException {
BufferedImage bufferedImage = null;
try {
Image newImage = ImageIO.read(inputStream);
int newWidth = (int)this.targetWidth;
ImageIcon imageIcon = new ImageIcon(newImage);
Image image = imageIcon.getImage();
Image resizedImage = null;
int iWidth = image.getWidth(null);
int iHeight = image.getHeight(null);
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Encodes image as a JPEG data stream
FileOutputStream out = new FileOutputStream(resizedFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(1.0f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
}
catch (Exception e) {
this.error = e.getMessage();
}
return bufferedImage;
}
有趣。我不知道JPEG规范中的“progressive”。可能这是编码器对该功能的支持的人为因素。 – trashgod 2010-11-08 19:53:38