最近在做小型游戏,遇到了要翻转图片的苦恼,经过我一下午的研究,终于发现了一种好用的方法。
部分代码来源于别人,我在这个基础上修改了下,变得更好用了,之前的别人image输入都是bufferedimage,我改成了image,也就是加了一个转换。
大家如果看不懂代码没关系,会用就行了,我会展示一下怎么用的。
imagerotate类
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
package mypackage;
import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.file;
/**
* author mythos_q
*
* time 2011-07-20
*
* description 图片旋转、翻转处理
*/
public class imgrotate
{
static int up_down_reverse = 0 ;
static int left_right_reverse = 1 ;
/**
* 旋转图片为指定角度
*
* @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(h, w, type)).creategraphics()).setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear);
graphics2d.rotate(math.toradians(degree), w / 2 , h / 2 + (w>h?(w-h)/ 2 :(h-w)/ 2 ));
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
return img;
}
/**
* 旋转图片为指定角度
*
* @param file
* 目标图像
* @param degree
* 旋转角度(90,180,270)
* @return
*/
public static file rotateimage(file file, int degree) throws exception{
if (degree== 90 ) return rotateimage90(file);
if (degree== 180 ) return rotateimage180(file);
if (degree== 270 ) return rotateimage270(file);
return null ;
}
public static image rotateimage(image image, int degree)
{
if (degree== 90 )
return rotateimage90(image);
if (degree== 180 )
return rotateimage180(image);
if (degree== 270 )
return rotateimage270(image);
return null ;
}
private static image rotateimage90(image image)
{
bufferedimage bufferedimage = imagetobufferedimage(image);
int w = bufferedimage.getwidth();
int h = bufferedimage.getheight();
int type = bufferedimage.getcolormodel().gettransparency();
bufferedimage img;
graphics2d graphics2d;
(graphics2d =
(img = new bufferedimage(h, w, type) ).creategraphics()
).setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear);
graphics2d.rotate(math.toradians( 270 ), w / 2 , h / 2 + (w-h)/ 2 );
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
return bufferedimagetoimage(img);
}
//左转90度
public static file rotateimage90(file file) throws exception
{
bufferedimage bufferedimage = imageio.read(file);
int w = bufferedimage.getwidth();
int h = bufferedimage.getheight();
int type = bufferedimage.getcolormodel().gettransparency();
bufferedimage img;
graphics2d graphics2d;
(graphics2d =
(img = new bufferedimage(h, w, type) ).creategraphics()
).setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear);
graphics2d.rotate(math.toradians( 270 ), w / 2 , h / 2 + (w-h)/ 2 );
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
imageio.write(img, "jpg" ,file);
return file;
}
//右转90度
public static file rotateimage270(file file) throws exception
{
bufferedimage bufferedimage = imageio.read(file);
int w = bufferedimage.getwidth();
int h = bufferedimage.getheight();
int type = bufferedimage.getcolormodel().gettransparency();
bufferedimage img;
graphics2d graphics2d;
(graphics2d = (img = new bufferedimage(h, w, type)).creategraphics()).setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear);
graphics2d.rotate(math.toradians( 90 ), w / 2 - (w-h)/ 2 , h / 2 );
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
imageio.write(img, "jpg" ,file);
return file;
}
public static image rotateimage270(image image)
{
bufferedimage bufferedimage = imagetobufferedimage(image);
int w = bufferedimage.getwidth();
int h = bufferedimage.getheight();
int type = bufferedimage.getcolormodel().gettransparency();
bufferedimage img;
graphics2d graphics2d;
(graphics2d = (img = new bufferedimage(h, w, type)).creategraphics()).setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear);
graphics2d.rotate(math.toradians( 90 ), w / 2 - (w-h)/ 2 , h / 2 );
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
return bufferedimagetoimage(img);
}
//对转
public static file rotateimage180(file file) throws exception
{
bufferedimage bufferedimage = imageio.read(file);
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( 180 ), w / 2 , h / 2 );
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
imageio.write(img, "jpg" ,file);
return file;
}
public static image rotateimage180(image image)
{
bufferedimage bufferedimage = imagetobufferedimage(image);
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( 180 ), w / 2 , h / 2 );
graphics2d.drawimage(bufferedimage, 0 , 0 , null );
graphics2d.dispose();
return bufferedimagetoimage(img);
}
/***
* 图片镜像处理
* @param file
* @param fx 0 为上下反转 1 为左右反转
* @return
*/
public static void imagemisro(file file, int fx)
{
try
{
bufferedimage bufferedimage = imageio.read(file);
int w = bufferedimage.getwidth();
int h = bufferedimage.getheight();
int [][] datas = new int [w][h];
for ( int i = 0 ; i < h; i++) {
for ( int j = 0 ; j < w; j++) {
datas[j][i] = bufferedimage.getrgb(j, i);
}
}
int [][] tmps = new int [w][h];
if (fx == 0 ) {
for ( int i = 0 , a = h - 1 ; i < h; i++, a--) {
for ( int j = 0 ; j < w; j++) {
tmps[j][a] = datas[j][i];
}
}
} else if (fx == 1 ) {
for ( int i = 0 ; i < h; i++) {
for ( int j = 0 , b = w - 1 ; j < w; j++, b--) {
tmps[b][i] = datas[j][i];
}
}
}
for ( int i = 0 ; i < h; i++){
for ( int j = 0 ; j<w ;j++){
bufferedimage.setrgb(j, i, tmps[j][i]);
}
}
imageio.write(bufferedimage, "jpg" , file);
} catch (exception e) {
e.printstacktrace();
}
}
/*
*
* 镜像处理,输入image和方式,返回翻转的新image
* type = 0 表示上下翻转,type = 1 表示左右翻转
*/
public static image imagemisro(image image, int type )
{
try
{
//用到了自己写的方法
bufferedimage bufferedimage = imagetobufferedimage(image);
int w = bufferedimage.getwidth();
int h = bufferedimage.getheight();
int [][] datas = new int [w][h];
for ( int i = 0 ; i < h; i++) {
for ( int j = 0 ; j < w; j++) {
datas[j][i] = bufferedimage.getrgb(j, i);
}
}
int [][] tmps = new int [w][h];
if (type == 0 ) {
for ( int i = 0 , a = h - 1 ; i < h; i++, a--) {
for ( int j = 0 ; j < w; j++) {
tmps[j][a] = datas[j][i];
}
}
} else if (type == 1 ) {
for ( int i = 0 ; i < h; i++) {
for ( int j = 0 , b = w - 1 ; j < w; j++, b--) {
tmps[b][i] = datas[j][i];
}
}
}
for ( int i = 0 ; i < h; i++){
for ( int j = 0 ; j<w ;j++){
bufferedimage.setrgb(j, i, tmps[j][i]);
}
}
image newimage = (image)bufferedimage;
return newimage;
//imageio.write(bufferedimage, "jpg", file);
} catch (exception e) {
e.printstacktrace();
}
return null ;
}
//image转换成bufferedimage
public static bufferedimage imagetobufferedimage( image image )
{
bufferedimage bufferedimage = new bufferedimage
(image.getwidth( null ), image.getheight( null ),bufferedimage.type_int_rgb);
graphics g = bufferedimage.creategraphics();
g.drawimage(image, 0 , 0 , null ); //这里,大家可能会有疑问,似乎没有对bufferedimage干啥
g.dispose(); //但是是正确的,g调用drawimage就自动保存了
return bufferedimage;
}
//bufferedimage 转换成image类型
public static image bufferedimagetoimage( bufferedimage b )
{
return (image)b;
}
}
|
下面展示用法:
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
|
package mypackage;
import java.awt.*;
import java.awt.image.*;
import java.net.url;
import javax.swing.*;
public class test extends jpanel
{
jframe frame;
image image = new imageicon( "hand2.jpg" ).getimage();
public void paint( graphics g )
{
g.drawimage(image, 0 , 0 , null ); //之前的图片
image m = imgrotate.imagemisro(image, imgrotate.left_right_reverse);
g.drawimage(m, 200 , 200 , null ); //水平翻转的图片
image mm = imgrotate.rotateimage(m, 90 ); //这里只能填90,180,270
g.drawimage(mm, 250 , 0 , null );
}
public test()
{
frame = new jframe();
frame.add( this );
frame.setsize( 500 , 500 );
frame.setlocationrelativeto( null );
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.setvisible( true );
}
public static void main(string[] args)
{
new test();
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_39627843/article/details/86497370