前言
在做项目的时候遇到一个业务需要对图片进行旋转,于是找到一个工具类,亲测有效;在此与大家共享,需要用时可以直接用哈!
实战
一、旋转工具类代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package zh.test.utils;
import java.awt.*; import java.awt.image.bufferedimage;
/** * 图片旋转工具类 */ public class rotateimage {
/** * 对图片进行旋转 * * @param src 被旋转图片 * @param angel 旋转角度 * @return 旋转后的图片 */ public static bufferedimage rotate(image src, int angel) { int src_width = src.getwidth( null ); int src_height = src.getheight( null ); // 计算旋转后图片的尺寸 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(); // 进行转换 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; }
/** * 计算旋转后的图片 * * @param src 被旋转的图片 * @param angel 旋转角度 * @return 旋转后的图片 */ public static rectangle calcrotatedsize(rectangle src, int angel) { // 如果旋转的角度大于90度做相应的转换 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)); } } |
二、调用工具类的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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;
/** * 测试图片旋转 */ @restcontroller @requestmapping (value = "/test" ) public class testcontroller {
@requestmapping (method = requestmethod.post) public void testimgrotate(multipartfile multipartfile) throws exception { bufferedimage src = imageio.read(multipartfile.getinputstream()); //顺时针旋转90度 bufferedimage des1 = rotateimage.rotate(src, 90 ); imageio.write(des1, "jpg" , new file( "e:/90.jpg" )); //顺时针旋转180度 bufferedimage des2 = rotateimage.rotate(src, 180 ); imageio.write(des2, "jpg" , new file( "c:/180.jpg" )); //顺时针旋转270度 bufferedimage des3 = rotateimage.rotate(src, 270 ); imageio.write(des3, "jpg" , new file( "c:/270.jpg" ));
}
} |
三、效果
1、被旋转的图片:
2、顺时针旋转90度图片:
3、顺时针旋转180度图片:
4、顺时针旋转270度图片:
总结:
1、写代码要尽可能的考虑周全,对于自己怀疑可能会出错的事情千万不要抱着侥幸心理庆幸可以逃过去;60年前墨菲就为我们总结好了---墨菲定律!
2、遇到问题后要学会跳出来想,不要轻易先下结论,不然很容易钻进去;要有一定思路去排查,即便你十分肯定没问题的地方也要认真去检查,有时候问题往往出在这些被我们忽略的地方。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/zhanghan18333611647/article/details/79188556
查看更多关于java实现图片无损任意角度旋转的详细内容...