java實現(xiàn)圖片任意角度旋轉(zhuǎn)
更新時間:2019年04月07日 16:08:27 作者:落日流年
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)圖片任意角度旋轉(zhuǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java實現(xiàn)圖片旋轉(zhuǎn),供大家參考,具體內(nèi)容如下
方法一:普通方法實現(xiàn)圖片旋轉(zhuǎn)
/** * 圖像旋轉(zhuǎn) * @param src * @param angel * @return */ public static BufferedImage Rotate(Image src, double angel) { int src_width = src.getWidth(null); int src_height = src.getHeight(null); // calculate the new image size 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_3BYTE_BGR); Graphics2D g2 = res.createGraphics(); // transform 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; } public static Rectangle CalcRotatedSize(Rectangle src, double angel) { // if angel is greater than 90 degree, we need to do some 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)); len_dalta_width=len_dalta_width>0?len_dalta_width:-len_dalta_width; int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height)); len_dalta_height=len_dalta_height>0?len_dalta_height:-len_dalta_height; int des_width = src.width + len_dalta_width * 2; int des_height = src.height + len_dalta_height * 2; des_width=des_width>0?des_width:-des_width; des_height=des_height>0?des_height:-des_height; return new java.awt.Rectangle(new Dimension(des_width, des_height)); }
方法二:opencv實現(xiàn)圖片旋轉(zhuǎn)
/** * opencv實現(xiàn)圖片旋轉(zhuǎn) * @param splitImage * @param angle * @return */ public static Mat rotate3(Mat splitImage, double angle) { double thera = angle * Math.PI / 180; double a = Math.sin(thera); double b = Math.cos(thera); int wsrc = splitImage.width(); int hsrc = splitImage.height(); int wdst = (int) (hsrc * Math.abs(a) + wsrc * Math.abs(b)); int hdst = (int) (wsrc * Math.abs(a) + hsrc * Math.abs(b)); Mat imgDst = new Mat(hdst, wdst, splitImage.type()); Point pt = new Point(splitImage.cols() / 2, splitImage.rows() / 2); // 獲取仿射變換矩陣 Mat affineTrans = Imgproc.getRotationMatrix2D(pt, angle, 1.0); System.out.println(affineTrans.dump()); // 改變變換矩陣第三列的值 affineTrans.put(0, 2, affineTrans.get(0, 2)[0] + (wdst - wsrc) / 2); affineTrans.put(1, 2, affineTrans.get(1, 2)[0] + (hdst - hsrc) / 2); Imgproc.warpAffine(splitImage, imgDst, affineTrans, imgDst.size(), Imgproc.INTER_CUBIC | Imgproc.WARP_FILL_OUTLIERS); return imgDst; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java的Struts2框架的結(jié)構(gòu)及其數(shù)據(jù)轉(zhuǎn)移方式
這篇文章主要介紹了詳解Java的Struts2框架的結(jié)構(gòu)及其數(shù)據(jù)轉(zhuǎn)移方式,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01使用Logback設(shè)置property參數(shù)方式
這篇文章主要介紹了使用Logback設(shè)置property參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java開發(fā)ShardingSphere的路由引擎類型示例詳解
這篇文章主要為大家介紹了java開發(fā)ShardingSphere的路由引擎類型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Java SE使用數(shù)組實現(xiàn)高速數(shù)字轉(zhuǎn)換功能
隨著大數(shù)據(jù)時代的到來,數(shù)字轉(zhuǎn)換功能變得越來越重要,在Java開發(fā)中,數(shù)字轉(zhuǎn)換功能也是經(jīng)常用到的,下面我們就來學(xué)習(xí)一下如何使用Java SE數(shù)組實現(xiàn)高速的數(shù)字轉(zhuǎn)換功能吧2023-11-11在mybatis中使用mapper進(jìn)行if條件判斷
這篇文章主要介紹了在mybatis中使用mapper進(jìn)行if條件判斷,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01