10

AffineTransformクラスを使用してJavaで画像を回転させる際に問題が発生します。

画像の回転(90度)コピーを作成するには、次の方法があります。

private BufferedImage createRotatedCopy(BufferedImage img, Rotation rotation) {
    int w = img.getWidth();
    int h = img.getHeight();

    BufferedImage rot = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);

    double theta;
    switch (rotation) {
        case CLOCKWISE:
            theta = Math.PI / 2;
            break;
        case COUNTERCLOCKWISE:
            theta = -Math.PI / 2;
            break;
        default:
            throw new AssertionError();
    }

    AffineTransform xform = AffineTransform.getRotateInstance(theta, w / 2, h / 2);
    Graphics2D g = (Graphics2D) rot.createGraphics();
    g.drawImage(img, xform, null);
    g.dispose();

    return rot;
}

ローテーションは、値がNONE、CLOCKWISE、およびCOUNTERCLOCKWISEの単純な列挙型です。

私の問題の症状はここに表示されます:

http://perp.se/so/rotate_problems.html

したがって、回転は正常に機能しますが、結果の画像は正しい座標(またはどのように配置するか)に固定されません。そもそも自分が何をしているのかわからないので(線形代数が弱い)、自分でこれを解決する方法がわかりません。

AffineTransformインスタンスをランダムにいじってみましたが、(もちろん)役に立ちませんでした。私はグーグル(そしてSOの検索)を試しましたが、私が見たすべての例は基本的に私と同じアプローチを使用しています...それは私にとってはうまくいきません。

アドバイスありがとうございます。

4

4 に答える 4

18

変換を単一の回転として表現する必要がある場合、アンカーポイントは回転の方向(またはのいずれか)によって異なり(w/2, w/2)ます(h/2, h/2)

translate; rotate; translateしかし、おそらく次のように表現する方が簡単です。

AffineTransform xform = new AffineTransform();
xform.translate(0.5*h, 0.5*w);
xform.rotate(theta);
xform.translate(-0.5*w, -0.5*h);

また、getQuadrantRotateInstanceの代わりに使用することを検討してgetRotateInstanceください。

于 2010-02-13T10:45:50.153 に答える
3

必要なのは90度の回転だけなので、AffineTransformのものの使用を避けることができます。

public BufferedImage rotate90DX(BufferedImage bi) {
    int width = bi.getWidth();
    int height = bi.getHeight();
    BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
    for(int i=0; i<width; i++)
        for(int j=0; j<height; j++)
            biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
    return biFlip;
}

これにより、長方形の画像のエッジが切り取られることも回避されます。

差出人:http ://snippets.dzone.com/posts/show/2936

于 2011-07-07T12:37:55.323 に答える
1

別のアプローチを試して、画像からアイコンを作成してから、回転アイコンを使用することもできます。

または、Sunフォーラムで見つけたこの古いコードを試すことができます。

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;

public class RotateImage {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://blogs.oracle.com/jag/resource/JagHeadshot-small.jpg");
        BufferedImage original = ImageIO.read(url);
        GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage rotated1 = tilt(original, -Math.PI/2, gc);
        BufferedImage rotated2 = tilt(original, +Math.PI/4, gc);
        BufferedImage rotated3 = tilt(original, Math.PI, gc);
        display(original, rotated1, rotated2, rotated3);
    }

    public static BufferedImage tilt(BufferedImage image, double angle, GraphicsConfiguration gc) {
        double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
        int w = image.getWidth(), h = image.getHeight();
        int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
        int transparency = image.getColorModel().getTransparency();
        BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
        Graphics2D g = result.createGraphics();
        g.translate((neww-w)/2, (newh-h)/2);
        g.rotate(angle, w/2, h/2);
        g.drawRenderedImage(image, null);
        return result;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }

    public static void display(BufferedImage im1, BufferedImage im2, BufferedImage im3, BufferedImage im4) {
        JPanel cp = new JPanel(new GridLayout(2,2));
        addImage(cp, im1, "original");
        addImage(cp, im2, "rotate -PI/2");
        addImage(cp, im3, "rotate +PI/4");
        addImage(cp, im4, "rotate PI");

        JFrame f = new JFrame("RotateImage");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(cp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static void addImage(Container cp, BufferedImage im, String title) {
        JLabel lbl = new JLabel(new ImageIcon(im));
        lbl.setBorder(BorderFactory.createTitledBorder(title));
        cp.add(lbl);
    }
}
于 2010-02-13T16:12:23.350 に答える
0

これがあなたの問題かもしれないかどうかはわかりません。

AffineTransform xform = AffineTransform.getRotateInstance(theta, w / 2, h / 2);

やってみませんか?

AffineTransform xform = AffineTransform.getRotateInstance(theta);

また

g.transform(AffineTransform.getRotateInstance(theta));
g.drawImage(img, 0, 0, w/2, h/2, null, null);
于 2010-02-13T10:12:52.290 に答える