我使用下面的代碼調整圖像大小並將它們存儲在服務器上。但是,對於某些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