我在java中旋转图像但想保存旋转图像

时间:2017-06-28 10:01:51

标签: java image swing graphics2d

我正在使用AffineTransform旋转图像,我曾尝试通过ImageIo.write保存旋转的图像,它保存图像但不保存原始图像的旋转图像。所以请提前告诉我如何纠正它。

这是我的旋转和保存图像的代码

公共类图片{

public static void main(String[] args) {
    new Image();
}

public Image() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
            }

            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}
public class TestPane extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JSlider slider;
    private BufferedImage image;

    public TestPane() {
        setLayout(new BorderLayout());

        try {
           File imagefile = new File("C:/pics/1206.jpg");
              image = ImageIO.read(imagefile);


              ImageIO.write(image, "jpg",new File("C:/pics"));
              ImageIO.write(image, "bmp",new File("C:/pics"));
              ImageIO.write(image, "gif",new File("C:/picsf"));
              ImageIO.write(image, "png",new File("C:/pics"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        slider = new JSlider();
        slider.setMinimum(0);
        slider.setMaximum(360);
        slider.setMinorTickSpacing(5);
        slider.setMajorTickSpacing(10);
        slider.setValue(0);
        add(slider, BorderLayout.SOUTH);
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                repaint();
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        return image == null ? new Dimension(200, 200) : new Dimension(image.getWidth(), image.getHeight());
    }

    public double getAngle() {

        return Math.toRadians(slider.getValue());

    }

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

        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setColor(Color.RED);
        g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
        g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

        g2d.setColor(Color.BLACK);
        int x = (getWidth() - image.getWidth()) / 2;
        int y = (getHeight() - image.getHeight()) / 2;
        AffineTransform at = new AffineTransform();
        at.setToRotation(getAngle(), x + (image.getWidth() / 2), y + (image.getHeight() / 2));
        at.translate(x, y);
        g2d.setTransform(at);
        g2d.drawImage(image, 0, 0, this);
        g2d.dispose();
    }
    }
}

1 个答案:

答案 0 :(得分:1)

让我们开始,这不是旋转图像,它会旋转用于显示图像的Graphics上下文,它不会影响图像原始图像

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

    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.RED);
    g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

    g2d.setColor(Color.BLACK);
    int x = (getWidth() - image.getWidth()) / 2;
    int y = (getHeight() - image.getHeight()) / 2;
    AffineTransform at = new AffineTransform();
    at.setToRotation(getAngle(), x + (image.getWidth() / 2), y + (image.getHeight() / 2));
    at.translate(x, y);
    g2d.setTransform(at);
    g2d.drawImage(image, 0, 0, this);
    g2d.dispose();
}

然后这......

  try {
        File imagefile = new File("C:/pics/1206.jpg");
        image = ImageIO.read(imagefile);


        ImageIO.write(image, "jpg",new File("C:/pics"));
        ImageIO.write(image, "bmp",new File("C:/pics"));
        ImageIO.write(image, "gif",new File("C:/picsf"));
        ImageIO.write(image, "png",new File("C:/pics"));
  } catch (IOException ex) {
        ex.printStackTrace();
  }

只是将原始图像保存为多种不同的格式...到同一个文件,但对角度的任何变化都没有反应

相反,你需要使用从原件生成新图像的东西,旋转你想要的数量......

  public BufferedImage rotateImageByDegrees(BufferedImage img, double degrees) {
        double rads = Math.toRadians(degrees);
        double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads));
        int w = img.getWidth();
        int h = img.getHeight();
        int newWidth = (int) Math.floor(w * cos + h * sin);
        int newHeight = (int) Math.floor(h * cos + w * sin);

        BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = rotated.createGraphics();
        AffineTransform at = new AffineTransform();
        at.translate((newWidth - w) / 2, (newHeight - h) / 2);

        int x = clickPoint == null ? w / 2 : clickPoint.x;
        int y = clickPoint == null ? h / 2 : clickPoint.y;

        at.rotate(rads, x, y);
        g2d.setTransform(at);
        g2d.drawImage(img, 0, 0, this);
        g2d.setColor(Color.RED);
        g2d.drawRect(0, 0, newWidth - 1, newHeight - 1);
        g2d.dispose();

        return rotated;
  }

然后你可以保存它......

File imagefile = new File("C:/pics/1206.jpg");
image = ImageIO.read(imagefile);

BufferedImage rotated = rotateImageByDegrees(image, 22.5);
ImageIO.write(rotated, "png", new File("RotatedBy225.png"));

所以,下次你使用my previous examples中的一个时,我告诉你它没有按你的想法/想要的那样做,我希望你能更好地理解我的意思,并且更加关注其他examples我们会告诉您