0

I'm working on an image processing app to rotate and image 90 degrees with this function, and could use some help with the logic. Integers i and j are coordinates, height and width are the total pixel height and width, rgb array is the color vavlues that need to be re-written into the new array. I know I need an array with height and width swapped, and I need to write the new color values into the correct locations, but I don't really know what to do.

Here is the function, any help would be appreciated.

private void rotate()

{

for(int i=0; i<height; i++)

   for(int j=0; j<width; j++)

   {    

      int rgbArray[] = new int[4];

      rgbArray = getPixelArray(picture[i][j]);

       picture[i][j] = getPixels(rgbArray);
    } 
 resetPicture();

}

  • I've posted solution to this here: [link](https://stackoverflow.com/questions/8639567/java-rotating-images/69770584#69770584) – Dave The Dane Oct 29 '21 at 15:11

1 Answers1

0

I found a simple working example at https://blog.idrsolutions.com/2019/05/image-rotation-in-java/:

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageRotator {

    public static void main(String[] args) throws  Exception {
        final double rads = Math.toRadians(90);
        final double sin = Math.abs(Math.sin(rads));
        final double cos = Math.abs(Math.cos(rads));

        File orgImage = new File("test.jpg");
        BufferedImage image = ImageIO.read(orgImage);

        final int w = (int) Math.floor(image.getWidth() * cos + image.getHeight() * sin);
        final int h = (int) Math.floor(image.getHeight() * cos + image.getWidth() * sin);
        final BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
        final AffineTransform at = new AffineTransform();
        at.translate(w / 2, h / 2);
        at.rotate(rads,0, 0);
        at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
        final AffineTransformOp rotateOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        rotateOp.filter(image,rotatedImage);



        ImageIO.write(rotatedImage, "JPG", new File(orgImage.getParentFile(),"new.jpg"));

    }
}