Java Image Rotate rotateImage(final File imagePath, int numquadrants)

Here you can find the source of rotateImage(final File imagePath, int numquadrants)

Description

Return image rotated as tempFile

License

Apache License

Parameter

Parameter Description
imagePath a parameter
numquadrants a parameter

Declaration

public static File rotateImage(final File imagePath, int numquadrants) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.imageio.ImageIO;

public class Main {
    /**/*from  ww w.  j a va2s .  c  om*/
     * Return image rotated as tempFile
     *
     * @param imagePath
     * @param numquadrants
     * @return
     */
    public static File rotateImage(final File imagePath, int numquadrants) {
        try {
            BufferedImage bufferedImage = ImageIO.read(imagePath);
            bufferedImage = transform(bufferedImage, numquadrants);
            File outputfile = Files.createTempFile("temp", "png").toFile();
            ImageIO.write(bufferedImage, "png", outputfile);
            return outputfile;
        } catch (IOException ex) {

        }
        return null;
    }

    public static BufferedImage transform(BufferedImage image, int numquadrants) {
        int w0 = image.getWidth();
        int h0 = image.getHeight();
        int w1 = w0;
        int h1 = h0;

        int centerX = w0 / 2;
        int centerY = h0 / 2;

        if (numquadrants % 2 == 1) {
            w1 = h0;
            h1 = w0;
        }

        if (numquadrants % 4 == 1) {
            if (w0 > h0) {
                centerX = h0 / 2;
                centerY = h0 / 2;
            } else if (h0 > w0) {
                centerX = w0 / 2;
                centerY = w0 / 2;
            }
            // if h0 == w0, then use default
        } else if (numquadrants % 4 == 3) {
            if (w0 > h0) {
                centerX = w0 / 2;
                centerY = w0 / 2;
            } else if (h0 > w0) {
                centerX = h0 / 2;
                centerY = h0 / 2;
            }
            // if h0 == w0, then use default
        }

        AffineTransform affineTransform = new AffineTransform();
        affineTransform.setToQuadrantRotation(numquadrants, centerX, centerY);

        AffineTransformOp opRotated = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR);

        BufferedImage transformedImage = new BufferedImage(w1, h1, image.getType());

        transformedImage = opRotated.filter(image, transformedImage);
        return transformedImage;
    }
}

Related

  1. rotate(Image image, double angle)
  2. rotate(Image img, double angle)
  3. rotate(Image img, double angle)
  4. rotate(Image img, float radian)
  5. Rotate(Image src, int angel)
  6. rotateImage(final Image img, final Point location, final double degrees)
  7. rotateImage(Image inImage, int inMaxWidth, int inMaxHeight, int inRotationDegrees)
  8. rotateImage(Image originalImage, double theta)
  9. rotateImage(String img_fn, int orient, String dest_fn)