验证码图片

验证码图片

http://www.oschina.net/code/snippet_616487_15391 原网址:

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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package
com.coolinsoft.miaosha.util.ocr;
 
import
java.awt.Color;
import
java.awt.Graphics2D;
import
java.awt.color.ColorSpace;
import
java.awt.geom.AffineTransform;
import
java.awt.image.AffineTransformOp;
import
java.awt.image.BufferedImage;
import
java.awt.image.ColorConvertOp;
import
java.awt.image.ColorModel;
import
java.awt.image.MemoryImageSource;
import
java.awt.image.PixelGrabber;
import
java.util.HashMap;
import
java.util.Map;
 
public
class
ImageFilter {
    
private
BufferedImage image;
    
private
int
iw, ih;  
//图片宽度、高度
    
private
int
[] pixels;
//像素
 
    
public
ImageFilter(BufferedImage image) {
        
this
.image = image;
        
iw = image.getWidth();
        
ih = image.getHeight();
        
pixels =
new
int
[iw * ih];
    
}
 
    
/** 图像二值化 */
    
public
BufferedImage changeGrey() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
        
// 设定二值化的域值,默认值为100
        
int
grey =
150
;
        
// 对图像进行二值化处理,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
0
; i < iw * ih; i++) {
            
int
red, green, blue;
            
int
alpha = cm.getAlpha(pixels[i]);
            
if
(cm.getRed(pixels[i]) > grey) {
                
red =
255
;
            
}
else
{
                
red =
0
;
            
}
 
            
if
(cm.getGreen(pixels[i]) > grey) {
                
green =
255
;
            
}
else
{
                
green =
0
;
            
}
 
            
if
(cm.getBlue(pixels[i]) > grey) {
                
blue =
255
;
            
}
else
{
                
blue =
0
;
            
}
 
            
pixels[i] = alpha <<
24
| red <<
16
| green <<
8
| blue;
        
}
        
// 将数组中的象素产生一个图像
        
return
ImageIOHelper.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,pixels,
0
, iw));
    
}
 
    
/** 提升清晰度,进行锐化 */
    
public
BufferedImage sharp() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,
                
pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
 
        
// 象素的中间变量
        
int
tempPixels[] =
new
int
[iw * ih];
        
for
(
int
i =
0
; i < iw * ih; i++) {
            
tempPixels[i] = pixels[i];
        
}
        
// 对图像进行尖锐化处理,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
1
; i < ih -
1
; i++) {
            
for
(
int
j =
1
; j < iw -
1
; j++) {
                
int
alpha = cm.getAlpha(pixels[i * iw + j]);
 
                
// 对图像进行尖锐化
                
int
red6 = cm.getRed(pixels[i * iw + j +
1
]);
                
int
red5 = cm.getRed(pixels[i * iw + j]);
                
int
red8 = cm.getRed(pixels[(i +
1
) * iw + j]);
                
int
sharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5);
 
                
int
green5 = cm.getGreen(pixels[i * iw + j]);
                
int
green6 = cm.getGreen(pixels[i * iw + j +
1
]);
                
int
green8 = cm.getGreen(pixels[(i +
1
) * iw + j]);
                
int
sharpGreen = Math.abs(green6 - green5)
                        
+ Math.abs(green8 - green5);
 
                
int
blue5 = cm.getBlue(pixels[i * iw + j]);
                
int
blue6 = cm.getBlue(pixels[i * iw + j +
1
]);
                
int
blue8 = cm.getBlue(pixels[(i +
1
) * iw + j]);
                
int
sharpBlue = Math.abs(blue6 - blue5)
                        
+ Math.abs(blue8 - blue5);
 
                
if
(sharpRed >
255
) {
                    
sharpRed =
255
;
                
}
                
if
(sharpGreen >
255
) {
                    
sharpGreen =
255
;
                
}
                
if
(sharpBlue >
255
) {
                    
sharpBlue =
255
;
                
}
 
                
tempPixels[i * iw + j] = alpha <<
24
| sharpRed <<
16
                        
| sharpGreen <<
8
| sharpBlue;
            
}
        
}
 
        
// 将数组中的象素产生一个图像
        
return
ImageIOHelper
                
.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,
                        
tempPixels,
0
, iw));
    
}
     
     
     
     
     
     
 
 
    
public
static
int
isWhite(
int
colorInt) {
        
Color color =
new
Color(colorInt);
        
if
(color.getRed() + color.getGreen() + color.getBlue() >
600
) {
            
return
1
;
        
}
        
return
0
;
    
}
     
    
public 
BufferedImage removeBackgroud(){
        
BufferedImage img =
this
.image;
        
img = img.getSubimage(
1
,
1
, img.getWidth() -
2
, img.getHeight() -
2
);
        
int
width = img.getWidth();
        
int
height = img.getHeight();
        
double
subWidth = (
double
) width /
5.0
;
        
for
(
int
i =
0
; i <
5
; i++) {
            
Map<Integer, Integer> map =
new
HashMap<Integer, Integer>();
           
for
(
int
x = (
int
) (
1
+ i * subWidth); x < (i +
1
) * subWidth && x < width -
1
; ++x) {
              
for
(
int
y =
0
; y < height; ++y) {
                  
if
(isWhite(img.getRGB(x, y)) ==
1
)
                      
continue
;
                  
if
(map.containsKey(img.getRGB(x, y))) {
                      
map.put(img.getRGB(x, y), map.get(img.getRGB(x, y)) +
1
);
                  
}
else
{
                      
map.put(img.getRGB(x, y),
1
);
                  
}
              
}
           
}
           
int
max =
0
;
           
int
colorMax =
0
;
           
for
(Integer color : map.keySet()) {
                
if
(max < map.get(color)) {
                 
max = map.get(color);
                 
colorMax = color;
           
}
        
}
        
for
(
int
x = (
int
) (
1
+ i * subWidth); x < (i +
1
) * subWidth && x < width -
1
; ++x) {
            
for
(
int
y =
0
; y < height; ++y) {
                
if
(img.getRGB(x, y) != colorMax) {
                    
img.setRGB(x, y, Color.WHITE.getRGB());
                
}
else
{
                     
img.setRGB(x, y, Color.BLACK.getRGB());
                
}
            
}
        
}
      
}
       
return
img;
   
}
     
 
    
/** 中值滤波 */
    
public
BufferedImage median() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,
                
pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
        
// 对图像进行中值滤波,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
1
; i < ih -
1
; i++) {
            
for
(
int
j =
1
; j < iw -
1
; j++) {
                
int
red, green, blue;
                
int
alpha = cm.getAlpha(pixels[i * iw + j]);
 
                
//int red2 = cm.getRed(pixels[(i - 1) * iw + j]);
                
int
red4 = cm.getRed(pixels[i * iw + j -
1
]);
                
int
red5 = cm.getRed(pixels[i * iw + j]);
                
int
red6 = cm.getRed(pixels[i * iw + j +
1
]);
                
//int red8 = cm.getRed(pixels[(i + 1) * iw + j]);
 
                
// 水平方向进行中值滤波
                
if
(red4 >= red5) {
                    
if
(red5 >= red6) {
                        
red = red5;
                    
}
else
{
                        
if
(red4 >= red6) {
                            
red = red6;
                        
}
else
{
                            
red = red4;
                        
}
                    
}
                
}
else
{
                    
if
(red4 > red6) {
                        
red = red4;
                    
}
else
{
                        
if
(red5 > red6) {
                            
red = red6;
                        
}
else
{
                            
red = red5;
                        
}
                    
}
                
}
 
                
// int green2 = cm.getGreen(pixels[(i - 1) * iw + j]);
                
int
green4 = cm.getGreen(pixels[i * iw + j -
1
]);
                
int
green5 = cm.getGreen(pixels[i * iw + j]);
                
int
green6 = cm.getGreen(pixels[i * iw + j +
1
]);
                
// int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);
 
                
// 水平方向进行中值滤波
                
if
(green4 >= green5) {
                    
if
(green5 >= green6) {
                        
green = green5;
                    
}
else
{
                        
if
(green4 >= green6) {
                            
green = green6;
                        
}
else
{
                            
green = green4;
                        
}
                    
}
                
}
else
{
                    
if
(green4 > green6) {
                        
green = green4;
                    
}
else
{
                        
if
(green5 > green6) {
                            
green = green6;
                        
}
else
{
                            
green = green5;
                        
}
                    
}
                
}
 
                
// int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);
                
int
blue4 = cm.getBlue(pixels[i * iw + j -
1
]);
                
int
blue5 = cm.getBlue(pixels[i * iw + j]);
                
int
blue6 = cm.getBlue(pixels[i * iw + j +
1
]);
                
// int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);
 
                
// 水平方向进行中值滤波
                
if
(blue4 >= blue5) {
                    
if
(blue5 >= blue6) {
                        
blue = blue5;
                    
}
else
{
                        
if
(blue4 >= blue6) {
                            
blue = blue6;
                        
}
else
{
                            
blue = blue4;
                        
}
                    
}
                
}
else
{
                    
if
(blue4 > blue6) {
                        
blue = blue4;
                    
}
else
{
                        
if
(blue5 > blue6) {
                            
blue = blue6;
                        
}
else
{
                            
blue = blue5;
                        
}
                    
}
                
}
                
pixels[i * iw + j] = alpha <<
24
| red <<
16
| green <<
8
                        
| blue;
            
}
        
}
 
        
// 将数组中的象素产生一个图像
        
return
ImageIOHelper
                
.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,
                        
pixels,
0
, iw));
    
}
 
    
/** 线性灰度变换 */
    
public
BufferedImage lineGrey() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,
                
pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
        
// 对图像进行进行线性拉伸,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
0
; i < iw * ih; i++) {
            
int
alpha = cm.getAlpha(pixels[i]);
            
int
red = cm.getRed(pixels[i]);
            
int
green = cm.getGreen(pixels[i]);
            
int
blue = cm.getBlue(pixels[i]);
 
            
// 增加了图像的亮度
            
red = (
int
) (
1.1
* red +
30
);
            
green = (
int
) (
1.1
* green +
30
);
            
blue = (
int
) (
1.1
* blue +
30
);
            
if
(red >=
255
) {
                
red =
255
;
            
}
            
if
(green >=
255
) {
                
green =
255
;
            
}
            
if
(blue >=
255
) {
                
blue =
255
;
            
}
            
pixels[i] = alpha <<
24
| red <<
16
| green <<
8
| blue;
        
}
 
        
// 将数组中的象素产生一个图像
 
        
return
ImageIOHelper
                
.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,
                        
pixels,
0
, iw));
    
}
 
    
/** 转换为黑白灰度图 */
    
public
BufferedImage grayFilter() {
        
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        
ColorConvertOp op =
new
ColorConvertOp(cs,
null
);
        
return
op.filter(image,
null
);
    
}
 
    
/** 平滑缩放 */
    
public
BufferedImage scaling(
double
s) {
        
AffineTransform tx =
new
AffineTransform();
        
tx.scale(s, s);
        
AffineTransformOp op =
new
AffineTransformOp(tx,
                
AffineTransformOp.TYPE_BILINEAR);
        
return
op.filter(image,
null
);
    
}
 
    
public
BufferedImage scale(Float s) {
        
int
srcW = image.getWidth();
        
int
srcH = image.getHeight();
        
int
newW = Math.round(srcW * s);
        
int
newH = Math.round(srcH * s);
        
// 先做水平方向上的伸缩变换
        
BufferedImage tmp =
new
BufferedImage(newW, newH, image.getType());
        
Graphics2D g = tmp.createGraphics();
        
for
(
int
x =
0
; x < newW; x++) {
            
g.setClip(x,
0
,
1
, srcH);
            
// 按比例放缩
            
g.drawImage(image, x - x * srcW / newW,
0
,
null
);
        
}
 
        
// 再做垂直方向上的伸缩变换
        
BufferedImage dst =
new
BufferedImage(newW, newH, image.getType());
        
g = dst.createGraphics();
        
for
(
int
y =
0
; y < newH; y++) {
            
g.setClip(
0
, y, newW,
1
);
            
// 按比例放缩
            
g.drawImage(tmp,
0
, y - y * srcH / newH,
null
);
        
}
        
return
dst;
    
}
 
}

代码]将验证码抽取下来之后 零食存在本地 惊进行解析     跳至 [1] [全屏预览]

?
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package
com.coolinsoft.miaosha.util.ocr;
 
import
java.awt.Color;
import
java.awt.Graphics2D;
import
java.awt.color.ColorSpace;
import
java.awt.geom.AffineTransform;
import
java.awt.image.AffineTransformOp;
import
java.awt.image.BufferedImage;
import
java.awt.image.ColorConvertOp;
import
java.awt.image.ColorModel;
import
java.awt.image.MemoryImageSource;
import
java.awt.image.PixelGrabber;
import
java.util.HashMap;
import
java.util.Map;
 
public
class
ImageFilter {
    
private
BufferedImage image;
    
private
int
iw, ih;  
//图片宽度、高度
    
private
int
[] pixels;
//像素
 
    
public
ImageFilter(BufferedImage image) {
        
this
.image = image;
        
iw = image.getWidth();
        
ih = image.getHeight();
        
pixels =
new
int
[iw * ih];
    
}
 
    
/** 图像二值化 */
    
public
BufferedImage changeGrey() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
        
// 设定二值化的域值,默认值为100
        
int
grey =
150
;
        
// 对图像进行二值化处理,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
0
; i < iw * ih; i++) {
            
int
red, green, blue;
            
int
alpha = cm.getAlpha(pixels[i]);
            
if
(cm.getRed(pixels[i]) > grey) {
                
red =
255
;
            
}
else
{
                
red =
0
;
            
}
 
            
if
(cm.getGreen(pixels[i]) > grey) {
                
green =
255
;
            
}
else
{
                
green =
0
;
            
}
 
            
if
(cm.getBlue(pixels[i]) > grey) {
                
blue =
255
;
            
}
else
{
                
blue =
0
;
            
}
 
            
pixels[i] = alpha <<
24
| red <<
16
| green <<
8
| blue;
        
}
        
// 将数组中的象素产生一个图像
        
return
ImageIOHelper.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,pixels,
0
, iw));
    
}
 
    
/** 提升清晰度,进行锐化 */
    
public
BufferedImage sharp() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,
                
pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
 
        
// 象素的中间变量
        
int
tempPixels[] =
new
int
[iw * ih];
        
for
(
int
i =
0
; i < iw * ih; i++) {
            
tempPixels[i] = pixels[i];
        
}
        
// 对图像进行尖锐化处理,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
1
; i < ih -
1
; i++) {
            
for
(
int
j =
1
; j < iw -
1
; j++) {
                
int
alpha = cm.getAlpha(pixels[i * iw + j]);
 
                
// 对图像进行尖锐化
                
int
red6 = cm.getRed(pixels[i * iw + j +
1
]);
                
int
red5 = cm.getRed(pixels[i * iw + j]);
                
int
red8 = cm.getRed(pixels[(i +
1
) * iw + j]);
                
int
sharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5);
 
                
int
green5 = cm.getGreen(pixels[i * iw + j]);
                
int
green6 = cm.getGreen(pixels[i * iw + j +
1
]);
                
int
green8 = cm.getGreen(pixels[(i +
1
) * iw + j]);
                
int
sharpGreen = Math.abs(green6 - green5)
                        
+ Math.abs(green8 - green5);
 
                
int
blue5 = cm.getBlue(pixels[i * iw + j]);
                
int
blue6 = cm.getBlue(pixels[i * iw + j +
1
]);
                
int
blue8 = cm.getBlue(pixels[(i +
1
) * iw + j]);
                
int
sharpBlue = Math.abs(blue6 - blue5)
                        
+ Math.abs(blue8 - blue5);
 
                
if
(sharpRed >
255
) {
                    
sharpRed =
255
;
                
}
                
if
(sharpGreen >
255
) {
                    
sharpGreen =
255
;
                
}
                
if
(sharpBlue >
255
) {
                    
sharpBlue =
255
;
                
}
 
                
tempPixels[i * iw + j] = alpha <<
24
| sharpRed <<
16
                        
| sharpGreen <<
8
| sharpBlue;
            
}
        
}
 
        
// 将数组中的象素产生一个图像
        
return
ImageIOHelper
                
.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,
                        
tempPixels,
0
, iw));
    
}
     
     
     
     
     
     
 
 
    
public
static
int
isWhite(
int
colorInt) {
        
Color color =
new
Color(colorInt);
        
if
(color.getRed() + color.getGreen() + color.getBlue() >
600
) {
            
return
1
;
        
}
        
return
0
;
    
}
     
    
public 
BufferedImage removeBackgroud(){
        
BufferedImage img =
this
.image;
        
img = img.getSubimage(
1
,
1
, img.getWidth() -
2
, img.getHeight() -
2
);
        
int
width = img.getWidth();
        
int
height = img.getHeight();
        
double
subWidth = (
double
) width /
5.0
;
        
for
(
int
i =
0
; i <
5
; i++) {
            
Map<Integer, Integer> map =
new
HashMap<Integer, Integer>();
           
for
(
int
x = (
int
) (
1
+ i * subWidth); x < (i +
1
) * subWidth && x < width -
1
; ++x) {
              
for
(
int
y =
0
; y < height; ++y) {
                  
if
(isWhite(img.getRGB(x, y)) ==
1
)
                      
continue
;
                  
if
(map.containsKey(img.getRGB(x, y))) {
                      
map.put(img.getRGB(x, y), map.get(img.getRGB(x, y)) +
1
);
                  
}
else
{
                      
map.put(img.getRGB(x, y),
1
);
                  
}
              
}
           
}
           
int
max =
0
;
           
int
colorMax =
0
;
           
for
(Integer color : map.keySet()) {
                
if
(max < map.get(color)) {
                 
max = map.get(color);
                 
colorMax = color;
           
}
        
}
        
for
(
int
x = (
int
) (
1
+ i * subWidth); x < (i +
1
) * subWidth && x < width -
1
; ++x) {
            
for
(
int
y =
0
; y < height; ++y) {
                
if
(img.getRGB(x, y) != colorMax) {
                    
img.setRGB(x, y, Color.WHITE.getRGB());
                
}
else
{
                     
img.setRGB(x, y, Color.BLACK.getRGB());
                
}
            
}
        
}
      
}
       
return
img;
   
}
     
 
    
/** 中值滤波 */
    
public
BufferedImage median() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,
                
pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
        
// 对图像进行中值滤波,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
1
; i < ih -
1
; i++) {
            
for
(
int
j =
1
; j < iw -
1
; j++) {
                
int
red, green, blue;
                
int
alpha = cm.getAlpha(pixels[i * iw + j]);
 
                
//int red2 = cm.getRed(pixels[(i - 1) * iw + j]);
                
int
red4 = cm.getRed(pixels[i * iw + j -
1
]);
                
int
red5 = cm.getRed(pixels[i * iw + j]);
                
int
red6 = cm.getRed(pixels[i * iw + j +
1
]);
                
//int red8 = cm.getRed(pixels[(i + 1) * iw + j]);
 
                
// 水平方向进行中值滤波
                
if
(red4 >= red5) {
                    
if
(red5 >= red6) {
                        
red = red5;
                    
}
else
{
                        
if
(red4 >= red6) {
                            
red = red6;
                        
}
else
{
                            
red = red4;
                        
}
                    
}
                
}
else
{
                    
if
(red4 > red6) {
                        
red = red4;
                    
}
else
{
                        
if
(red5 > red6) {
                            
red = red6;
                        
}
else
{
                            
red = red5;
                        
}
                    
}
                
}
 
                
// int green2 = cm.getGreen(pixels[(i - 1) * iw + j]);
                
int
green4 = cm.getGreen(pixels[i * iw + j -
1
]);
                
int
green5 = cm.getGreen(pixels[i * iw + j]);
                
int
green6 = cm.getGreen(pixels[i * iw + j +
1
]);
                
// int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);
 
                
// 水平方向进行中值滤波
                
if
(green4 >= green5) {
                    
if
(green5 >= green6) {
                        
green = green5;
                    
}
else
{
                        
if
(green4 >= green6) {
                            
green = green6;
                        
}
else
{
                            
green = green4;
                        
}
                    
}
                
}
else
{
                    
if
(green4 > green6) {
                        
green = green4;
                    
}
else
{
                        
if
(green5 > green6) {
                            
green = green6;
                        
}
else
{
                            
green = green5;
                        
}
                    
}
                
}
 
                
// int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);
                
int
blue4 = cm.getBlue(pixels[i * iw + j -
1
]);
                
int
blue5 = cm.getBlue(pixels[i * iw + j]);
                
int
blue6 = cm.getBlue(pixels[i * iw + j +
1
]);
                
// int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);
 
                
// 水平方向进行中值滤波
                
if
(blue4 >= blue5) {
                    
if
(blue5 >= blue6) {
                        
blue = blue5;
                    
}
else
{
                        
if
(blue4 >= blue6) {
                            
blue = blue6;
                        
}
else
{
                            
blue = blue4;
                        
}
                    
}
                
}
else
{
                    
if
(blue4 > blue6) {
                        
blue = blue4;
                    
}
else
{
                        
if
(blue5 > blue6) {
                            
blue = blue6;
                        
}
else
{
                            
blue = blue5;
                        
}
                    
}
                
}
                
pixels[i * iw + j] = alpha <<
24
| red <<
16
| green <<
8
                        
| blue;
            
}
        
}
 
        
// 将数组中的象素产生一个图像
        
return
ImageIOHelper
                
.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,
                        
pixels,
0
, iw));
    
}
 
    
/** 线性灰度变换 */
    
public
BufferedImage lineGrey() {
        
PixelGrabber pg =
new
PixelGrabber(image.getSource(),
0
,
0
, iw, ih,
                
pixels,
0
, iw);
        
try
{
            
pg.grabPixels();
        
}
catch
(InterruptedException e) {
            
e.printStackTrace();
        
}
        
// 对图像进行进行线性拉伸,Alpha值保持不变
        
ColorModel cm = ColorModel.getRGBdefault();
        
for
(
int
i =
0
; i < iw * ih; i++) {
            
int
alpha = cm.getAlpha(pixels[i]);
            
int
red = cm.getRed(pixels[i]);
            
int
green = cm.getGreen(pixels[i]);
            
int
blue = cm.getBlue(pixels[i]);
 
            
// 增加了图像的亮度
            
red = (
int
) (
1.1
* red +
30
);
            
green = (
int
) (
1.1
* green +
30
);
            
blue = (
int
) (
1.1
* blue +
30
);
            
if
(red >=
255
) {
                
red =
255
;
            
}
            
if
(green >=
255
) {
                
green =
255
;
            
}
            
if
(blue >=
255
) {
                
blue =
255
;
            
}
            
pixels[i] = alpha <<
24
| red <<
16
| green <<
8
| blue;
        
}
 
        
// 将数组中的象素产生一个图像
 
        
return
ImageIOHelper
                
.imageProducerToBufferedImage(
new
MemoryImageSource(iw, ih,
                        
pixels,
0
, iw));
    
}
 
    
/** 转换为黑白灰度图 */
    
public
BufferedImage grayFilter() {
        
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        
ColorConvertOp op =
new
ColorConvertOp(cs,
null
);
        
return
op.filter(image,
null
);
    
}
 
    
/** 平滑缩放 */
    
public
BufferedImage scaling(
double
s) {
        
AffineTransform tx =
new
AffineTransform();
        
tx.scale(s, s);
        
AffineTransformOp op =
new
AffineTransformOp(tx,
                
AffineTransformOp.TYPE_BILINEAR);
        
return
op.filter(image,
null
);
    
}
 
    
public
BufferedImage scale(Float s) {
        
int
srcW = image.getWidth();
        
int
srcH = image.getHeight();
        
int
newW = Math.round(srcW * s);
        
int
newH = Math.round(srcH * s);
        
// 先做水平方向上的伸缩变换
        
BufferedImage tmp =
new
BufferedImage(newW, newH, image.getType());
        
Graphics2D g = tmp.createGraphics();
        
for
(
int
x =
0
; x < newW; x++) {
            
g.setClip(x,
0
,
1
, srcH);
            
// 按比例放缩
            
g.drawImage(image, x - x * srcW / newW,
0
,
null
);
        
}
 
        
// 再做垂直方向上的伸缩变换
        
BufferedImage dst =
new
BufferedImage(newW, newH, image.getType());
        
g = dst.createGraphics();
        
for
(
int
y =
0
; y < newH; y++) {
            
g.setClip(
0
, y, newW,
1
);
            
// 按比例放缩
            
g.drawImage(tmp,
0
, y - y * srcH / newH,
null
);
        
}
        
return
dst;
    
}
 
}

转载于:https://www.cnblogs.com/leo3689/p/4419580.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109492.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 一文搞懂双亲委派模型「建议收藏」

    一文搞懂双亲委派模型「建议收藏」类加载器虚拟机设计团队把类加载阶段中的“通过一个类的全限定名来获取此类的二进制字节流”这个动作放到Java虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类。实现这个动作的代码模块称为“类加载器”。从Java虚拟机的角度来讲,只存在以下两种不同的类加载器:启动类加载器(BootstrapClassLoader),使用C++实现,是虚拟机自身的一部分所有其它类的加载…

  • SpringBoot非官方教程 | 第十篇: 用Spring Restdocs创建API文档「建议收藏」

    SpringBoot非官方教程 | 第十篇: 用Spring Restdocs创建API文档

  • 2019latex安装教程

    2019latex安装教程菜鸡一枚,一开始装latex找了好多博客,按照他们的步骤走还是失败,最后把他们集齐,拼在了一起,终于召唤出了latex!!!参考博客链接:安装教程环境变量教程安装教程环境变量教程其实还找了许多,但是最上面的四个教程是最有用的(对我来说)首先去这个链接下latex官网具体操作步骤看这个安装教程,他写的很好很详细,在按照步骤装好texlive后,先别急!,要更改环境变量(我是需要的…

  • html中bgsound背景音乐标签在浏览器里无法播放[通俗易懂]

    html中bgsound背景音乐标签在浏览器里无法播放[通俗易懂]1.原代码:问题:经过尝试,发现仅仅只有IE浏览器可以支持自动播放,但是需要先进行添加控件(自动弹出)。其他浏览器不支持自动播放。查找W3C后发现是bgsound的兼容性

  • 安卓root权限管理_root权限在哪里设置

    安卓root权限管理_root权限在哪里设置Android权限说明Android系统是运行在Linux内核上的,Android与Linux分别有自己的一套严格的安全及权限机制,Android系统权限相关的内容,(一)linux文件系统上的权限-rwxr-x–xsystemsystem41562012-06-3016:12test.apk.代表的是相应的用户/用户组及其他人对此文件的访问权限,与此文件运行起来

    2022年10月29日
  • 【值得收藏】一份非常完整的Mysql规范

    做一个积极的人编码、改bug、提升自己我有一个乐园,面向编程,春暖花开!本文转载在,在原有内容基础上结合阿里巴巴Java开发手册中Mysql数据库章节的介绍,加上自己的理解和说明,整理而成。涉及的内容较多,建议收藏后仔细阅读!一、数据库命名规范数据库的表名、字段名必须使用小写字母或数字(不要使用驼峰哦),并禁止出现数字开头,禁止两个下划线中间只出现数字。正例:aliyun _ …

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号