Java圖像之自定義角度旋轉(zhuǎn)(實例)
圖像的旋轉(zhuǎn)需要調(diào)用 Graphics2D 類的rotate()方法,該方法將根據(jù)指定的弧度旋轉(zhuǎn)圖像。
語法如下:
rotate(double theta)
其中, theta 是指旋轉(zhuǎn)的弧度。
說明:該方法只接受旋轉(zhuǎn)的弧度作為參數(shù),可以使用 Math 類的 toRadians()方法將角度轉(zhuǎn)換為弧度。 toRadians()方法接受角度值作為參數(shù),返回值是轉(zhuǎn)換完畢的弧度值。
實例代碼:
/** *//**
* 旋轉(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ù)制好的圖片,原圖片依然沒有變,沒有旋轉(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;// 空圖片。
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;// 空圖片。
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é)
以上就是本文的全部內(nèi)容,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java Spring分別實現(xiàn)定時任務(wù)方法
這篇文章主要為大家詳細(xì)介紹了Java與Spring設(shè)置動態(tài)定時任務(wù)的方法,定時任務(wù)的應(yīng)用場景十分廣泛,如定時清理文件、定時生成報表、定時數(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如何配置請求的入?yún)⒑统鰠son數(shù)據(jù)格式
這篇文章主要介紹了spring boot如何配置請求的入?yún)⒑统鰠son數(shù)據(jù)格式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
詳解Maven項目缺少M(fèi)aven Dependencies解決方法總結(jié)
這篇文章主要介紹了詳解Maven項目缺少M(fèi)aven Dependencies解決方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

