Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)
圖像的旋轉(zhuǎn)需要調(diào)用 Graphics2D 類的rotate()方法,該方法將根據(jù)指定的弧度旋轉(zhuǎn)圖像。
語(yǔ)法如下:
rotate(double theta)
其中, theta 是指旋轉(zhuǎn)的弧度。
說(shuō)明:該方法只接受旋轉(zhuǎn)的弧度作為參數(shù),可以使用 Math 類的 toRadians()方法將角度轉(zhuǎn)換為弧度。 toRadians()方法接受角度值作為參數(shù),返回值是轉(zhuǎn)換完畢的弧度值。
實(shí)例代碼:
/** *//**
* 旋轉(zhuǎn)圖片為指定角度
*
* @param bufferedimage
* 目標(biāo)圖像
* @param degree
* 旋轉(zhuǎn)角度
* @return
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree){
int w= bufferedimage.getWidth();// 得到圖片寬度。
int h= bufferedimage.getHeight();// 得到圖片高度。
int type= bufferedimage.getColorModel().getTransparency();// 得到圖片透明度。
BufferedImage img;// 空的圖片。
Graphics2D graphics2d;// 空的畫筆。
(graphics2d= (img= new BufferedImage(w, h, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);// 旋轉(zhuǎn),degree是整型,度數(shù),比如垂直90度。
graphics2d.drawImage(bufferedimage, 0, 0, null);// 從bufferedimagecopy圖片至img,0,0是img的坐標(biāo)。
graphics2d.dispose();
return img;// 返回復(fù)制好的圖片,原圖片依然沒(méi)有變,沒(méi)有旋轉(zhuǎn),下次還可以使用。
}
/** *//**
* 變更圖像為指定大小
*
* @param bufferedimage
* 目標(biāo)圖像
* @param w
* 寬
* @param h
* 高
* @return
*/
public static BufferedImage resizeImage(final BufferedImage bufferedimage,
final int w, final int h) {
int type= bufferedimage.getColorModel().getTransparency();// 得到透明度。
BufferedImage img;// 空?qǐng)D片。
Graphics2D graphics2d;// 空畫筆。
(graphics2d= (img= createImage(w, h, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage
.getWidth(), bufferedimage.getHeight(), null);
graphics2d.dispose();
return img;
}
/** *//**
* 水平翻轉(zhuǎn)圖像
*
* @param bufferedimage 目標(biāo)圖像
* @return
*/
public static BufferedImage flipImage(final BufferedImage bufferedimage){
int w = bufferedimage.getWidth();// 得到寬度。
int h = bufferedimage.getHeight();// 得到高度。
BufferedImage img;// 空?qǐng)D片。
Graphics2D graphics2d;// 空畫筆。
(graphics2d = (img = createImage(w, h, bufferedimage
.getColorModel().getTransparency())).createGraphics())
.drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
graphics2d.dispose();
return img;
}
總結(jié)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示
這篇文章主要介紹了SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
SpringCloud @RefreshScope刷新機(jī)制深入探究
RefeshScope這個(gè)注解想必大家都用過(guò),在微服務(wù)配置中心的場(chǎng)景下經(jīng)常出現(xiàn),他可以用來(lái)刷新Bean中的屬性配置,那大家對(duì)他的實(shí)現(xiàn)原理了解嗎?它為什么可以做到動(dòng)態(tài)刷新呢2023-03-03
java復(fù)制文件的4種方式及拷貝文件到另一個(gè)目錄下的實(shí)例代碼
這篇文章主要介紹了java復(fù)制文件的4種方式,通過(guò)實(shí)例帶給大家介紹了java 拷貝文件到另一個(gè)目錄下的方法,需要的朋友可以參考下2018-06-06
springboot 多環(huán)境配置 yml文件版的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot 多環(huán)境配置 yml文件版的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot如何根據(jù)用戶系統(tǒng)時(shí)區(qū)動(dòng)態(tài)展示時(shí)間
這篇文章主要介紹了SpringBoot如何根據(jù)用戶系統(tǒng)時(shí)區(qū)動(dòng)態(tài)展示時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
使用Spring啟動(dòng)時(shí)運(yùn)行自定義業(yè)務(wù)
這篇文章主要介紹了使用Spring啟動(dòng)時(shí)運(yùn)行自定義業(yè)務(wù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解)
這篇文章主要介紹了Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Mybatis動(dòng)態(tài)SQL之where標(biāo)簽用法說(shuō)明
這篇文章主要介紹了Mybatis動(dòng)態(tài)SQL之where標(biāo)簽用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

