package com.util;
002
003 //~--- JDK imports ------------------------------------------------------------
004
005 import com.sun.image.codec.jpeg.JPEGCodec;
006 import com.sun.image.codec.jpeg.JPEGEncodeParam;
007 import com.sun.image.codec.jpeg.JPEGImageEncoder;
008
009 import java.awt.*;
010 import java.awt.image.*;
011
012 import java.io.*;
013 import java.io.FileOutputStream;
014 import java.io.OutputStream;
015
016 import java.net.*;
017
018 import javax.imageio.*;
019 import javax.imageio.ImageIO;
020
021 /** *//**
022 * 登陆验证图片转换为数字
023 *
024 *
025 * @version 1.0, 08/04/20
026 * @author 张健滢
027 */
028 public class ImgIdent {
029
030 // 数字字符
比特表
031 private final long[][] NUMERIC = {
032 { 512104545, 562436190 }, // ''0''
033 { 148931080, 136348222 }, // ''1''
034 { 511971394, 69273663 }, // ''2''
035 { 511971406, 17045598 }, // ''3''
036 { 35168914, 586948743 }, // ''4''
037 { 1065486398, 17045598 }, // ''5''
038 { 239208494, 830871646 }, // ''6''
039 { 1065623684, 69239824 }, // ''7''
040 { 512104542, 562436190 }, // ''8''
041 { 512104547, 486805660 }
042 }; // ''9''
043
044 // 字框高
045 private int intCharHeight = 10;
046
047 // 字框横向间隙
048 private int intCharSpaceH = 5;
049
050 // 字框纵向间隙
051 private int intCharSpaceY = 1;
052
053 // 字框宽
054 private int intCharWidth = 5;
055 private int IntImgHeight;
056 private BufferedImage img;
057 private int intBgColor;
058 private int intCharColor;
059 private int intImgWith;
060 private int intMaxX;
061 private int intMaxY;
062 private int intMinX;
063 private int intMinY;
064
065 // 座标原点
066 private Point pOrigin;
067 private String strNum;
068
069 /** *//**
070 * Constructs
071 *
072 *
073 * @param img
074 *
075 * @throws IOException
076 */
077 public ImgIdent(BufferedImage img) throws IOException {
078 this.img = img;
079 init();
080 }
081
082 /** *//**
083 *
构造函数
084 * @param file 本地文件
085 * @throws IOException
086 */
087 public ImgIdent(File file) throws IOException {
088 img = ImageIO.read(file);
089 init();
090 }
091
092 /** *//**
093 * 构造函数
094 * @param url
远程文件
095 * @throws IOException
096 */
097 public ImgIdent(URL url) throws IOException {
098 img = ImageIO.read(url);
099 init();
100 }
101
102 /** *//**
103 * 类初始工作
104 */
105 private void init() {
106
107 // 得到图象的长度和宽度
108 intImgWith = img.getWidth();
109 IntImgHeight = img.getHeight();
110
111 // 得到图象的背景颜色
112 intBgColor = img.getRGB(7, 4);
113
114 // System.out.println(intBgColor);
115
116 // 初始化图象原点座标
117 pOrigin = new Point(0, 0);
118 }
119
120 /** *//**
121 * Method description
122 *
123 */
124 private void getBaseInfo() {
125 System.out.println(intBgColor + "|" + intCharColor);
126 System.out.println(intMinX + "|" + intMinY + "|" + intMaxX + "|" + intMaxY);
127 }
128
129 /** *//**
130 * 得到字符的左上右下点座标
131 * @param intNo int 第n个字符
132 * @return int[]
133 */
134 private Point[] getCharRange(int intNo) {
135
136 // 左上右下点座标
137 Point pTopLeft = new Point(0, 0);
138 Point pBottomRight = new Point(0, 0);
139
140 // 左上点
141 pTopLeft.x = pOrigin.x + intCharWidth * (intNo - 1) + intCharSpaceH * (intNo - 1);
142 pTopLeft.y = pOrigin.y;
143
144 // 右下点
145 pBottomRight.x = 1 + pOrigin.x + intCharWidth * intNo + intCharSpaceH * (intNo - 1) - 1;
146 pBottomRight.y = pOrigin.y + intCharHeight - 1;
147
148 return new Point[] { pTopLeft, pBottomRight };
149 }
150
151 /** *//**
152 * 与背景颜色比较返回相应的字符
153 * @param x int 横座标
154 * @param y int 纵座标
155 * @return char 返回字符
156 */
157 private char getBit(int x, int y) {
158 int intCurtColor;
159
160 intCurtColor = img.getRGB(x, y);
161
162 // System.out.println("[" + x + "," + y + "]" + intCurtColor + "==" + intBgColor + "==>" + (Math.abs(intCurtColor) >7308252));
163 // return (Math.abs(intCurtColor) >= 5689325)
164 // ? ''0''
165 // : ''1'';
166 return (intCurtColor == intBgColor)
167 ? ''0''
168 : ''1'';
169
170 // 5689325 6008535
171 }
172
173 /** *//**
174 * 得到第n个字符对应的字符串
175 * @param intNo int 第n个字符
176 * @return String 代表字符位的串
177 */
178 private String getCharString(int intNo) {
179
180 // 本字符的左上右下点座标
181 Point[] p = getCharRange(intNo);
182 Point pTopLeft = p[0];
183 Point pBottomRight = p[1];
184
185 // 换算边界值
186 int intX1, intY1, intX2, intY2;
187
188 intX1 = pTopLeft.x;
189 intY1 = pTopLeft.y;
190 intX2 = pBottomRight.x;
191 intY2 = pBottomRight.y;
192
193 // System.out.println("intX1=" + intX1);
194 // System.out.println("intY1=" + intY1);
195 // System.out.println("intX2=" + intX2);
196 // System.out.println("intY2=" + intY2);
197
198 // 在边界内循环取象素
199 int i, j;
200 String strChar = "";
201
202 for (i = intY1; i <= intY2; i++) {
203 for (j = intX1; j <= intX2; j++) {
204 System.out.print(getBit(j, i));
205 strChar = strChar + getBit(j, i);
206 }
207
208 System.out.println();
209 }
210
211 System.out.println();
212
213 return strChar;
214 }
215
216 /** *//**
217 * 得到第n个字符对应数值
218 * @param intNo int 第n个字符
219 * @return int 对应数值
220 */
221 public int getNum(int intNo) {
222
223 // 取得位字符串
224 String strChar = getCharString(intNo);
225
226 // System.out.println(intNo+"=="+strChar);
227 // 取得串高位串和低位串
228 String strCharHigh = strChar.substring(0, strChar.length() / 2);
229 String strCharLow = strChar.substring(strChar.length() / 2);
230
231 // 计算高位和低位值
232 long lCharHigh = Long.parseLong(strCharHigh, 2);
233
234 System.out.println(lCharHigh);
235
236 long lCharLow = Long.parseLong(strCharLow, 2);
237
238 System.out.println(lCharLow);
239
240 // 在数字中循环比较
241 int intNum = ''*'';
242
243 for (int i = 0; i <= 9; i++) {
244 if ((lCharHigh == NUMERIC[i][0]) && (lCharLow == NUMERIC[i][1])) {
245 intNum = i;
246
247
break;
248 } else {
249 if ((lCharHigh == 834533329) && (lCharLow == 242870177)) {
250 intNum = 6;
251 } // 834533329 242870177
252 else {
253 intNum = 1;
254 } // 默认为1 低位为 937393609 937393601
255 }
256 }
257
258 return intNum;
259 }
260
261 /** *//**
262 * 保存图片
263 *
264 *
265 * @param length
266 *
267 * @return
268 */
269 public String getValidatecode(int length) {
270 String strNum = "";
271
272 for (int i = 1; i <= length; i++) {
273 synchronized (this) {
274 strNum += String.valueOf(getNum(i));
275 }
276 }
277
278 return strNum;
279 }
280
281 /** *//**
282 * Method description
283 *
284 *
285 * @param iag
286 * @param savePath
287 *
288 * @throws FileNotFoundException
289 * @throws IOException
290 */
291 public void saveJPEG(BufferedImage iag, String savePath) throws FileNotFoundException, IOException {
292 OutputStream jos = new FileOutputStream(savePath);
293 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jos);
294 JPEGEncodeParam jpegEP = JPEGCodec.get
DefaultJPEGEncodeParam(iag);
295
296 jpegEP.setQuality((float) 1, true);
297 encoder.encode(iag, jpegEP);
298 jos.flush();
299 jos.close();
300 }
301 }
302
303
304
305 恩这样数字是可以识别出来了,可以我要怎么完成提交那块的工作呢?好在Apache已经为我做完了。我用了
306 HttpClient这样一个模拟Http
协议的小工具包。我只要往那个 Add_MSG.asp里面提交就完了。
307
308
309
310 package com.util;
311
312 //~--- non-JDK imports --------------------------------------------------------
313
314 import org.apache.commons.httpclient.*;
315 import org.apache.commons.httpclient.methods.GetMethod;
316 import org.apache.commons.httpclient.methods.PostMethod;
317 import org.apache.commons.httpclient.params.HttpClientParams;
318 import org.apache.commons.httpclient.params.HttpMethodParams;
319
320 //~--- JDK imports ------------------------------------------------------------
321
322 import java.awt.image.BufferedImage;
323
324 import java.io.InputStream;
325
326 import javax.imageio.ImageIO;
327
328
329 public class MyHttpClient {
330
331 /** *//**
332 * Method description
333 *
334 *
335 * @param title 留言标题
336 * @param name 留言者
337 * @param Content 内容
338 * @param proIP 代理IP
339 * @param port 代理端口
340 * @param usePro 是否使用代理
341 */
342 public synchronized void doSomeThing(String title, String name, String Content, String proIP, int port,
343 boolean usePro) {
344
345 // 构造HttpClient的实例
346 HttpClient httpClient = new HttpClient();
347 HttpClientParams clientParams = new HttpClientParams();
348
349 // 隐藏自己请求相关的信息
350 clientParams.setParameter("http.useragent", "
Mozilla/4.0 (compatible; FIREFOX 9.0; IBM AIX 5)");
351
352 // httpClient.getHttpConnectionManager().getParams().setSoTimeout(30 * 1000);
353 clientParams.setHttpElementCharset("GBK");
354
355 HttpState httpState = new HttpState();
356
357 httpClient.setParams(clientParams);
358 httpClient.getParams(), .setParameter(HttpClientParams.
HTTP_CONTENT_CHARSET, "GBK");
359 httpClient.setState(httpState);
360 clientParams.setVersion(HttpVersion.HTTP_1_1);
361
362 // httpClient.getHostConfiguration().setProxy("148.233.159.58", 3128);
363
364 if (usePro) // 使用代理
365 {
366 httpClient.getHostConfiguration().setProxy(proIP, port);
367 }
368
369 // 创建GET方法的实例
370 GetMethod getMethod = new GetMethod("http://www.XXXcom/Guestbook/imgchk/validatecode.asp");
371
372 // 使用系统提供的默认的恢复策略
373 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
374
375 try {
376
377 // 执行getMethod
378 int statusCode = httpClient.executeMethod(getMethod);
379
380 // System.out.println(statusCode);
381 if (statusCode != HttpStatus.SC_OK) {
382 System.err.println("Method
failed: " + getMethod.getStatusLine());
383 } // 读取内容
384
385 InputStream inStream = getMethod.getResponseBodyAsStream();
386
387 // 处理内容
388 // System.out.println(new String(responseBody));
389 BufferedImage iag = ImageIO.read(inStream);
390 ImgIdent imgIdent = new ImgIdent(iag);
391
392 // imgIdent.saveJPEG(iag, "C:/ddd.jpg");
393 String validate = imgIdent.getValidatecode(4);
394
395 System.out.println(validate);
396
397 PostMethod method = new PostMethod("http://www.XXX.com/Guestbook/add_msg.asp");
398 String connect = Content;
399 String Title = title;
400
401 method.setParameter("subject", Title);
402 method.setParameter("g_name", name);
403 method.setParameter("companyname", "");
404 method.setParameter("mail", "");
405 method.setParameter("homepageurl", "http://");
406 method.setParameter("pic", "p5.gif");
407 method.setParameter("validatecode", validate);
408 method.setParameter("content", connect);
409
410 // if (todo) {
411 int code = httpClient.executeMethod(method);
412
413 // String Stringresponse = new String(method.getResponseBodyAsString().getBytes("8859_1"));
414 // 打印返回的信息
415 // System.out.println(Stringresponse);
416 // }
417
418 method.releaseConnection();
419
420 // System.out.println(iag.getHeight());
421 // System.out.println(iag.getWidth());
422 // //背景 颜色
423 // intBgColor = iag.getRGB(38, 0);
424 // System.out.println("intBgColor=" + intBgColor);
425 //
426 //
427 // intBgColor = iag.getRGB(0, 0);
428 // System.out.println("intBgColor=" + intBgColor);
429 } catch (Exception e) {
430
431 // 发生网络
异常
432 e.printStackTrace();
433 } finally {}
434
435 // 释放连接 getMethod.releaseConnection(); }
436 getMethod.releaseConnection();
437 }
438 }
文章出处:飞诺网(www.diybl.com):http://www.diybl.com/course/3_program/java/javashl/2008426/111563.html