|
1 public class GetImageValue
2 {
3 //设定图片RGB字符串
4 string[] ArrayList = new string[]{
5
"0001110001111111011000111100000111000001110000011100000111000001110000011100
0001011000110111111100011100", //0
6
"0011100011111000111110000001100000011000000110000001100000011000000110000001
1000000110001111111111111111", //1
7
"0111110011111110100000110000001100000011000001100000110000011000001100000110
0000110000001111111111111111", //2
8
"0111110011111111100000110000001100000110011110000111111000000111000000110000
0011100001111111111001111100", //3
9
"0000110000011100000111000011110001101100011011001000110011001100111111111011
1111000011000000110000001100", //4
10
"1111111111111111110000001100000011000000111110001111111000000111000000110000
0011100001111111111001111100", //5
11
"0001111000111111011000010110000011000000110111101110111111100011110000010100
0001011000110110111100011110", //6
12
"0111101000111111000000010000000000000011000001100000010000001100000010000001
1000000110000011000000110000", //7
13
"0011111001111111011000110110001101110010001111100011111001100111110000011100
0001111000110111111100111110", //8
14
"0001110001111011011000111100000111000001101000110111111100111101000000010000
0001010000110101110000111100", //9
15
"0011100011111110100001001000001110000011100000111000001100000011100000111000
0011110001001111011000111000", //10
16
"0000110001111100011111000000110000001100000011000000110000001100000011000000
1100000011000111110101111111", //11
17
"1111100011011100000001000000011000000110000011000001100000110000011000001100
0000100000000101111011111110", //12
18
"1011100011111110000000100000011000001100111100001111010000001110000001100000
0110000011101111110011111000", //13
19
"0000011000001110000011100000111000010110001101100010011001100100111111111111
1111000001000000011000000110", //14
20
"1111111011111110100000001000000010000000111100001111010000001110000001100000
0110000011101111110011111000", //15
21
"0011110001111110110000101100000010000000101111001111111011000111100000111000
0011110001101111111000111100", //16
22
"1110111111111111000000110000001000000110000001000000100000011000000100000011
0000001100000110000001100000", //17
23
"0111110011111110110001101100011011000100011111000111110011001110100000111000
0011110001101101111001111100", //18
24
"0111100011111110110001101000001110000011110001111111111101111011000000110000
0100100001101111110001111000", //19
25
"0011110001111111110000111000000111000001111000110111111100110101000000000000
0011010000010111111000111100", //20=9
26
"0011100011111100110001101000001110000001100000110000001110000011100000101000
0011110001101111010000101000", //21=0
27
"0011100001011101011000111100000111000001111000110111111100111101000000010000
0011010000100111011000111100", //22=9
28
"0000011000001010000011100001011000110110001101100100011001100110101110111111
1111000001100000011000000110", //23=4
29
"0001111000101111011000010100000011000000110110101111110101100011110000011000
0001010000110101111000011110", //24=6
30
"0011110001111110111000110100000111000001111000110111111100111101000000010000
0011000000110101101000111100", //25=9
31
"1111100011110000000001100000010000000010000011000001100000110000011000000100
0000100000001111110011111110" //26=2
32 };
33
34 /// <summary>
35 /// 获取图片验证码数字
36 /// </summary>
37 /// <returns></returns>
38 public string GetImageValues()
39 {
40 string url = "http://xxxx.xxxx/image";
41 WebRequest myWebRequest = WebRequest.Create(url);
42 WebResponse myWebResponse = myWebRequest.GetResponse();
43 Stream ReceiveStream = myWebResponse.GetResponseStream();
44 Bitmap map = new Bitmap(ReceiveStream, false);
45 UnCodebase ucode = new UnCodebase(map);
46
47 ucode.GrayByPixels(); //灰度处理
48
49 Bitmap[] pics = ucode.readMap();
50 int[] gray = new int[4];
51 for (int j = 0; j < 4; j++)
52 {
53 gray[j] = ucode.GetSingleDgGrayValue(pics[j]);
54 }
55 string[] arr = new string[4];
56 for (int i = 0; i < 4; i++)
57 {
58 arr[i] = ucode.GetSingleBmpCode(pics[i], gray[i]);
59 }
60 string picnum = getPicnums(arr);
61 return picnum;
62 }
63
64 public string getPicnums(string[] arr)
65 {
66 string Code = "";
67 for (int i = 0; i < 4; i++)
68 {
69 string code = arr[i]; //得到代码串
70
71 for (int arrayIndex = 0; arrayIndex < ArrayList.Length;
arrayIndex++)
72 {
73 //逐点判断特征码是否相同,允许误差!
74 char temp1, temp2;
75 int point = 0;
76 if (ArrayList[arrayIndex].Equals(code))
77 {
78 point = 0;
79 if (arrayIndex > 9)
80 {
81 if (arrayIndex == 20 || arrayIndex == 22 ||
arrayIndex == 25)
82 {
83 Code = Code + "9";
84 }
85 else if (arrayIndex == 21)
86 {
87 Code = Code + "0";
88 }
89 else if (arrayIndex == 23)
90 {
91 Code = Code + "4";
92 }
93 else if (arrayIndex == 24)
94 {
95 Code = Code + "6";
96 }
97 else if (arrayIndex == 26)
98 {
99 Code = Code + "2";
100 }
101 else
102 {
103 Code = Code + (arrayIndex - 10).ToString
();
104 }
105 }
106 else
107 {
108 Code = Code + arrayIndex.ToString();
109 }
110 break;
111 }
112 else
113 {
114 //将字符串数组,直接转为单个字符进行对比,并记录
不相同的点
115 for (int Comparison = 0; Comparison <
code.Length; Comparison++)
116 {
117 temp1 = arr[i][Comparison];
118 temp2 = ArrayList[arrayIndex][Comparison];
119 if (temp1 != temp2)
120 {
121 point = point + 1;
122 }
123 }
124 }
125
126 //当不相同点的值小于10的时候,也就是说误差点小于10的
时候则直接等于此数字,否则将跳出循环继续对下一个特征码进行判断
127 if (point < 10)
128 {
129 if (arrayIndex > 9)
130 {
131 if (arrayIndex == 20 || arrayIndex == 22 ||
arrayIndex == 25)
132 {
133 Code = Code + "9";
134 }
135 else if (arrayIndex == 21)
136 {
137 Code = Code + "0";
138 }
139 else if (arrayIndex == 23)
140 {
141 Code = Code + "4";
142 }
143 else if (arrayIndex == 24)
144 {
145 Code = Code + "6";
146 }
147 else if (arrayIndex == 26)
148 {
149 Code = Code + "2";
150 }
151 else
152 {
153 Code = Code + (arrayIndex - 10).ToString
();
154 }
155 }
156 else
157 {
158 Code = Code + arrayIndex.ToString();
159 }
160 break;
161 }
162 }
163 }
164 return Code;
165 }
166
167
168 -------------------------图片处理类
169
170
171 class UnCodebase
172 {
173 public Bitmap bmpobj;
174 public UnCodebase(Bitmap pic)
175 {
176 bmpobj = new Bitmap(pic); //转换为Format32bppRgb
177 }
178
179 /**/
180 /// <summary>
181 /// 根据RGB,计算灰度值
182 /// </summary>
183 /// <param name="posClr">Color值</param>
184 /// <returns>灰度值,整型</returns>
185 private int GetGrayNumColor(System.Drawing.Color posClr)
186 {
187 return (posClr.R * 19595 + posClr.G * 38469 + posClr.B *
7472) >> 16;
188 }
189
190 /**/
191 /// <summary>
192 /// 灰度转换,逐点方式
193 /// </summary>
194 public void GrayByPixels()
195 {
196 for (int i = 0; i < bmpobj.Height; i++)
197 {
198 for (int j = 0; j < bmpobj.Width; j++)
199 {
200 int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j,
i));
201 bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue,
tmpValue, tmpValue));
202 }
203 }
204 }
205
206 /**/
207 /// <summary>
208 /// 去图形边框
209 /// </summary>
210 /// <param name="borderWidth"></param>
211 public void ClearPicBorder(int borderWidth)
212 {
213 for (int i = 0; i < bmpobj.Height; i++)
214 {
215 for (int j = 0; j < bmpobj.Width; j++)
216 {
217 if (i < borderWidth || j < borderWidth || j >
bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
218 bmpobj.SetPixel(j, i, Color.FromArgb(255, 255,
255));
219 }
220 }
221 }
222
223 /**/
224 /// <summary>
225 /// 灰度转换,逐行方式
226 /// </summary>
227 public void GrayByLine()
228 {
229 Rectangle rec = new Rectangle(0, 0, bmpobj.Width,
bmpobj.Height);
230 BitmapData bmpData = bmpobj.LockBits(rec,
ImageLockMode.ReadWrite, bmpobj.PixelFormat);//
PixelFormat.Format32bppPArgb);
231 // bmpData.PixelFormat = PixelFormat.Format24bppRgb;
232 IntPtr scan0 = bmpData.Scan0;
233 int len = bmpobj.Width * bmpobj.Height;
234 int[] pixels = new int[len];
235 Marshal.Copy(scan0, pixels, 0, len);
236
237 //对图片进行处理
238 int GrayValue = 0;
239 for (int i = 0; i < len; i++)
240 {
241 GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
242 pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue,
GrayValue)).ToArgb(); //Color转byte
243 }
244
245 bmpobj.UnlockBits(bmpData);
246 }
247
248 /**/
249 /// <summary>
250 /// 得到有效图形并调整为可平均分割的大小
251 /// </summary>
252 /// <param name="dgGrayValue">灰度背景分界值</param>
253 /// <param name="CharsCount">有效字符数</param>
254 /// <returns></returns>
255 public void GetPicValidByValue(int dgGrayValue, int CharsCount)
256 {
257 int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
258 int posx2 = 0; int posy2 = 0;
259 for (int i = 0; i < bmpobj.Height; i++) //找有效区
260 {
261 for (int j = 0; j < bmpobj.Width; j++)
262 {
263 int pixelValue = bmpobj.GetPixel(j, i).R;
264 if (pixelValue < dgGrayValue) //根据灰度值
265 {
266 if (posx1 > j) posx1 = j;
267 if (posy1 > i) posy1 = i;
268
269 if (posx2 < j) posx2 = j;
270 if (posy2 < i) posy2 = i;
271 };
272 };
273 };
274 // 确保能整除
275 int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount;
//可整除的差额数
276 if (Span < CharsCount)
277 {
278 int leftSpan = Span / 2; //分配到左边的空列 ,如span为
单数,则右边比左边大1
279 if (posx1 > leftSpan)
280 posx1 = posx1 - leftSpan;
281 if (posx2 + Span - leftSpan < bmpobj.Width)
282 posx2 = posx2 + Span - leftSpan;
283 (责任编辑:admin) |