Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 23rd, 2013  |  syntax: None  |  size: 4.05 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public BufferedImage transform(BufferedImage image){
  2.  
  3.     System.out.println("Input width: "+ image.getWidth());
  4.     System.out.println("Input height: "+ image.getHeight());
  5.  
  6.     AffineTransform affineTransform = new AffineTransform();
  7.     affineTransform.setToQuadrantRotation(1, image.getWidth() / 2, image.getHeight() / 2);
  8.  
  9.     AffineTransformOp opRotated = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR);
  10.     BufferedImage transformedImage = opRotated.createCompatibleDestImage(image, image.getColorModel());
  11.     System.out.println("Resulting width: "+ transformedImage.getWidth());
  12.     System.out.println("Resulting height: "+ transformedImage.getHeight());
  13.  
  14.     transformedImage = opRotated.filter(image, transformedImage);
  15.     return transformedImage;
  16. }
  17.        
  18. import java.awt.geom.AffineTransform;
  19. import java.awt.image.AffineTransformOp;
  20. import java.awt.image.BufferedImage;
  21. import java.io.IOException;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24.  
  25. import javax.imageio.ImageIO;
  26. import javax.swing.ImageIcon;
  27. import javax.swing.JLabel;
  28. import javax.swing.JOptionPane;
  29.  
  30. public class RotateImage {
  31.    public static final String IMAGE_PATH = "http://duke.kenai.com/"
  32.          + "models/Duke3DprogressionSmall.jpg";
  33.  
  34.    public static void main(String[] args) {
  35.       try {
  36.          URL imageUrl = new URL(IMAGE_PATH);
  37.          BufferedImage img0 = ImageIO.read(imageUrl);
  38.          ImageIcon icon0 = new ImageIcon(img0);
  39.  
  40.          int numquadrants = 1;
  41.          BufferedImage img1 = transform(img0, numquadrants );
  42.          ImageIcon icon1 = new ImageIcon(img1);
  43.  
  44.          JOptionPane.showMessageDialog(null, new JLabel(icon0));
  45.          JOptionPane.showMessageDialog(null, new JLabel(icon1));
  46.  
  47.       } catch (MalformedURLException e) {
  48.          e.printStackTrace();
  49.       } catch (IOException e) {
  50.          e.printStackTrace();
  51.       }
  52.    }
  53.  
  54.    public static BufferedImage transform(BufferedImage image, int numquadrants) {
  55.       int w0 = image.getWidth();
  56.       int h0 = image.getHeight();
  57.       int w1 = w0;
  58.       int h1 = h0;
  59.  
  60.       int centerX = w0 / 2;
  61.       int centerY = h0 / 2;
  62.  
  63.       if (numquadrants % 2 == 1) {
  64.          w1 = h0;
  65.          h1 = w0;
  66.       }
  67.  
  68.       if (numquadrants % 4 == 1) {
  69.          if (w0 > h0) {
  70.             centerX = h0 / 2;
  71.             centerY = h0 / 2;
  72.          } else if (h0 > w0) {
  73.             centerX = w0 / 2;
  74.             centerY = w0 / 2;
  75.          }
  76.          // if h0 == w0, then use default
  77.       } else if (numquadrants % 4 == 3) {
  78.          if (w0 > h0) {
  79.             centerX = w0 / 2;
  80.             centerY = w0 / 2;
  81.          } else if (h0 > w0) {
  82.             centerX = h0 / 2;
  83.             centerY = h0 / 2;
  84.          }
  85.          // if h0 == w0, then use default
  86.       }
  87.  
  88.       AffineTransform affineTransform = new AffineTransform();
  89.       affineTransform.setToQuadrantRotation(numquadrants, centerX, centerY);
  90.  
  91.       AffineTransformOp opRotated = new AffineTransformOp(affineTransform,
  92.             AffineTransformOp.TYPE_BILINEAR);
  93.  
  94.       BufferedImage transformedImage = new BufferedImage(w1, h1,
  95.             image.getType());
  96.  
  97.       transformedImage = opRotated.filter(image, transformedImage);
  98.       return transformedImage;
  99.    }
  100. }
  101.        
  102. public static BufferedImage rotateImage(BufferedImage image, int quadrants) {
  103.  
  104.     int w0 = image.getWidth();
  105.     int h0 = image.getHeight();
  106.     int w1 = w0;
  107.     int h1 = h0;
  108.     int centerX = w0 / 2;
  109.     int centerY = h0 / 2;
  110.  
  111.     if (quadrants % 2 == 1) {
  112.         w1 = h0;
  113.         h1 = w0;
  114.     }
  115.  
  116.     if (quadrants % 4 == 1) {
  117.         centerX = h0 / 2;
  118.         centerY = h0 / 2;
  119.     } else if (quadrants % 4 == 3) {
  120.         centerX = w0 / 2;
  121.         centerY = w0 / 2;
  122.     }
  123.  
  124.     AffineTransform affineTransform = new AffineTransform();
  125.     affineTransform.setToQuadrantRotation(quadrants, centerX, centerY);
  126.     AffineTransformOp opRotated = new AffineTransformOp(affineTransform,
  127.             AffineTransformOp.TYPE_BILINEAR);
  128.     BufferedImage transformedImage = new BufferedImage(w1, h1,
  129.             image.getType());
  130.     transformedImage = opRotated.filter(image, transformedImage);
  131.  
  132.     return transformedImage;
  133.  
  134. }