Choose Your Language

Thursday, 17 April 2014

java program to rotate image

package imaging;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Aravind Sankaran
 */
public class RotateImage {
    public  static void main(String args[]){
        try {    
        String path="C:\\images\\ppp.jpg";
        File fileInput=new File(path);
        BufferedImage  img = ImageIO.read(new File(path)); // try/catch IOException
        int width = img.getWidth();
        int height = img.getHeight();        
        String fileName=fileInput.getName();
        fileName=fileName.substring(0, fileName.lastIndexOf('.'));
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bufferedImage.createGraphics();
      
// draw graphics     
        double angle=45.0;     
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.rotate(Math.toRadians(angle), width, width);
        g2d.drawImage(img, 0, 0, null);        
        g2d.dispose();
//save as png
File file = new File("C:\\images\\"+fileName+".png");
ImageIO.write(bufferedImage, "png", file);
     } catch (IOException ex) {
      
     }

    }
}
input:

output:


No comments:

Post a Comment