Original java realizes lossless and arbitrary rotation of pictures

Keywords: Java

This article mainly introduces in detail how to realize the image rotation at any angle without losing damage in java, which has certain reference value. Interested partners can refer to it.

Preface

When doing a project, we encounter a business need to rotate the picture, so we find a tool class, which is effective for personal testing. Here we share it with you, and we can use it directly when we need to use it!! ________

actual combat

I. Rotary Tool Class Code:

package zh.test.utils;
 
import java.awt.*;
import java.awt.image.BufferedImage;
 
/**
 * Picture Rotation Tool Class
 */
public class RotateImage {
 
 /**
 * Rotate the picture
 *
 * @param src Rotated picture
 * @param angel Rotation angle
 * @return Rotated pictures
 */
 public static BufferedImage Rotate(Image src, int angel) {
 int src_width = src.getWidth(null);
 int src_height = src.getHeight(null);
 //Calculate the size of the rotated picture
 Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
 src_width, src_height)), angel);
 BufferedImage res = null;
 res = new BufferedImage(rect_des.width, rect_des.height,
 BufferedImage.TYPE_INT_RGB);
 Graphics2D g2 = res.createGraphics();
 //Conversion
 g2.translate((rect_des.width - src_width) / 2,
 (rect_des.height - src_height) / 2);
 g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
 
 g2.drawImage(src, null, null);
 return res;
 }
 
 /**
 * Calculate the image after rotation
 *
 * @param src Rotated pictures
 * @param angel Rotation angle
 * @return Rotated pictures
 */
 public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
 //If the rotation angle is greater than 90 degrees, do the corresponding conversion.
 if (angel >= 90) {
 if (angel / 90 % 2 == 1) {
 int temp = src.height;
 src.height = src.width;
 src.width = temp;
 }
 angel = angel % 90;
 }
 
 double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
 double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
 double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
 double angel_dalta_width = Math.atan((double) src.height / src.width);
 double angel_dalta_height = Math.atan((double) src.width / src.height);
 
 int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
 - angel_dalta_width));
 int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
 - angel_dalta_height));
 int des_width = src.width + len_dalta_width * 2;
 int des_height = src.height + len_dalta_height * 2;
 return new Rectangle(new Dimension(des_width, des_height));
 }
}

2. Calling the code of the tool class:

package zh.test.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import zh.test.utils.RotateImage;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
 
/**
 * Test picture rotation
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {
 
 @RequestMapping(method = RequestMethod.POST)
 public void testImgRotate(MultipartFile multipartFile) throws Exception {
 BufferedImage src = ImageIO.read(multipartFile.getInputStream());
 //Rotate 90 degrees clockwise
 BufferedImage des1 = RotateImage.Rotate(src, 90);
 ImageIO.write(des1, "jpg", new File("e:/90.jpg"));
 //Rotate 180 degrees clockwise
 BufferedImage des2 = RotateImage.Rotate(src, 180);
 ImageIO.write(des2, "jpg", new File("c:/180.jpg"));
 //Rotate 270 degrees clockwise
 BufferedImage des3 = RotateImage.Rotate(src, 270);
 ImageIO.write(des3, "jpg", new File("c:/270.jpg"));
 
 }
 
}

Three, effect

1. Pictures rotated:

2. Rotate 90 degrees clockwise:

3. Clockwise rotation 180 degrees pictures:

4. Clockwise rotation 270 degrees pictures:

Conclusion:

1. Write code as carefully as possible. Don't be lucky to escape when you suspect something might go wrong. Murphy summed it up for us 60 years ago - -- Murphy's Law! ____________

2. Learn to jump out and think after encountering problems. Don't jump to conclusions easily, otherwise it's easy to get into them. Have a certain way of thinking to investigate, even if you are very sure that there are no problems, you should check carefully. Sometimes the problems often arise in those places that we overlook.

Posted by boardy on Sun, 21 Apr 2019 16:21:34 -0700