Custom Angle rotation of Java image (example)

  • 2020-10-07 18:42:04
  • OfStack

The rotation of the image requires calling the rotate() method of the Graphics2D class, which rotates the image according to the specified radians.

The syntax is as follows:


rotate(double theta)

Where theta is the radian of rotation.

Note: This method takes only radians of rotation as a parameter. You can use the toRadians() method of the Math class to convert the Angle to radians. The toRadians() method takes an Angle value as an argument and returns the value of the converted radians.

Example code:


/** *//**
 *  Rotate the picture for the specified Angle 
 * 
 * @param bufferedimage
 *  Target image 
 * @param degree
 *  Rotation Angle 
 * @return
 */
 public static BufferedImage rotateImage(final BufferedImage bufferedimage,
 final int degree){
 int w= bufferedimage.getWidth();//  Get the image width. 
 int h= bufferedimage.getHeight();//  Get the height of the image. 
 int type= bufferedimage.getColorModel().getTransparency();//  Get image transparency. 
 BufferedImage img;//  Empty picture. 
 Graphics2D graphics2d;//  An empty brush. 
 (graphics2d= (img= new BufferedImage(w, h, type))
 .createGraphics()).setRenderingHint(
 RenderingHints.KEY_INTERPOLATION,
 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);//  Rotation, degree It's integer, degree, like vertical 90 Degrees. 
 graphics2d.drawImage(bufferedimage, 0, 0, null);//  from bufferedimagecopy The picture to img . 0,0 is img Coordinates. 
 graphics2d.dispose();
 return img;//  Return the copied image, the original image is still unchanged, no rotation, you can use it next time. 
 }

 /** *//**
 *  Changes the image to the specified size 
 * 
 * @param bufferedimage
 *  Target image 
 * @param w
 *  wide 
 * @param h
 *  high 
 * @return
 */
 public static BufferedImage resizeImage(final BufferedImage bufferedimage,
 final int w, final int h) {
 int type= bufferedimage.getColorModel().getTransparency();//  You get transparency. 
 BufferedImage img;//  Empty image. 
 Graphics2D graphics2d;//  Air brush. 
 (graphics2d= (img= createImage(w, h, type))
 .createGraphics()).setRenderingHint(
 RenderingHints.KEY_INTERPOLATION,
 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage
 .getWidth(), bufferedimage.getHeight(), null);
 graphics2d.dispose();
 return img;
 }

 /** *//**
 *  Horizontal flip image 
 * 
 * @param bufferedimage  Target image 
 * @return
 */
 public static BufferedImage flipImage(final BufferedImage bufferedimage){
 int w = bufferedimage.getWidth();//  You get the width. 
 int h = bufferedimage.getHeight();//  I get the height. 
 BufferedImage img;//  Empty image. 
 Graphics2D graphics2d;//  Air brush. 
 (graphics2d = (img = createImage(w, h, bufferedimage
 .getColorModel().getTransparency())).createGraphics())
 .drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
 graphics2d.dispose();
 return img;
 } 

conclusion


Related articles: