This article shares a simple JavaSE image verification code recognition program for your reference. The specific content is as follows
First, you should sample the picture and then grayscale the sample, that is, turn it into black and white.
Then you can use this class to analyze the object 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 if it matches a suitable one.
This program was written more than 3 years ago, but later it was not written in depth. Image recognition is a very deep field, which requires deep mathematical skills and thinking ability. The Java program is not efficient and cannot recognize deformed or stretched pictures. However, it was enough in that era. If you have better open source image recognition code, please be sure to write to communicate:)
/** * Image parsing engine, suitable for analysis of website verification codes. * First, the sample must be loaded, the parser will scan horizontally from left to right, and will automatically record if it is found in the sample. * Of course, this program is not suitable for samples and is not unique, that is, the picture to be identified is scaled or coordinates are changed or deformed. This program cannot perform such identification. * If the color changes in the picture very much, this program may have problems. Of course, you can choose a standard value as the standard for converting it to a 0,1 matrix. * * Sample production: Please convert the sample to grayscale mode. It is best to contain only two colors. Of course, if you don’t convert, I will also help you 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 { // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ swatcheValues = null; // Matrixed private byte[][] targetColors; // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ values[i] = String.valueOf(i); } ImageParser parse = new ImageParser(files, values); long start = 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 - start)); } catch (Exception e) { e.printStackTrace(); } } // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("The sample file does not match the sample value! Please reset it!"); return; } switches = new ArrayList(fileslength); swatcheValues = new ArrayList(valueslength); int i = 0; try { for (; i < files.length; i++) { switches.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 == null) { System.out.println("You have not loaded the sample, please load the sample first!"); } } /** * Parsing the value of the image* * @param parseFilePath * Give the image path* @return Return 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 = switches.iterator(); while (it.hasNext()) { byte[][] bytes = (byte[][]) it.next(); int temple = 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 = switches.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(); } // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ @param source * Source matrix * @param bigMatrix * Large matrix * @return Return true if it exists */ 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 > bigMatrix[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 < height + i; k++) { y = 0; for (int l = j; l < width + j; l++) { // System.out.println("bytes[" + x + "][" + y + "]" // + " = " + source[x][y] + ", " + "other[" // + k + "][" + l + "] = " + bigMatrix[k][l]); if ((source[x][y] & bigMatrix[k][l]) == source[x][y]) { count++; } else break cout; y++; } x++; } // System.out.println("count = " + count); if (count == comparecount) return true; } } } return false; } /** * Cut matrix* * @param source * Source matrix* @param x * X coordinates* @param y * Y coordinates* @param width * Width of the matrix* @param height * Height of the matrix* @return Cut matrix*/ private static final byte[][] splitMatrix(byte[][]] source, int x, int y, int width, int height) { byte[][] resultbytes = new byte[height][width]; for (int i = y, k = 0; i < height + y; i++, k++) { for (int j = x, l = 0; j < width + x; j++, l++) { resultbytes[k][l] = source[i][j]; // System.out.println("source[" + i + "][" + j + "]" + " = " + // source[i][j] + ", " + "resultbytes[" // + k + "][" + l + "] = " + resultbytes[k][l]); } } return resultbytes; } /** * Convert the image into a matrix array* * @param filePath * File path* @return Return the matrix* @throws Exception * An exception may be thrown*/ private byte[][] imageToMatrix(String filePath) throws Exception { // Read in the file Image image = ImageIO.read(new File(filePath)); int w = image.getWidth(null); int h = image.getHeight(null); BufferedImage src = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); src.getGraphics().drawImage(image, 0, 0, null); byte[][] colors = new byte[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int rgb = src.getRGB(j, i); // Grayscale processing of pixels String sRed = Integer.toHexString(rgb).substring(2, 4); String sGreen = Integer.toHexString(rgb).substring(4, 6); String sBlank = Integer.toHexString(rgb).substring(6, 8); long ired = Math.round((Integer.parseInt(sRed, 16) * 0.3 + 0.5d)); long green = Math.round((Integer.parseInt(sGreen, 16) * 0.59 + 0.5d)); long iblank = Math.round((Integer.parseInt(sBlank, 16) * 0.11 + 0.5d)); long al = ired + green + iblank; // if (al > 127) // System.out.print(" " + " "); // else // System.out.print(" " + "1"); // System.out.print(" " + (tempint > = maxint ? 0 : 1)); // System.out.println("tempInt = " + tempint); /* Convert the image to 0,1 */ // The value here can be modified to the value you need to judge in the future colors[i][j] = (byte) (al > 127 ? 0 : 1); } // System.out.println(); } return colors; } /** * Print matrix* * @param matrix */ private static final void printMatrix(byte[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (matrix[i][j] == 0) System.out.print(" "); else System.out.print(" 1"); } System.out.println(); } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.