Java BufferedImage Rotate rotate(BufferedImage img, int angle)

Here you can find the source of rotate(BufferedImage img, int angle)

Description

rotate

License

Open Source License

Declaration

public static BufferedImage rotate(BufferedImage img, int angle) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;

import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage rotate(BufferedImage img, int angle) {
        AffineTransform trans;// www .  j  a  v a 2  s  .c om
        BufferedImage newImg;
        int w = img.getWidth();
        int h = img.getHeight();
        if (angle == 0) {
            newImg = new BufferedImage(w, h, img.getType());
            trans = null;
        } else if (angle == 90) {
            newImg = new BufferedImage(h, w, img.getType());
            trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, h, 0.0);
        } else if (angle == 180) {
            newImg = new BufferedImage(w, h, img.getType());
            trans = new AffineTransform(-1.0, 0.0, 0.0, -1.0, w, h);
        } else if (angle == 270) {
            newImg = new BufferedImage(h, w, img.getType());
            trans = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, w);
        } else {
            throw new RuntimeException("Invalid angle " + angle);
        }
        Graphics2D g = newImg.createGraphics();
        try {
            if (trans != null) {
                g.transform(trans);
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            }
            g.drawImage(img, 0, 0, w, h, null);
        } finally {
            g.dispose();
        }
        return newImg;
    }
}

Related

  1. rotate(BufferedImage image, double theta)
  2. rotate(BufferedImage image, double theta, int anchorX, int anchorY)
  3. rotate(BufferedImage image, float theRadiansAngle)
  4. rotate(BufferedImage image, int degrees)
  5. rotate(BufferedImage img, int angle)
  6. rotate(BufferedImage img, int angle)
  7. rotate(BufferedImage img, int angle)
  8. rotate(BufferedImage img, int b, int a, int angle)
  9. rotate(BufferedImage source, int angle)