Rotate a buffered image in Java

I am trying to rotate a buffered image in java. Here is the code I'm using:

public static BufferedImage rotate(BufferedImage bimg, double angle){
    int w = bimg.getWidth();    
    int h = bimg.getHeight();
    Graphics2D graphic = bimg.createGraphics();
    graphic.rotate(Math.toRadians(angle), w/2, h/2);
    graphic.drawImage(bimg, null, 0, 0);
    graphic.dispose();
    return bimg;
}

      

I have looked through numerous stack overflow issues and answers on this topic and have not been able to figure out why the image is shredded when I try to rotate it. Here is an example showing an uploaded image: uploaded image

After I click the rotate button that calls the above function with a buffered image and 90.0 for the angle: the image is split

Can someone help me understand what is happening and how to fix it?

Thank!

+1


source to share


3 answers


As always, the Internet is there to help. So this is some code I have put together along with other resources / posts / blogs that will return a new image sized to contain a rotated image

public BufferedImage rotateImageByDegrees(BufferedImage img, double angle) {

            double rads = Math.toRadians(angle);
            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 = w / 2;
            int y = h / 2;

            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;
        }

      

Updated

So, using this PNG:

Image



And this code ...

package javaapplication1.pkg040;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage master;
        private BufferedImage rotated;

        public TestPane() {
            try {
                master = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/Miho_Small.png"));
                rotated = rotateImageByDegrees(master, 0.0);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            Timer timer = new Timer(40, new ActionListener() {
                private double angle = 0;
                private double delta = 1.0;

                @Override
                public void actionPerformed(ActionEvent e) {
                    angle += delta;
                    rotated = rotateImageByDegrees(master, angle);
                    repaint();
                }
            });
            timer.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (rotated != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - rotated.getWidth()) / 2;
                int y = (getHeight() - rotated.getHeight()) / 2;
                g2d.drawImage(rotated, x, y, this);
                g2d.dispose();
            }
        }

        public BufferedImage rotateImageByDegrees(BufferedImage img, double angle) {

            double rads = Math.toRadians(angle);
            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 = w / 2;
            int y = h / 2;

            at.rotate(rads, x, y);
            g2d.setTransform(at);
            g2d.drawImage(img, 0, 0, this);
            g2d.dispose();

            return rotated;
        }
    }

}

      

I can create something like ...

Rotating

+13


source


You get the jumbled image result because you are drawing the rotated image in the input image itself. Instead, you need to create graphics from a new BufferedImage.

public static BufferedImage rotate(BufferedImage bimg, double angle) {

    int w = bimg.getWidth();    
    int h = bimg.getHeight();

    BufferedImage rotated = new BufferedImage(w, h, bimg.getType());  
    Graphics2D graphic = rotated.createGraphics();
    graphic.rotate(Math.toRadians(angle), w/2, h/2);
    graphic.drawImage(bimg, null, 0, 0);
    graphic.dispose();
    return rotated;
}

      



Note that if you want to avoid clipping corners, you need to adjust the width & height of the BufferedImage output.

+1


source


You will need to account for the resize and the new width and height for the output. See: fooobar.com/questions/2408107 / ...

-1


source







All Articles