圖像的旋轉需要調用 Graphics2D 類的rotate()方法,該方法將根據指定的弧度旋轉圖像。
語法如下:
1
|
rotate( double theta) |
其中, theta 是指旋轉的弧度。
說明:該方法只接受旋轉的弧度作為參數,可以使用 Math 類的 toRadians()方法將角度轉換為弧度。 toRadians()方法接受角度值作為參數,返回值是轉換完畢的弧度值。
實例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/** */ /** * 旋轉圖片為指定角度 * * @param bufferedimage * 目標圖像 * @param degree * 旋轉角度 * @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 ); // 旋轉,degree是整型,度數,比如垂直90度。 graphics2d.drawImage(bufferedimage, 0 , 0 , null ); // 從bufferedimagecopy圖片至img,0,0是img的坐標。 graphics2d.dispose(); return img; // 返回復制好的圖片,原圖片依然沒有變,沒有旋轉,下次還可以使用。 } /** */ /** * 變更圖像為指定大小 * * @param bufferedimage * 目標圖像 * @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; } /** */ /** * 水平翻轉圖像 * * @param bufferedimage 目標圖像 * @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; } |
總結
以上就是本文的全部內容,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.360doc.com/content/13/0612/01/12664468_292274023.shtml