用java2D的scale实现图片翻转
通过java2D实现沿着图片的某一边进行翻转,如沿着左边、下边、上边、右边翻转图像,获得原始图像以及翻转图像在一张图片上的新图片。
scale的 AffineTransform矩阵为[sx,0,0],可见scale是对原图片的(x,y)像素的点进行相乘处理(sx*x,sy*y).
[0,sy,0]
[0,0,1]
对于翻转图片来说,如果横向翻转,那么将scale的参数设为(-1,1),纵向翻转将参数设为(1,-1)
scale()方法对于很多熟悉java2d的人经常用来进行放大缩小描绘处理,但是scale
以图像沿着下边翻转为例
BufferedImage imageTmp
=
null
;
int imageWidth = image.getWidth( null );
int imageHeight = image.getHeight( null );
Graphics2D g2 = (Graphics2D)imageTmp.createGraphics();
g2.drawImage(image, 0 , 0 , null );
// 翻转描绘,以下边为准
g2.scale( 1.0 , - 1.0 );
g2.drawImage(image, 0 , - 2 * imageHeight, null );
g2.scale( 1.0 , - 1.0 );
g2.dispose();
其中scale()方法中传入(1,-1)是以图片中的(x,y)映射为(x,-y)进行描绘,描绘的翻转图片的左上角的位置为(0,-2*imageHeight)
int imageWidth = image.getWidth( null );
int imageHeight = image.getHeight( null );
Graphics2D g2 = (Graphics2D)imageTmp.createGraphics();
g2.drawImage(image, 0 , 0 , null );
// 翻转描绘,以下边为准
g2.scale( 1.0 , - 1.0 );
g2.drawImage(image, 0 , - 2 * imageHeight, null );
g2.scale( 1.0 , - 1.0 );
g2.dispose();
对于其他边的翻转,算法基本相同,注意俩点
- 新图片的尺寸
- scale中传入的参数
1
import
java.awt.Graphics2D;
2 import java.awt.Image;
3 import java.awt.image.BufferedImage;
4
5 /**
6 * <p><code>OverTurnImages</code>,提供可以沿着Image的某个边进行翻转的方法
7 * </p>
8 *
9 * @author 如果有一天de
10 *
11 */
12 public class OverTurnImage {
13 /**
14 * <p>翻转类型:原始image</p>
15 */
16 public static final int orign = 0 ;
17 /**
18 * <p>矩形左侧边翻转</p>
19 */
20 public static final int LeftEdgeTurn = 1 ;
21 /**
22 * <p>矩形右侧边翻转</p>
23 */
24 public static final int RightEdgeTurn = 2 ;
25 /**
26 * <p>矩形上边翻转</p>
27 */
28 public static final int UpperEdgeTurn = 3 ;
29 /**
30 * <p>矩形下边翻转</p>
31 */
32 public static final int DownEdgeTurn = 4 ;
33 /**
34 * <p>生成翻转的图片,同时保存原图片</p>
35 *
36 * @param image 原始图片
37 * @param turnFlag 翻转类型
38 * @return Image 翻转后的图片
39 */
40 public static BufferedImage createTurnImage(BufferedImage image, int turnFlag)
41 {
42 BufferedImage imageTmp = null ;
43 int imageWidth = image.getWidth( null );
44 int imageHeight = image.getHeight( null );
45
46
47 if (turnFlag == 1 || turnFlag == 2 )
48 {
49 imageTmp = new BufferedImage(imageWidth * 2 ,imageHeight,BufferedImage.TYPE_INT_ARGB);
50 } else if (turnFlag == 3 || turnFlag == 4 )
51 {
52 imageTmp = new BufferedImage(imageWidth,imageHeight * 2 ,BufferedImage.TYPE_INT_ARGB);
53 } else {
54 return image;
55 }
56 Graphics2D g2 = (Graphics2D)imageTmp.createGraphics();
57 if (turnFlag == 1 )
58 {
59 g2.drawImage(image,imageWidth, 0 , null );
60 g2.scale( - 1.0 , 1.0 );
61 g2.drawImage(image, - imageWidth, 0 , null );
62 g2.scale( - 1.0 , 1.0 );
63 g2.dispose();
64 }
65 if (turnFlag == 2 )
66 {
67 g2.drawImage(image, 0 , 0 , null );
68 g2.scale( - 1.0 , 1.0 );
69 g2.drawImage(image, - 2 * imageWidth, 0 , null );
70 g2.scale( - 1.0 , 1.0 );
71 g2.dispose();
72 }
73 if (turnFlag == 3 )
74 {
75 g2.drawImage(image, 0 , imageHeight, null );
76 g2.scale( 1.0 , - 1.0 );
77 g2.drawImage(image, 0 , - imageHeight, null );
78 g2.scale( 1.0 , - 1.0 );
79 g2.dispose();
80 }
81 if (turnFlag == 4 )
82 {
83 g2.drawImage(image, 0 , 0 , null );
84 g2.scale( 1.0 , - 1.0 );
85 g2.drawImage(image, 0 , - 2 * imageHeight, null );
86 g2.scale( 1.0 , - 1.0 );
87 g2.dispose();
88 }
89
90
91 return imageTmp;
92 }
93
94 }
2 import java.awt.Image;
3 import java.awt.image.BufferedImage;
4
5 /**
6 * <p><code>OverTurnImages</code>,提供可以沿着Image的某个边进行翻转的方法
7 * </p>
8 *
9 * @author 如果有一天de
10 *
11 */
12 public class OverTurnImage {
13 /**
14 * <p>翻转类型:原始image</p>
15 */
16 public static final int orign = 0 ;
17 /**
18 * <p>矩形左侧边翻转</p>
19 */
20 public static final int LeftEdgeTurn = 1 ;
21 /**
22 * <p>矩形右侧边翻转</p>
23 */
24 public static final int RightEdgeTurn = 2 ;
25 /**
26 * <p>矩形上边翻转</p>
27 */
28 public static final int UpperEdgeTurn = 3 ;
29 /**
30 * <p>矩形下边翻转</p>
31 */
32 public static final int DownEdgeTurn = 4 ;
33 /**
34 * <p>生成翻转的图片,同时保存原图片</p>
35 *
36 * @param image 原始图片
37 * @param turnFlag 翻转类型
38 * @return Image 翻转后的图片
39 */
40 public static BufferedImage createTurnImage(BufferedImage image, int turnFlag)
41 {
42 BufferedImage imageTmp = null ;
43 int imageWidth = image.getWidth( null );
44 int imageHeight = image.getHeight( null );
45
46
47 if (turnFlag == 1 || turnFlag == 2 )
48 {
49 imageTmp = new BufferedImage(imageWidth * 2 ,imageHeight,BufferedImage.TYPE_INT_ARGB);
50 } else if (turnFlag == 3 || turnFlag == 4 )
51 {
52 imageTmp = new BufferedImage(imageWidth,imageHeight * 2 ,BufferedImage.TYPE_INT_ARGB);
53 } else {
54 return image;
55 }
56 Graphics2D g2 = (Graphics2D)imageTmp.createGraphics();
57 if (turnFlag == 1 )
58 {
59 g2.drawImage(image,imageWidth, 0 , null );
60 g2.scale( - 1.0 , 1.0 );
61 g2.drawImage(image, - imageWidth, 0 , null );
62 g2.scale( - 1.0 , 1.0 );
63 g2.dispose();
64 }
65 if (turnFlag == 2 )
66 {
67 g2.drawImage(image, 0 , 0 , null );
68 g2.scale( - 1.0 , 1.0 );
69 g2.drawImage(image, - 2 * imageWidth, 0 , null );
70 g2.scale( - 1.0 , 1.0 );
71 g2.dispose();
72 }
73 if (turnFlag == 3 )
74 {
75 g2.drawImage(image, 0 , imageHeight, null );
76 g2.scale( 1.0 , - 1.0 );
77 g2.drawImage(image, 0 , - imageHeight, null );
78 g2.scale( 1.0 , - 1.0 );
79 g2.dispose();
80 }
81 if (turnFlag == 4 )
82 {
83 g2.drawImage(image, 0 , 0 , null );
84 g2.scale( 1.0 , - 1.0 );
85 g2.drawImage(image, 0 , - 2 * imageHeight, null );
86 g2.scale( 1.0 , - 1.0 );
87 g2.dispose();
88 }
89
90
91 return imageTmp;
92 }
93
94 }