2010-05-17 73 views
2

我正在嘗試使用JAI在圖像上執行旋轉任務。我可以得到這個工作沒有問題。但是,圖像中的中間色調嚴重損失。圖像可以在photoshop中旋轉而不會缺少對比度。使用JAI旋轉灰度圖像增加對比度

請看下面的3張圖片在這裏堆疊在一起,看看我的意思;

http://imgur.com/SYPhZ.jpg

頂部圖像是原始,中間是在photoshop中旋轉,以證明是可以做到的,底部是從我的代碼的結果。

要看到實際的圖像,請看這裏;

旋轉前:http://imgur.com/eiAOO.jpg 旋轉後:http://imgur.com/TTUKS.jpg

你可以看到這個問題最清楚,如果你在兩個不同的標籤加載圖像,以及它們之間的一抖。

在代碼方面,我加載圖像如下;

public void testIt() throws Exception { 

    File source = new File("c:\\STRIP.jpg"); 
    FileInputStream fis = new FileInputStream(source); 
    BufferedImage sourceImage = ImageIO.read(fis); 
    fis.close(); 

    BufferedImage rotatedImage = doRotate(sourceImage, 15); 
    FileOutputStream output = new FileOutputStream("c:\\STRIP_ROTATED.jpg"); 
    ImageIO.write(rotatedImage, "JPEG", output); 

} 

然後這裏是旋轉函數;

public BufferedImage doRotate(BufferedImage input, int angle) { 
    int width = input.getWidth(); 
    int height = input.getHeight(); 


    double radians = Math.toRadians(angle/10.0); 

    // Rotate about the input image's centre 
    AffineTransform rotate = AffineTransform.getRotateInstance(radians, width/2.0, height/2.0); 

    Shape rect = new Rectangle(width, height); 

    // Work out how big the rotated image would be.. 
    Rectangle bounds = rotate.createTransformedShape(rect).getBounds(); 

    // Shift the rotated image into the centre of the new bounds 
    rotate.preConcatenate(
      AffineTransform.getTranslateInstance((bounds.width - width)/2.0, (bounds.height - height)/2.0)); 

    BufferedImage output = new BufferedImage(bounds.width, bounds.height, input.getType()); 
    Graphics2D g2d = (Graphics2D) output.getGraphics(); 

    // Fill the background with white 
    g2d.setColor(Color.WHITE); 
    g2d.fill(new Rectangle(width, height)); 

    RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
    hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

    g2d.setRenderingHints(hints); 
    g2d.drawImage(input, rotate, null); 

    return output; 
} 

回答

1

這顯然是在JAI一個bug已經存在了一段時間:

最早的提及,我能找到這個問題appears here的。那篇原文指向old jai-core issue here。閱讀該決議後,似乎仍有一個根目錄錯誤仍然是打開的,並且described here

無論所有偵測工作是否與您的應用程序相關,都可以構建比JAI用於測試代碼的默認色彩空間容忍度更高的色彩空間。

在絕對最差的情況下,您可以自己編寫像素遍歷來創建旋轉圖像。這不是最佳的解決方案,但如果你今天絕對需要解決這個問題,我會提到它的完整性。