Java圖像之自定義角度旋轉(zhuǎn)(實(shí)例)
圖像的旋轉(zhuǎn)需要調(diào)用 Graphics2D 類(lèi)的rotate()方法,該方法將根據(jù)指定的弧度旋轉(zhuǎn)圖像。
語(yǔ)法如下:
rotate(double theta)
其中, theta 是指旋轉(zhuǎn)的弧度。
說(shuō)明:該方法只接受旋轉(zhuǎn)的弧度作為參數(shù),可以使用 Math 類(lèi)的 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;// 空的畫(huà)筆。
(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;// 空畫(huà)筆。
(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;// 空畫(huà)筆。
(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)文章
Java Spring分別實(shí)現(xiàn)定時(shí)任務(wù)方法
這篇文章主要為大家詳細(xì)介紹了Java與Spring設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法,定時(shí)任務(wù)的應(yīng)用場(chǎng)景十分廣泛,如定時(shí)清理文件、定時(shí)生成報(bào)表、定時(shí)數(shù)據(jù)同步備份等2022-07-07
kafka運(yùn)維consumer-groups.sh消費(fèi)者組管理
這篇文章主要為大家介紹了kafka運(yùn)維consumer-groups.sh消費(fèi)者組管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Spring boot如何配置請(qǐng)求的入?yún)⒑统鰠son數(shù)據(jù)格式
這篇文章主要介紹了spring boot如何配置請(qǐng)求的入?yún)⒑统鰠son數(shù)據(jù)格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
詳解Maven項(xiàng)目缺少M(fèi)aven Dependencies解決方法總結(jié)
這篇文章主要介紹了詳解Maven項(xiàng)目缺少M(fèi)aven Dependencies解決方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

