Rotating BufferedImage to left or right

If you need to rotate an BufferedImage to left or right you can use the following code

BufferedImage originalImage = // some image that you have as file / stream.
BufferedImage rotatedImage = new BufferedImage(originalImage.getHeight(), originalImage.getWidth(), originalImage.getType());

for (int row = 0; row < originalImage.getWidth(); row++) {
     for (int column = 0; column < originalImage.getHeight(); column++) {
         if (rotateLeft) {
              rotatedImage.setRGB(column, originalImage.getWidth() - row - 1, originalImage.getRGB(row, column));
         } else {
              rotatedImage.setRGB(originalImage.getHeight() - column - 1, row, originalImage.getRGB(row, column));
         }
     }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s