前言
在做項目的時候遇到一個業務需要對圖片進行旋轉,于是找到一個工具類,親測有效;在此與大家共享,需要用時可以直接用哈!
實戰
一、旋轉工具類代碼:
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