0

I am using following code to rotate an given image.

public int getImageWidth(BufferedImage img) {
        if (rotate == Rotate.UPSIDE_DOWN || rotate == Rotate.ABOUT_CENTER)
            return img.getWidth();
        else
            return img.getHeight();
    }





public int getImageHeight(BufferedImage img) {

        if (rotate == Rotate.UPSIDE_DOWN || rotate == Rotate.ABOUT_CENTER)
            return img.getHeight();
        else
            return img.getWidth();

    }

This is the Rotation enums

public enum Rotate {
        DOWN, UP, UPSIDE_DOWN, ABOUT_CENTER;
    }

Actual rotation method

public BufferedImage rotateImage(BufferedImage source, int x, int y,
            float orientation) throws Exception {

        int newWidth = getImageWidth(source);
        int newHeight = getImageHeight(source);
        int cWidth = newWidth / 2;
        int cHeight = newHeight / 2;
        int imgType = source.getType() == 0 ? 5 : source.getType();

        BufferedImage result = new BufferedImage(getImageWidth(source),
                getImageHeight(source),imgType);
        Graphics2D g2 = result.createGraphics();

        if (rotate == Rotate.DOWN) {
            g2.translate(x + cHeight, y + cWidth);
            g2.rotate(Math.toRadians(90));
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
        } else if (rotate == Rotate.UP) {
            g2.translate(x + cHeight, y + cWidth);
            g2.rotate(Math.toRadians(-90));
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
        } else if (rotate == Rotate.UPSIDE_DOWN) {
            g2.translate(x + cWidth, y + cHeight);
            g2.rotate(Math.toRadians(180));
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
        } else if (rotate == Rotate.ABOUT_CENTER) {
            Rectangle r = new Rectangle(x, y, newWidth, newHeight);
            g2.setClip(r);
            AffineTransform original = g2.getTransform();
            AffineTransform at = new AffineTransform();
            at.concatenate(original);
            at.rotate(orientation, x + cWidth, y + cHeight);
            g2.setTransform(at);
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
            g2.setTransform(original);
        }

        g2.dispose();
        g2 = null;

        return result;

    }

The client code

// rotate derived & filtered image to 90 degree
        // using Affine transform
        setRotate(Rotate.UP);

        BufferedImage rSubImage = rotateImage(fSubImage, 0, 0, -90);

Now the following is an source image,

Source Image

When i rotate this image using above code , the result is very strange

Wrong Rotated Image

What did i do wrong ?

also the quality is lost after rotation , please notice it.

Mihir
  • 2,480
  • 7
  • 38
  • 57
  • You need to adjust the positioning of the rotation matrix. Take a look at [this example](http://stackoverflow.com/questions/15779877/rotate-bufferedimage-inside-jpanel/15780090#15780090), in particular the second example – MadProgrammer Aug 23 '13 at 06:52
  • `I am using following code` - looks a lot like the code from: [Rotated Icon](http://tips4java.wordpress.com/2009/04/06/rotated-icon/) except the bits and pieces you post don't make any sense. The least you can do is post a proper SSCCE so people can see the entire code you changed. – camickr Aug 23 '13 at 22:03
  • @camickr do you have any problem if i use someone else code ? if you don't want to give answer here why the you put comment here ? – Mihir Aug 24 '13 at 09:08
  • @Mihir, code is posted to be shared. Normally people would say I found this code (provide link) that rotates an Icon. I'm trying to modify it to rotate an image but am having problems... I also get frustrated when people don't bother to respond/accept answers when given help: http://stackoverflow.com/q/18404864/131872. Finally I can't give an answer based on the information available. My suggestion was to post a proper question which would always include a SSCCE. As I stated the code you posted makes no sense. You get what you give. – camickr Aug 24 '13 at 15:10

1 Answers1

0

I think the solution is to use AffineTranform, translating the Image to reamin in the center. I modified your code and tested it, for me it works:

public BufferedImage rotateImage(BufferedImage source, int x, int y,
        float orientation) throws Exception {

    int newWidth = getImageWidth(source);
    int newHeight = getImageHeight(source);
    int imgType = source.getType() == 0 ? 5 : source.getType();

    BufferedImage result = new BufferedImage(getImageWidth(source),
            getImageHeight(source), imgType);

    if (rotate == Rotate.DOWN) {

        AffineTransform tranform = new AffineTransform();
        tranform.translate(newWidth / 2, newHeight / 2);
        tranform.rotate(Math.toRadians(90));
        tranform.translate(-source.getWidth()/2, -source.getHeight()/2);
        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(source, tranform, null);

    } else if (rotate == Rotate.UP) {

        AffineTransform tranform = new AffineTransform();
        tranform.translate(newWidth / 2, newHeight / 2);
        tranform.rotate(Math.toRadians(-90));
        tranform.translate(-source.getWidth()/2, -source.getHeight()/2);
        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(source, tranform, null);

    } else if (rotate == Rotate.UPSIDE_DOWN) {

        AffineTransform tranform = new AffineTransform();
        tranform.translate(newWidth / 2, newHeight / 2);
        tranform.rotate(Math.toRadians(180));
        tranform.translate(-source.getWidth()/2, -source.getHeight()/2);
        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(source, tranform, null);


    } else if (rotate == Rotate.ABOUT_CENTER) {

        //......

    }



    return result;

}

Hope it helps!

razcor
  • 355
  • 2
  • 11