2

JAI を使用して画像の回転タスクを実行しようとしています。私はこれを問題なく動作させることができます。ただし、画像の中間調が大幅に失われています。このコントラストの欠如なしに、画像を Photoshop で回転させることができます。

私が何を意味するかを見るために、ここで互いに隣り合わせに積み重ねられた次の3つの画像を見てください。

http://imgur.com/SYPhZ.jpg

上の画像はオリジナルで、真ん中はそれが可能であることを証明するためにフォトショップで回転させたもので、下は私のコードの結果です。

実際の画像はこちらをご覧ください。

回転前: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;
}
4

1 に答える 1

1

これは明らかに、しばらく前から存在していた JAI のバグです。

この問題について私が見つけた最初の言及はここに表示されます。その元の記事は、古いjai-coreの問題をここで指摘しています。その解決策を読んだところ、根本的なバグがまだ未解決であり、ここで説明されているようです

その検出作業のすべてがアプリケーションに関連するかどうかに関係なく、JAI がテスト コードに使用しているデフォルトよりも寛容な色空間を構築できる可能性があります。

最悪の場合、ピクセル トラバーサルを自分で記述して、回転した画像を作成することもできます。これは最適な解決策ではありませんが、今日この問題の解決策が絶対に必要な場合は、完全を期すために言及します。

于 2010-05-17T17:20:52.953 に答える