Java中的旋转BufferedImage

问题描述 投票:0回答:2

我一直在尝试使用for循环旋转图像。我的代码确实有效,但是此方法似乎不必要,并且图像在旋转时会丢失像素。有没有更简单的方法可以做到这一点?

//rotates image around center a degrees
    public void drawRotatedImage(Graphics g, BufferedImage image, double a, int x, int y, int pixelSize) {
        //origin point for rotation
        int rX = image.getWidth() / 2;
        int rY = image.getHeight() / 2;

        for(int x1 = 0; x1 < image.getWidth(); x1++) {
            for(int y1 = 0; y1 < image.getHeight(); y1++) {
                int c = image.getRGB(x1, y1);
                //rotating point
                int nX = (int) (((x1-rX) * Math.cos(a)) - ((y1-rY) * Math.sin(a)));
                int nY = (int) (((x1-rX) * Math.sin(a)) + ((y1-rY) * Math.cos(a)));
                g.setColor(new Color(c));
                //drawing each pixel
                g.fillRect((nX*pixelSize) + x, (nY*pixelSize) + y, pixelSize, pixelSize);
            }
        }
    }
java for-loop methods graphics bufferedimage
2个回答
1
投票

我创建了一个简单的示例程序(改编自Rotating BufferedImage instances

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Test");

        frame.add(new JComponent() {

            BufferedImage image = ImageIO.read(new URL(
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/%D0%9D%D0%B5%D1%80%D0%BF%D0%B8%D1%87%D0%B8%D0%B9_%D0%B2%D0%B7%D0%B3%D0%BB%D1%8F%D0%B4.jpg/500px-%D0%9D%D0%B5%D1%80%D0%BF%D0%B8%D1%87%D0%B8%D0%B9_%D0%B2%D0%B7%D0%B3%D0%BB%D1%8F%D0%B4.jpg"));

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);

                AffineTransform at = new AffineTransform();
                at.translate(getWidth() / 2, getHeight() / 2); // draw image in center of frame
                // at.scale(0.5, 0.5);
                at.rotate(Math.toRadians(180));
                at.translate(-image.getWidth() / 2, -image.getHeight() / 2); // rotate around the center

                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(image, at, null);

            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 800);
        frame.setVisible(true);
    }
}

0
投票

尝试使用AffineTransform

获取旋转的实例

static void rotate(BufferedImage from, BufferedImage to, double rotate)
{
  double x = from.getWidth() /2;
  double y = from.getHeight() /2;

  // rotate around the center
  double f = rotate; // 6.284 is full turn
  AffineTransform trans = AffineTransform.getRotateInstance(f, x, y);
  AffineTransformOp op = new AffineTransformOp(trans,
                AffineTransformOp.TYPE_BILINEAR);
  op.filter(from, to);
}

或如果您只想显示它

static void rotate(Graphics2d g2d, BufferedImage img, double rotate)
{
  double x = from.getWidth() /2;
  double y = from.getHeight() /2;

  // rotate around the center
  double f = rotate; // 6.284 is full turn
  AffineTransform trans = AffineTransform.getRotateInstance(f, x, y);
  g2d.drawImage(img, trans, null);
}
© www.soinside.com 2019 - 2024. All rights reserved.