提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.awt.Color; |
|
4 |
import java.awt.Font; |
|
5 |
import java.awt.Graphics; |
|
6 |
import java.awt.image.BufferedImage; |
|
7 |
import java.util.Random; |
|
8 |
|
|
9 |
public class CertifyMethod { |
|
10 |
private int padding=2; |
|
11 |
private String randstr; |
|
12 |
private BufferedImage image1; |
|
13 |
public Color getRandColor(int fc, int bc) { |
|
14 |
Random random = new Random(); |
|
15 |
if (fc > 255) |
|
16 |
fc = 255; |
|
17 |
if (bc > 255) |
|
18 |
bc = 255; |
|
19 |
int r = fc + random.nextInt(bc - fc); |
|
20 |
int g = fc + random.nextInt(bc - fc); |
|
21 |
int b = fc + random.nextInt(bc - fc); |
|
22 |
return new Color(r, g, b); |
|
23 |
} |
|
24 |
public void generateImg(int x,int y,int count,boolean onlynum) |
|
25 |
{ |
|
26 |
int fontsize=y-padding-padding; |
|
27 |
char[] chars; |
|
28 |
if (onlynum == true) { |
|
29 |
chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', |
|
30 |
'9' }; |
|
31 |
} else |
|
32 |
chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', |
|
33 |
'9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', |
|
34 |
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', |
|
35 |
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
36 |
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
37 |
'W', 'X', 'Y', 'Z' }; |
|
38 |
int length = chars.length; |
|
39 |
BufferedImage image = new BufferedImage(x, y, |
|
40 |
BufferedImage.TYPE_INT_RGB); |
|
41 |
Graphics g = image.getGraphics(); |
|
42 |
Random random = new Random(); |
|
43 |
g.setColor(getRandColor(200, 250)); |
|
44 |
g.fillRect(0, 0, x, y); |
|
45 |
Font f=new Font("Times New Roman", Font.PLAIN, fontsize); |
|
46 |
g.setFont(f); |
|
47 |
g.setColor(getRandColor(160, 200)); |
|
48 |
for (int i = 0; i < 155; i++) { |
|
49 |
int x_1 = random.nextInt(x); |
|
50 |
int y_1 = random.nextInt(y); |
|
51 |
int x_2 = random.nextInt(12); |
|
52 |
int y_2 = random.nextInt(12); |
|
53 |
g.drawLine(x_1, y_1, x_1 + x_2, y_1 + y_2); |
|
54 |
} |
|
55 |
int wordpad=(x-6-6)/count; |
|
56 |
String srand=""; |
|
57 |
for(int j=0;j<count;j++){ |
|
58 |
String trand=String.valueOf(chars[random.nextInt(length)]); |
|
59 |
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); |
|
60 |
g.drawString(trand,wordpad*j+6,fontsize); |
|
61 |
srand=srand+trand; |
|
62 |
} |
|
63 |
g.dispose(); |
|
64 |
this.randstr=srand.toLowerCase(); |
|
65 |
this.image1=image; |
|
66 |
|
|
67 |
} |
|
68 |
public void generateImg(int x,int y) |
|
69 |
{ |
|
70 |
generateImg(x,y,5,true); |
|
71 |
} |
|
72 |
public String getRandstr() { |
|
73 |
return randstr; |
|
74 |
} |
|
75 |
public void setRandstr(String randstr) { |
|
76 |
this.randstr = randstr; |
|
77 |
} |
|
78 |
public BufferedImage getImage1() { |
|
79 |
return image1; |
|
80 |
} |
|
81 |
public void setImage1(BufferedImage image1) { |
|
82 |
this.image1 = image1; |
|
83 |
} |
|
84 |
|
|
85 |
} |