Java Identification Verification Code

Source: Internet
Author: User
Tags eol locale stringbuffer

Resources Download links (resources free, heavy share)

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

jai_imageio-1.1-alpha,swingx-1.0:http://download.csdn.net/detail/chenyangqi/9190683

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

Project Introduction:

Our school is using the campus Rui Jie client Internet, I because of long time without password, has forgotten the password, only remember is six pure numbers, and do not want to go to service Center for password, I would like to use HTTP simulation of Ruijie Web version to achieve landing, traverse 000000-999999 of the password. Also learn by the way. Nonsense not to say, directly into the theme, landing page as follows, user name password verification code:

To achieve the simulation landing difficulties there are two, one is the HTTP request Cookie management, one is the verification code identification, change article explains how to identify the verification code, as for the simulation landing please see my late post (blog connection: Http://www.cnblogs.com/chenyangqi /p/4906376.html)

  

1: Download and install Tesseract

I have provided download link (the article at the beginning of the download link), download the installation can, compression package also provides a Chinese language pack, Tesseract is a support for Chinese recognition of OCR, which is why I chose him, as for the installation and verification of the success of the installation, Baidu, I will not nonsense.

The location I installed is: D:\Program Files (x86) \TESSERACT-OCR, under which the structure is as follows (where Tessdata is where the language pack resides)

Second: Verification Code acquisition

Download httpwatch (download link at the beginning of the article), install, and use in IE, the landing page to grab the packet, find the URL of the verification code, such as (HttpWatch installation use method, self-Baidu);

Obtain the URL after the key verification code picture is downloaded to local. For example: d://verifycode.jpg

Download the image to the local code as follows:

Private Static voidgetImage (String name_get, String password_get) {GetMethod get=NewGetMethod ("Your Captcha url"); Try{Client.executemethod (GET); File StoreFile=NewFile ("D:/verifycode.jpg"); Saved locally on path FileOutputStream output=NewFileOutputStream (StoreFile); InputStream is=Get.getresponsebodyasstream (); FileOutputStream Fos=NewFileOutputStream (StoreFile); byte[] B =New byte[1024];  while((Is.read (b))! =-1) {fos.write (b);            } is.close (); Fos.close ();        } Catch(IOException e) {e.printstacktrace (); }    }

  

Three: Java implementation Verification Code identification

The code below, Eclipse creates a new Java project, introduces Jai_imageio-1.1-alpha.jar,swingx-1.0.jar two packages (download link at the beginning of the article) and imports into the project. OK, ready to write code, a total of two classes Orc.class Imageiohelper.class (just modify the path you installed Tesseract, you can directly introduce your project to use).

The Ocr.class code is as follows:the parameters of the Recognizetext (File imagefile, String imageformat) method are the local location of the verification code picture downloaded in the previous step. D://verifycode.jpg

 Packagecom.cyq.request;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStreamReader;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.jdesktop.swingx.util.OS; Public classOCR {Private FinalString lang_option = "-L";//English Letter lowercase L, not number 1    Private FinalString EOL = System.getproperty ("Line.separator"); PrivateString Tesspath = "D://program Files (x86)//TESSERACT-OCR";//tesseract Installation path Publicstring Recognizetext (File imagefile, string imageformat)throwsException {File tempimage=imageiohelper.createimage (ImageFile, ImageFormat); File OutputFile=NewFile (Imagefile.getparentfile (), "Output"); StringBuffer StrB=NewStringBuffer (); List<String> cmd =NewArraylist<string>(); if(Os.iswindowsxp ()) {Cmd.add (Tesspath+ "//tesseract"); } Else if(Os.islinux ()) {Cmd.add ("Tesseract"); } Else{cmd.add (Tesspath+ "//tesseract"); } cmd.add ("");        Cmd.add (Outputfile.getname ()); Processbuilder PB=NewProcessbuilder ();        Pb.directory (Imagefile.getparentfile ()); Cmd.set (1, Tempimage.getname ());        Pb.command (CMD); Pb.redirecterrorstream (true); Process Process=Pb.start (); intW =process.waitfor (); //Delete temporary working filesTempimage.delete (); if(W = = 0) {BufferedReader in=NewBufferedReader (NewInputStreamReader (NewFileInputStream (Outputfile.getabsolutepath () + ". txt"),                    "UTF-8"));            String str;  while(str = in.readline ())! =NULL) {strb.append (str). append (EOL);        } in.close (); } Else{String msg; Switch(w) { Case1: Msg= "Errors accessing files. There may is spaces in your image ' s filename. ";  Break;  Case29: Msg= "Cannot recongnize the image or its selected region.";  Break;  Case31: Msg= "Unsupported image format.";  Break; default: Msg= "Errors occurred.";        } tempimage.delete (); }        NewFile (Outputfile.getabsolutepath () + ". txt"). Delete (); returnstrb.tostring (); }}

The Imageiohelper.class code is as follows:

 Packagecom.cyq.request;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.Iterator;ImportJava.util.Locale;ImportJavax.imageio.IIOImage;ImportJavax.imageio.ImageIO;ImportJavax.imageio.ImageReader;ImportJavax.imageio.ImageWriteParam;ImportJavax.imageio.ImageWriter;ImportJavax.imageio.metadata.IIOMetadata;ImportJavax.imageio.stream.ImageInputStream;ImportJavax.imageio.stream.ImageOutputStream;ImportCom.sun.media.imageio.plugins.tiff.TIFFImageWriteParam; Public classImageiohelper {/*** image file converted to TIF format * *@paramimagefile * File path *@paramimageformat * File extension *@return     */     Public Staticfile createimage (file ImageFile, String imageformat) {file Tempfile=NULL; Try{Iterator<ImageReader> readers =ImageIO. Getimagereadersbyformatname (ImageFormat); ImageReader Reader=Readers.next (); Imageinputstream IIS=Imageio.createimageinputstream (ImageFile);            Reader.setinput (IIS); Iiometadata Streammetadata=Reader.getstreammetadata (); Tiffimagewriteparam Tiffwriteparam=NewTiffimagewriteparam (Locale.chinese);            Tiffwriteparam.setcompressionmode (imagewriteparam.mode_disabled); Iterator<ImageWriter> writers =ImageIO. Getimagewritersbyformatname ("Tiff"); ImageWriter writer=Writers.next (); BufferedImage Bi= Reader.read (0); Iioimage Image=NewIioimage (BI,NULL, Reader.getimagemetadata (0)); Tempfile=Tempimagefile (ImageFile); Imageoutputstream iOS=Imageio.createimageoutputstream (tempfile);            Writer.setoutput (iOS);            Writer.write (streammetadata, image, Tiffwriteparam);            Ios.close ();            Writer.dispose ();        Reader.dispose (); } Catch(IOException e) {e.printstacktrace (); }        returnTempfile; }    Private Staticfile Tempimagefile (file imagefile) {String path=Imagefile.getpath (); StringBuffer StrB=NewStringBuffer (path); Strb.insert (Path.lastindexof ('. '), 0); return NewFile (Strb.tostring (). Replacefirst ("(? <=//.) (//w+) $ "," TIF ")); }}

The call in the main method is OK:

  

Private Staticstring GetCode () {string Valcode=NULL; String Path= "D://verifycode.jpg"; Try{Valcode=NewOCR (). Recognizetext (NewFile (path), "JPG");
System.out.println ("Verification Code:" +valcode)}Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } returnValcode; }

OK, verification code identification over, as for the entire HTTP landing please see my post (http://www.cnblogs.com/chenyangqi/p/4906376.html).

Disclaimer: The blog post for the original, reproduced please indicate the source

This program is only used for learning, do not use this content to engage in illegal activities and violent cracking activities

Java Identification Verification Code

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.