前言
太仆寺ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
在做項目的時候遇到一個業(yè)務需要對圖片進行旋轉(zhuǎn),于是找到一個工具類,親測有效;在此與大家共享,需要用時可以直接用哈!
實戰(zhàn)
一、旋轉(zhuǎn)工具類代碼:
package zh.test.utils; import java.awt.*; import java.awt.image.BufferedImage; /** * 圖片旋轉(zhuǎn)工具類 */ public class RotateImage { /** * 對圖片進行旋轉(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); // 計算旋轉(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(); // 進行轉(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; } /** * 計算旋轉(zhuǎn)后的圖片 * * @param src 被旋轉(zhuǎn)的圖片 * @param angel 旋轉(zhuǎn)角度 * @return 旋轉(zhuǎn)后的圖片 */ public static Rectangle CalcRotatedSize(Rectangle src, int angel) { // 如果旋轉(zhuǎn)的角度大于90度做相應的轉(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; /** * 測試圖片旋轉(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()); //順時針旋轉(zhuǎn)90度 BufferedImage des1 = RotateImage.Rotate(src, 90); ImageIO.write(des1, "jpg", new File("e:/90.jpg")); //順時針旋轉(zhuǎn)180度 BufferedImage des2 = RotateImage.Rotate(src, 180); ImageIO.write(des2, "jpg", new File("c:/180.jpg")); //順時針旋轉(zhuǎn)270度 BufferedImage des3 = RotateImage.Rotate(src, 270); ImageIO.write(des3, "jpg", new File("c:/270.jpg")); } }
三、效果
1、被旋轉(zhuǎn)的圖片:
2、順時針旋轉(zhuǎn)90度圖片:
3、順時針旋轉(zhuǎn)180度圖片:
4、順時針旋轉(zhuǎn)270度圖片:
總結(jié):
1、寫代碼要盡可能的考慮周全,對于自己懷疑可能會出錯的事情千萬不要抱著僥幸心理慶幸可以逃過去;60年前墨菲就為我們總結(jié)好了---墨菲定律!
2、遇到問題后要學會跳出來想,不要輕易先下結(jié)論,不然很容易鉆進去;要有一定思路去排查,即便你十分肯定沒問題的地方也要認真去檢查,有時候問題往往出在這些被我們忽略的地方。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞名稱:java實現(xiàn)圖片無損任意角度旋轉(zhuǎn)
網(wǎng)頁網(wǎng)址:http://muchs.cn/article2/ijdpoc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、品牌網(wǎng)站設計、微信公眾號、靜態(tài)網(wǎng)站、網(wǎng)站改版、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)