本文为大家分享了JavaSE图像验证码简单识别程序,供大家参考,具体内容如下
首先你应该对图片进行样本采集,然后将样本进行灰度处理,也就是变成黑白两色。
然后你就可以使用该类,对目标文件进行分析。具体怎么实现我觉得这个类非常清楚,就是将样本从左都有这么横向移动,匹配出一个合适的就将坐标调整到下个位置。
此程序已是3年多前写的,后来没有在深入写下去,图像识别一个很深的领域,得需要很深的数学功底跟思维能力,这个java的程序效率不高,也不能识别变形的或者拉伸的图片,但在那个年代,已经足够用了,大家如果有更好的开源的图像识别代码,请务必来信交流:)
/** * 图片解析引擎,适合做网站验证码的分析。 * 首先必须载入样品,解析器将从左到右横向扫描,发现于样本的就自动记录。 * 当然本程序不适合样本不是唯一的,也就是说要识别的图片被缩放或者坐标变动和变形本程序无法进行这样的识别。 * 如果图片中的颜色变化非常大,此程序可能会有问题,当然了你可以选择一个标准的值做为转换成0,1矩阵的标准。 * * 样本的制作:请将样本转换成灰度模式,只含有两色最好,当然了不转换我也帮你转换了。 * */ 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 { // ------------------------------------------------------------ Private Data // 样本的矩阵 private static List swatches = null; // 样本的值 private static List swatcheValues = null; // 图片文件的矩阵化 private byte[][] targetColors; // ------------------------------------------------------------ Test main method public static void main(String[] args) { // 加入样本与其样本对应的数值 String[] files = new String[10]; String[] values = new String[10]; for (int i = 0; iwidth) width = templen; } // System.out.println("MaxWidth = " + width); // System.out.println("MaxHeight = " + height); int xTag = 0; while ((xTag + width) bigMatrix.length) return false; try { for (int i = 0; i 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 127) // System.out.print(" " + " "); // else // System.out.print(" " + "1"); // System.out.print(" " + (tempint > = maxint ? 0 : 1)); // System.out.println("tempInt = " + tempint); /* 将图像转换成0,1 */ // 此处的值可以将来修改成你所需要判断的值 colors[i][j] = (byte) (al > 127 ? 0 : 1); } // System.out.println(); } return colors; } /** * 打印矩阵 * * @param matrix */ private static final void printMatrix(byte[][] matrix) { for (int i = 0; i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。