2016-04-28 60 views
2

我試圖使用EXIF信息修復照片方向,照片正確旋轉但旋轉後它們變得質量很低......我的猜測是在寫入新圖像期間傳遞的參數是錯誤。任何幫助讚賞。使用AffineTransform變換圖像後,照片質量較差

Before After

//code get Exif information 
Metadata metadata = ImageMetadataReader.readMetadata(outputFile); 
     Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); 
     if(directory == null) { 
      logger.warn("no EXIF info."); 
      outputFile.delete(); 
      return; 
     } 
     JpegDirectory jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory.class); 
     int orientation; 
     try { 
      orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION); 
      if(orientation != 1) { 
       //rotate image 
       int w = jpegDirectory.getImageWidth(); 
       int h = jpegDirectory.getImageHeight(); 
       ImageInformation imageInformation = new ImageInformation(orientation, w, h); 
       AffineTransform affineTransform = getExifTransformation(imageInformation); 

       InputStream pictureStream = new FileInputStream(outputFile); 
       BufferedImage pictureBuffer = ImageIO.read(pictureStream); 
       pictureStream.close(); 
       if (pictureBuffer == null) { 
        logger.warn("The picture buffer parsed is null."); 
       } 
       pictureBuffer = transformImage(pictureBuffer, affineTransform); 


    //code do image transfer 
    public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception { 

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); 
    BufferedImage destinationImage = op.createCompatibleDestImage(image, null); 
    Graphics2D g = destinationImage.createGraphics(); 
    g.setBackground(Color.WHITE); 
    g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight()); 
    destinationImage = op.filter(image, destinationImage); 
    return destinationImage; 
} 
+0

我不認爲圖像質量被降解,大多數圖案看起來與以前一樣尖銳(請參閱牛奶瓶上的麋鹿)。但是,我會說混合起來的顏色和總體照度下降。 – FiReTiTi

回答

0

這可能會解決你的問題。據AffineTransformOp

"If destCM is null, an appropriate ColorModel is used; this ColorModel may 
have an alpha channel even if the source ColorModel is opaque." 

因此,我建議如下:

AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); 
BufferedImage destinationImage = op.createCompatibleDestImage(image, null); 
destinationImage = op.filter(image, null); 
return destinationImage; 

甚至放棄兼容圖像:

AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); 
BufferedImage destinationImage = op.filter(image, null); 
return destinationImage; 

而且我不知道雙三次是那麼重要,但可能不是問題。

由於兼容圖像返回與阿爾法圖像即透明

Graphics2D g = destinationImage.createGraphics(); 
g.setBackground(Color.WHITE); 
g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight()); 

將投入的透明性的層中的圖像上;隨後畫的圖像與白色融合在一起。

1

感謝所有幫助:-) 變化後轉換功能這一點,問題就解決了,不知道爲什麼是這種情況,gpasch可能是正確的

public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception { 

    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); 
    BufferedImage destinationImage = new BufferedImage(image.getWidth(),image.getHeight(), image.getType()); 
    destinationImage = op.filter(image, destinationImage); 
    return destinationImage; 
}