java實(shí)現(xiàn)圖片任意角度旋轉(zhuǎn)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖片旋轉(zhuǎn),供大家參考,具體內(nèi)容如下
方法一:普通方法實(shí)現(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實(shí)現(xiàn)圖片旋轉(zhuǎn)
/**
* opencv實(shí)現(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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對(duì)象
這篇文章主要為大家詳細(xì)介紹了Spring如何使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對(duì)象,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
SpringBoot 快速實(shí)現(xiàn) api 加密的方法
在項(xiàng)目中,為了保證數(shù)據(jù)的安全,我們常常會(huì)對(duì)傳遞的數(shù)據(jù)進(jìn)行加密,常用的加密算法包括對(duì)稱加密(AES)和非對(duì)稱加密(RSA),本文給大家介紹SpringBoot 快速實(shí)現(xiàn) api 加密,感興趣的朋友一起看看吧2023-10-10
淺談Spring AOP中args()和argNames的含義
這篇文章主要介紹了Spring AOP中args()和argNames的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
使用kotlin編寫spring cloud微服務(wù)的過程
這篇文章主要介紹了使用kotlin編寫spring cloud微服務(wù)的相關(guān)知識(shí),本文給大家提到配置文件的操作代碼,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)
本文詳細(xì)介紹了使用RabbitTemplate進(jìn)行消息傳遞的幾種模式,包括點(diǎn)對(duì)點(diǎn)通信、發(fā)布/訂閱模式、工作隊(duì)列模式、路由模式和主題模式,每種模式都通過代碼示例展示了生產(chǎn)者和消費(fèi)者的實(shí)現(xiàn),幫助開發(fā)者理解和運(yùn)用RabbitMQ進(jìn)行高效的消息處理2024-10-10
詳解Spring整合mybatis--Spring中的事務(wù)管理(xml形式)
這篇文章主要介紹了Spring整合mybatis--Spring中的事務(wù)管理(xml形式),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11

