java實(shí)現(xiàn)圖片無損任意角度旋轉(zhuǎn)
前言
在做項(xiàng)目的時(shí)候遇到一個(gè)業(yè)務(wù)需要對(duì)圖片進(jìn)行旋轉(zhuǎn),于是找到一個(gè)工具類,親測(cè)有效;在此與大家共享,需要用時(shí)可以直接用哈!
實(shí)戰(zhàn)
一、旋轉(zhuǎn)工具類代碼:
package zh.test.utils; import java.awt.*; import java.awt.image.BufferedImage; /** * 圖片旋轉(zhuǎn)工具類 */ public class RotateImage { /** * 對(duì)圖片進(jìn)行旋轉(zhuǎn) * * @param src 被旋轉(zhuǎn)圖片 * @param angel 旋轉(zhuǎn)角度 * @return 旋轉(zhuǎn)后的圖片 */ public static BufferedImage Rotate(Image src, int angel) { int src_width = src.getWidth(null); int src_height = src.getHeight(null); // 計(jì)算旋轉(zhuǎn)后圖片的尺寸 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(); // 進(jìn)行轉(zhuǎn)換 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; } /** * 計(jì)算旋轉(zhuǎn)后的圖片 * * @param src 被旋轉(zhuǎn)的圖片 * @param angel 旋轉(zhuǎn)角度 * @return 旋轉(zhuǎn)后的圖片 */ public static Rectangle CalcRotatedSize(Rectangle src, int angel) { // 如果旋轉(zhuǎn)的角度大于90度做相應(yīng)的轉(zhuǎn)換 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)); } }
二、調(diào)用工具類的代碼:
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; /** * 測(cè)試圖片旋轉(zhuǎn) */ @RestController @RequestMapping(value = "/test") public class TestController { @RequestMapping(method = RequestMethod.POST) public void testImgRotate(MultipartFile multipartFile) throws Exception { BufferedImage src = ImageIO.read(multipartFile.getInputStream()); //順時(shí)針旋轉(zhuǎn)90度 BufferedImage des1 = RotateImage.Rotate(src, 90); ImageIO.write(des1, "jpg", new File("e:/90.jpg")); //順時(shí)針旋轉(zhuǎn)180度 BufferedImage des2 = RotateImage.Rotate(src, 180); ImageIO.write(des2, "jpg", new File("c:/180.jpg")); //順時(shí)針旋轉(zhuǎn)270度 BufferedImage des3 = RotateImage.Rotate(src, 270); ImageIO.write(des3, "jpg", new File("c:/270.jpg")); } }
三、效果
1、被旋轉(zhuǎn)的圖片:
2、順時(shí)針旋轉(zhuǎn)90度圖片:
3、順時(shí)針旋轉(zhuǎn)180度圖片:
4、順時(shí)針旋轉(zhuǎn)270度圖片:
總結(jié):
1、寫代碼要盡可能的考慮周全,對(duì)于自己懷疑可能會(huì)出錯(cuò)的事情千萬(wàn)不要抱著僥幸心理慶幸可以逃過去;60年前墨菲就為我們總結(jié)好了---墨菲定律!
2、遇到問題后要學(xué)會(huì)跳出來想,不要輕易先下結(jié)論,不然很容易鉆進(jìn)去;要有一定思路去排查,即便你十分肯定沒問題的地方也要認(rèn)真去檢查,有時(shí)候問題往往出在這些被我們忽略的地方。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java基礎(chǔ)學(xué)習(xí)JVM中GC的算法
這篇文章主要介紹了java基礎(chǔ)學(xué)習(xí)JVM中GC的算法,通過圖文加深對(duì)GC算法思路的理解。2017-11-11基于ScheduledExecutorService的兩種方法(詳解)
下面小編就為大家?guī)硪黄赟cheduledExecutorService的兩種方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10java中File類的三種創(chuàng)建文件夾方法總結(jié)
這篇文章主要給大家介紹了關(guān)于java中File類的三種創(chuàng)建文件夾方法,File類代表文件或目錄路徑名的抽象表達(dá)形式,通過File類提供的方法,我們可以很方便地創(chuàng)建文件夾,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04RocketMQ源碼分析之Broker過期消息清理機(jī)制
這篇文章主要為大家介紹了RocketMQ源碼分析之Broker過期消息清理機(jī)制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05詳解SpringBoot靜態(tài)方法獲取bean的三種方式
本文主要介紹了詳解SpringBoot靜態(tài)方法獲取bean的三種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Springboot定時(shí)任務(wù)Scheduled重復(fù)執(zhí)行操作
這篇文章主要介紹了Springboot定時(shí)任務(wù)Scheduled重復(fù)執(zhí)行操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Spring事件監(jiān)聽機(jī)制使用和原理示例講解
Spring事件監(jiān)聽機(jī)制是一個(gè)很不錯(cuò)的功能,我們?cè)谶M(jìn)行業(yè)務(wù)開發(fā)的時(shí)候可以引入,在相關(guān)的開源框架中也是用它的身影,比如高性能網(wǎng)關(guān)ShenYu中就使用了Spring事件監(jiān)聽機(jī)制來發(fā)布網(wǎng)關(guān)的更新數(shù)據(jù),它可以降低系統(tǒng)的耦合性,使系統(tǒng)的擴(kuò)展性更好2023-06-06