JAI を使用して画像の回転タスクを実行しようとしています。私はこれを問題なく動作させることができます。ただし、画像の中間調が大幅に失われています。このコントラストの欠如なしに、画像を Photoshop で回転させることができます。
私が何を意味するかを見るために、ここで互いに隣り合わせに積み重ねられた次の3つの画像を見てください。
上の画像はオリジナルで、真ん中はそれが可能であることを証明するためにフォトショップで回転させたもので、下は私のコードの結果です。
実際の画像はこちらをご覧ください。
回転前:http: //imgur.com/eiAOO.jpg 回転後:http: //imgur.com/TTUKS.jpg
2 つの異なるタブに画像を読み込み、それらの間をフリックすると、問題を最も明確に確認できます。
コードに関しては、次のように画像をロードします。
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;
}