Java verification code and java Verification Code

Source: Internet
Author: User

Java verification code and java Verification Code

Download link for required resources (resources are free of charge and shared among others)

Tesseract: http://download.csdn.net/detail/chenyangqi/9190667

Jai_imageio-1.1-alpha: swingx-1.0

HttpWatch Professional: http://download.csdn.net/detail/chenyangqi/9208339

Project Introduction:

Our school uses the campus ruijie client to access the Internet. Because I don't need a password for a long time, I have forgotten the password. I only remember that it was six pure numbers and I didn't want to change the password at the service center, I just want to use Http to simulate the login of ruijie web edition, and traverse the password of 000000-999999. By the way. Go to the topic directly. The logon page is as follows:

There are two difficulties to implement simulated login. One is Cookie Management in Http requests, the other is verification code identification, and the other is how to identify the verification code, as for the simulated login please see my post blog (blog connection: http://www.cnblogs.com/chenyangqi/p/4906376.html)

  

1: download and install Tesseract

I have provided a download link (the download link is provided at the beginning of the article). Download and install it. The compressed package also provides a Chinese Language Pack. Tesseract is an OCR service that supports Chinese recognition, this is also the reason why I chose him. As for installation and verification, I am not talking nonsense.

The installation path is D: \ Program Files (x86) \ Tesseract-OCR. The structure in this directory is as follows (tessdata is the location where the Language Pack is stored)

Ii. Verification Code Acquisition

Download HttpWatch (the download link is provided at the beginning of the article), install it, use it in IE, capture packets on the login page, and find the URL of the verification code, for example (how to install and use HttpWatch, self-built Baidu );

After obtaining the URL, download the verification code image to your local device. For example: D: // verifycode.jpg

The code for downloading an image to a local machine is as follows:

Private static void getImage (String name_get, String password_get) {GetMethod get = new GetMethod ("your verification code URL"); try {client.exe cuteMethod (get ); file storeFile = new File ("D:/verifycode.jpg"); // The local path FileOutputStream output = new FileOutputStream (storeFile); InputStream is = get. getResponseBodyAsStream (); FileOutputStream fos = new FileOutputStream (storeFile); byte [] B = new byte [1024]; while (Is. read (B ))! =-1) {fos. write (B);} is. close (); fos. close ();} catch (IOException e) {e. printStackTrace ();}}

 

 

  

Iii. Java verification code recognition

The Code is as follows, eclipse creates a java project, introduces the jai_imageio-1.1-alpha.jar, the two packages (the download link is provided at the beginning of the article), imports into the project. OK. Prepare to write the code. There are two classes in total: ORC. class ImageIOhelper. class (you only need to modify the path for installing Tesseract, which can be directly introduced to your project ).

The OCR. class Code is as follows: the parameter of the recognizeText (File imageFile, String imageFormat) method is the local location of the Verification Code image downloaded in the previous step. D: // verifycode.jpg

Package com. cyq. request; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. inputStreamReader; import java. util. arrayList; import java. util. list; import org. jdesktop. swingx. util. OS; public class OCR {private final String LANG_OPTION = "-l"; // lowercase l, not a number 1 private final String EOL = System. getProperty ("line. separator "); private String tessPath =" D: // Progra M Files (x86) // Tesseract-OCR "; // Tesseract installation path public String recognizeText (File imageFile, String imageFormat) throws Exception {File tempImage = ImageIOHelper. createImage (imageFile, imageFormat); File outputFile = new File (imageFile. getParentFile (), "output"); StringBuffer strB = new StringBuffer (); List <String> cmd = new ArrayList <String> (); if (OS. isWindowsXP () {cmd. add (tessPath + "// tesserac T ");} else if (OS. isLinux () {cmd. add ("tesseract");} else {cmd. add (tessPath + "// tesseract");} cmd. add (""); cmd. add (outputFile. getName (); ProcessBuilder pb = new ProcessBuilder (); pb. directory (imageFile. getParentFile (); cmd. set (1, tempImage. getName (); pb. command (cmd); pb. redirectErrorStream (true); Process process = pb. start (); int w = process. waitFor (); // Delete the temporary working file tempImage. delete (); if (W = 0) {BufferedReader in = new BufferedReader (new InputStreamReader (new FileInputStream (outputFile. getAbsolutePath () + ". txt ")," UTF-8 "); String str; while (str = in. readLine ())! = Null) {strB. append (str ). append (EOL);} in. close () ;}else {String msg; switch (w) {case 1: msg = "Errors accessing files. there may be spaces in your image's filename. "; break; case 29: msg =" Cannot recongnize the image or its selected region. "; break; case 31: msg =" Unsupported image format. "; break; default: msg =" Errors occurred. ";} tempImage. delete ();} new File (outputFile. getAbsolutePath () + ". txt "). delete (); return strB. toString ();}}

The ImageIOhelper. class Code is as follows:

Package com. cyq. request; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import java. util. iterator; import java. util. locale; import javax. imageio. IIOImage; import javax. imageio. imageIO; import javax. imageio. imageReader; import javax. imageio. imageWriteParam; import javax. imageio. imageWriter; import javax. imageio. metadata. IIOMetadata; import javax. imageio. stream. imageInputSt Ream; import javax. imageio. stream. imageOutputStream; import com. sun. media. imageio. plugins. tiff. required imagewriteparam; public class ImageIOHelper {/*** convert the image File to tif format ** @ param imageFile * File path * @ param imageFormat * File extension * @ return */public static File createImage (File imageFile, string imageFormat) {File tempFile = null; try {Iterator <ImageReader> readers = ImageIO. getImageReadersByFormatName (ima GeFormat); ImageReader reader = readers. next (); ImageInputStream iis = ImageIO. createImageInputStream (imageFile); reader. setInput (iis); IIOMetadata streamMetadata = reader. getStreamMetadata (); Specify imagewriteparam before writeparam = new specify imagewriteparam (Locale. CHINESE); required writeparam. setCompressionMode (ImageWriteParam. MODE_DISABLED); Iterator <ImageWriter> writers = ImageIO. getImageWritersByForm AtName ("tiff"); ImageWriter writer = writers. next (); BufferedImage bi = reader. read (0); IIOImage image = new IIOImage (bi, null, reader. getImageMetadata (0); tempFile = tempImageFile (imageFile); ImageOutputStream ios = ImageIO. createImageOutputStream (tempFile); writer. setOutput (ios); writer. write (streamMetadata, image, writable writeparam); ios. close (); writer. dispose (); reader. dispose ();} catch (IOExc Eption e) {e. printStackTrace ();} return tempFile;} private static File tempImageFile (File imageFile) {String path = imageFile. getPath (); StringBuffer strB = new StringBuffer (path); strB. insert (path. lastIndexOf ('. '), 0); return new File (strB. toString (). replaceFirst ("(? <= //.) (// W +) $ "," tif "));}}

 

The call to the Main method is OK:

  

Private static String getCode () {String valCode = null; String path = "d: // verifycode.jpg"; try {valCode = new OCR (). recognizeText (new File (path), "jpg ");
System. out. println ("Verification Code:" + valCode)} catch (IOException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} return valCode ;}

 

Okay, verification code identification over, as for the entire Http Login please see my post blog (http://www.cnblogs.com/chenyangqi/p/4906376.html ).

Statement: This blog post is original to the blogger. Please indicate the source for reprinting.

This program simulation is only used for learning. Do not use this content for illegal activities or brute-force cracking activities.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.