2012-01-22 45 views
5

我試圖通過水平翻轉圖像並調整其大小來轉換圖像。問題是,當轉換完成後,圖片的顏色都很奇怪,它已經變成了這種微紅的色調。是否有可能以某種方式解決這個問題,我認爲我在某個地方讀到這可能是AWT庫中的一些錯誤,但我不確定嗎?圖像轉換會導致紅色圖像?

下面是代碼:

import java.awt.Graphics2D; 
import java.awt.geom.AffineTransform; 
import java.awt.image.AffineTransformOp; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class LocalImageSizeFlip { 

public static void main(String[] args) { 
    BufferedImage img = null; 

    try { 
     img = ImageIO.read(new File("C:\\picture.jpg")); 
     AffineTransform tx = AffineTransform.getScaleInstance(1, -1); 
     tx.translate(0, -img.getHeight(null)); 
     AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
     img = op.filter(img, null); 
     img = resize(img, 100, 75); 
     File newFile = new File("newPicture.jpg"); 
     ImageIO.write(img, "JPEG", newFile); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private static BufferedImage resize(BufferedImage image, int width, int height) { 
    BufferedImage resizedImage = new BufferedImage(width, height, 
    BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g = resizedImage.createGraphics(); 
    g.drawImage(image, 0, 0, width, height, null); 
    g.dispose(); 
    return resizedImage; 
    } 
} 
+0

參見[BufferedImage的顏色變化(http://stackoverflow.com/questions/6122964/bufferedimage-colour-change) – trashgod

回答

6

具有圖像開發的色調通常意味着圖像被使用了錯誤的色彩空間,Adobe RGB的與sRGB的是常年的最愛渲染。嘗試在代碼中將TYPE_INT_ARGB更改爲TYPE_INT_RGB。

+0

它的伎倆,謝謝! – user1075481

0

您也可以嘗試以下類型:BufferedImage.TYPE_3BYTE_BGR