我正在嘗試使用JAI在圖像上執行旋轉任務。我可以得到這個工作沒有問題。但是,圖像中的中間色調嚴重損失。圖像可以在photoshop中旋轉而不會缺少對比度。使用JAI旋轉灰度圖像增加對比度
請看下面的3張圖片在這裏堆疊在一起,看看我的意思;
頂部圖像是原始,中間是在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;
}