Java Utililty Methods BufferedImage Rotate

List of utility methods to do BufferedImage Rotate

Description

The list of methods to do BufferedImage Rotate are organized into topic(s).

Method

BufferedImagerotateByRightAngle(BufferedImage source, int degrees)
Rotates given image by given degrees which should be a multiple of 90
assert degrees % 90 == 0;
degrees = degrees % 360;
int w = source.getWidth();
int h = source.getHeight();
int w1, h1;
switch (degrees) {
case 90:
case 270:
...
BufferedImagerotateFortyFiveClockwise(BufferedImage img)
Rotate img 45 degrees clockwise.
double piOverFour = Math.PI / 4.0;
int w = img.getWidth();
int h = img.getHeight();
int diag = (int) Math.sqrt(w * w + h * h) + 2; 
BufferedImage rotated = new BufferedImage(diag, diag, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) rotated.getGraphics();
AffineTransform trans = g2d.getTransform();
trans.translate(diag / 2, 0);
...
BufferedImagerotateImage(BufferedImage image, double angle)
rotate Image
int width = image.getWidth();
int height = image.getHeight();
BufferedImage destinationImage = new BufferedImage(width, height, image.getType());
Graphics2D graphics = destinationImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.rotate(Math.toRadians(angle), width / 2, height / 2);
...
BufferedImagerotateImage(BufferedImage image, double angle)
Rotates an image.
double theta = Math.toRadians(angle);
double sin = Math.abs(Math.sin(theta));
double cos = Math.abs(Math.cos(theta));
int w = image.getWidth();
int h = image.getHeight();
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
...
BufferedImagerotateImage(BufferedImage image, double radians)
rotate Image
BufferedImage rotatedImage = new BufferedImage(image.getWidth(), image.getHeight(),
        BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2d = rotatedImage.createGraphics();
g2d.rotate(radians);
g2d.setBackground(Color.WHITE);
int maxOfWidthHieght = Math.max(image.getWidth(null), image.getHeight(null));
g2d.clearRect(-5 * maxOfWidthHieght, -5 * maxOfWidthHieght, 10 * maxOfWidthHieght, 10 * maxOfWidthHieght);
g2d.drawImage(image, 0, 0, Color.WHITE, null);
...
BufferedImagerotateImage(BufferedImage image, float angle)
the graphic2d changes, must use the new one for future operations.
AffineTransform tx = new AffineTransform();
tx.rotate(angle, image.getWidth() / 2, image.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
return op.filter(image, null);
BufferedImagerotateImage(BufferedImage image, int cwRotationNeeded)
rotate Image
double angle = Math.toRadians(cwRotationNeeded);
int type = thumbnailType(image);
double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int width = image.getWidth();
int height = image.getHeight();
int newWidth = (int) Math.floor(width * cos + height * sin);
int newHeight = (int) Math.floor(height * cos + width * sin);
...
BufferedImagerotateImage(BufferedImage imageToRotate, float degrees)
Rotates 90, -90 deegrees
if (degrees != 90.0 && degrees != -90.0)
    throw new IllegalArgumentException("Only implemented +90 and -90 rotation");
BufferedImage rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null),
        imageToRotate.getType());
Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
g2d.rotate(Math.toRadians(degrees));
g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);
return rotatedImage;
...
BufferedImagerotateImage(BufferedImage img, double angle)
rotateImage
   public static BufferedImage rotateImage(BufferedImage img,  double angle)

This method rotates the parsed image to the parsed angle.

int w = img.getWidth();
int h = img.getHeight();
AffineTransform tx = new AffineTransform();
tx.translate(w / 2, h / 2);
tx.rotate(angle);
tx.scale(1, 1);
tx.translate(-w / 2, -h / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
...
BufferedImagerotateImage(BufferedImage img, double angle, int type)
rotate Image
return rotateImage(img, angle, type, Color.BLACK);