Detailed description of JavaSE image verification code simple recognition program, javase Verification Code

Source: Internet
Author: User

Detailed description of JavaSE image verification code simple recognition program, javase Verification Code

This article shares with you the simple JavaSE image verification code recognition program for your reference. The specific content is as follows:

First, you should collect samples for images, and then perform grayscale processing on the samples, that is, convert them into black and white colors.

Then you can use this class to analyze the target file. I think this class is very clear about how to implement it. It is to move the sample horizontally from the left and adjust the coordinates to the next position after matching an appropriate one.

This program was written more than three years ago and was not written in depth. image recognition is a very deep field and requires a deep mathematical skills and thinking ability, this java program is inefficient and cannot recognize distorted or stretched images. However, in those days, it was enough. If you have better open-source image recognition code, please be sure to send a letter to us :)

/*** Image Parsing engine, suitable for analyzing website verification codes. * First, the sample must be loaded. The parser will scan the image from left to right and automatically record it if it is found in the sample. * Of course, this program is not suitable for samples that are not unique. That is to say, the image to be recognized is scaled, the coordinates are changed, and the deformation cannot be identified in this program. * If the color in the image changes greatly, this program may be faulty. Of course, you can select a standard value as the standard for converting to a 0, 1 matrix. ** Sample creation: Please convert the sample to the gray mode. It is best to only contain two colors. Of course, I will help you convert it if you do not convert it. **/Import java. awt. image; import java. awt. image. bufferedImage; import java. io. file; import java. util. arrayList; import java. util. iterator; import java. util. list; import javax. imageio. imageIO; public class ImageParser {// samples Private Data // matrix of samples private static List swatches = null; // The sample value private static List swatcheValues = null; // matrix pr of image files Ivate byte [] [] targetColors; // ---------------------------------------------------------------- Test main method public static void main (String [] args) {// Add the value String [] files = new String [10]; String [] values = new String [10]; for (int I = 0; I <files. length; I ++) {files [I] = "D:/workspace/SZXClientAPP/res/" + I + ". jpg "; values [I] = String. valueOf (I);} ImageParser parse = new ImageParse R (files, values); long startime = System. currentTimeMillis (); try {// parse the image System. out. println (parse. parseValue ("D:/workspace/SZXClientAPP/res/ValidateNum"); long sincetime = System. currentTimeMillis (); System. out. println ("time spent =" + (sincetime-startime);} catch (Exception e) {e. printStackTrace () ;}// ---------------------------------------------------------------- Constructors/*** load all sample paths Value corresponding to the sample ** @ param files */public ImageParser (String [] files, String [] values) {// only one sample can be created once. if (swatches = null & swatcheValues = null) {int fileslength = files. length; int valueslength = values. length; if (fileslength! = Valueslength) {System. out. println ("the sample file does not match the sample value! Set it again! "); Return;} swatches = new ArrayList (fileslength); swatcheValues = new ArrayList (valueslength); int I = 0; try {for (; I <files. length; I ++) {swatches. add (imageToMatrix (files [I]); swatcheValues. add (I, values [I]) ;}} catch (Exception e) {System. out. println (files [I] + "can not be parsed"); e. printStackTrace () ;}} public ImageParser () {super (); if (swatches = null | swatcheValues = nul L) {System. out. println ("You have not loaded the sample. Please load the sample first! ") ;}}/*** Resolution Image value ** @ param parseFilePath * indicates the image path * @ return returns the String * @ throws Exception */public String parseValue (String parseFilePath) throws Exception {StringBuffer result = new StringBuffer (); targetColors = imageToMatrix (parseFilePath); // printMatrix (targetColors); int height = targetColors. length; int targetWidth = targetColors [0]. length; int width = 0; Iterator it = swatches. iterator (); While (it. hasNext () {byte [] [] bytes = (byte [] []) it. next (); int templen = bytes [0]. length; if (templen> width) width = templen;} // System. out. println ("MaxWidth =" + width); // System. out. println ("MaxHeight =" + height); int xTag = 0; while (xTag + width) <targetWidth) {cout: {Iterator itx = swatches. iterator (); int I = 0; while (itx. hasNext () {byte [] [] bytes = (byte [] []) itx. next (); Byte [] [] temp = splitMatrix (targetColors, xTag, 0, width, height); // System. out. println (I ++); if (isMatrixInBigMatrix (bytes, temp) {xTag + = width; // System. out. println ("new maxtrix:"); // printMatrix (temp); result. append (swatcheValues. get (I); break cout;} I ++;} xTag ++;} return result. toString ();} // -------------------------------------------------------------- Private methods/*** judge In other matrices ** @ param source * source matrix * @ param bigMatrix * large matrix * @ return returns true */private static final boolean isMatrixInBigMatrix (byte [] [] source, byte [] [] bigMatrix) {if (source = bigMatrix) return true; if (source = null | bigMatrix = null) return false; if (source. length> bigMatrix. length) return false; try {for (int I = 0; I <source. length; I ++) {if (source [I]. length> bigMatr Ix [I]. length) return false;} catch (ArrayIndexOutOfBoundsException e) {return false;} int height = source. length; int width = source [0]. length; int x = 0, y = 0; int I = 0, j = 0; int count = 0; int comparecount = height * width; for (; I <bigMatrix. length-height + 1; I ++) {for (j = 0; j <bigMatrix [I]. length-width + 1; j ++) {cout: {x = 0; count = 0; for (int k = I; k 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.