首先你应该对图片进行样本采集,然后将样本进行灰度处理,也就是变成黑白两色。

然后你就可以使用该类,对目标文件进行分析。具体怎么实现我觉得这个类非常清楚,就是将样本从左都有这么横向移动,匹配出一个合适的就将坐标调整到下个位置。

此程序已是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; i < files.length; i++){
            files[i] = "D:/workspace/SZXClientAPP/res/" + i + ".jpg";
            values[i] = String.valueOf(i);
        }
        
        ImageParser parse = new ImageParser(files, values);
        long startime = System.currentTimeMillis();
        try {
            // 解析图片
            System.out.println(parse.parseValue("D:/workspace/SZXClientAPP/res/ValidateNum"));
            long sincetime = System.currentTimeMillis();
            System.out.println("所花时间 = " + (sincetime - startime));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // ------------------------------------------------------------ Constructors

    /**
     * 载入所有样本路径与其样本对应的数值
     * 
     * @param files
     */
    public ImageParser(String[] files, String[] values) {
        // 只允许样本创建一次即可
        if (swatches == null && swatcheValues == null) {
            int fileslength = files.length;
            int valueslength = values.length;
            if(fileslength != valueslength){
                System.out.println("样本文件与样本数值不匹配!请重新设置!");
                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 == null) {
            System.out.println("您未载入样本,请先载入样本!");
        }
    }

    /**
     * 解析图片的值
     * 
     * @param parseFilePath
     *            给出图片路径
     * @return 返回字符串
     * @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

    /**
     * 判断一个矩阵是否在另外的矩阵中
     * 
     * @param source
     *            源矩阵
     * @param bigMatrix
     *            大的矩阵
     * @return 如果存在就返回 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 > 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;
    }

    /**
     * 切割矩阵
     * 
     * @param source
     *            源矩阵
     * @param x
     *            X坐标
     * @param y
     *            Y坐标
     * @param width
     *            矩阵的宽度
     * @param height
     *            矩阵的高度
     * @return 切割后的矩阵
     */
    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;
    }

    /**
     * 图片转换成矩阵数组
     * 
     * @param filePath
     *            文件路径
     * @return 返回矩阵
     * @throws Exception
     *             可能会抛出异常
     */
    private byte[][] imageToMatrix(String filePath) throws Exception {
        // 读入文件
        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);
                // 像素进行灰度处理
                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 igreen = 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 + igreen + iblank;

                // if (al > 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 < 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();
        }
    }
}
  

JavaSE图像验证码简单识别程序相关推荐

  1. java 验证码图片识别_JavaSE图像验证码简单识别程序详解

    本文为大家分享了JavaSE图像验证码简单识别程序,供大家参考,具体内容如下 首先你应该对图片进行样本采集,然后将样本进行灰度处理,也就是变成黑白两色. 然后你就可以使用该类,对目标文件进行分析.具体 ...

  2. 图像验证码识别(三)——基本流程讨论

    图像验证码的识别很类似OCR,不过验证码的功能就是防止机器人暴力破解,因此相比于OCR,图片上的干扰因素要多的多.因此如果直接读取图片的特征值进行训练,这样正确率会非常低. 常见的验证码干扰有很多种 ...

  3. python图像验证码识别_python 简单图像识别--验证码

    python  简单图像识别--验证码 记录下,准备工作安装过程很是麻烦. 首先库:pytesseract,image,tesseract,PIL windows安装PIL,直接exe进行安装更方便( ...

  4. 图像验证码识别(七)——字符分割

    2019独角兽企业重金招聘Python工程师标准>>> 前面经过各种去除噪点.干扰线,验证码图片现在已经只有两个部分,如果pixel为白就是背景,如果pixel为黑就为字符.正如前面 ...

  5. 图片验证码识别程序全面分析

    我们在登陆账号的时候,发现有图片验证码需要识别,我们在注册账号的时候,也会有图片验证码需要识别.很多"程序猿"可能也在找如何进行图片验证码识别的编程.图片验证码常有,但是关于如何快 ...

  6. matlab实验之简单识别手写0-9数字程序

    实验目的   能够用matlab设计一个程序,能够简单识别0-9等阿拉伯数字   或者识别abcd等字母 实验原理 根据手写图片在二通道里的每个像素点以二进制表示,可以设计一个函数,得到每一个手写样本 ...

  7. python打开是什么颜色-python实现简单颜色识别程序

    本文实例为大家分享了python实现简单颜色识别程序的具体代码,供大家参考,具体内容如下 import numpy as np import cv2 font= cv2.FONT_HERSHEY_SI ...

  8. 程序员用「美貌」突破二维图像的人脸识别

    GitChat 作者:于航 原文: 如何利用"女装术"突破基于二维图像的人脸识别 关注微信公众号:「GitChat 技术杂谈」 一本正经的讲技术 [不要错过文末彩蛋] 首先声明,这 ...

  9. python实现对简单的运算型验证码的识别【不使用OpenCV】

    最近在写我们学校的教务系统的手机版,在前端用户执行绑定操作后,服务器将执行登录,但在登录过程中,教务系统中有个运算型的验证码,大致是这个样子的: 下面我们开始实现这个验证码的识别. 1.图片读取 从网 ...

最新文章

  1. 1.解决python中导入包不成功的问题(出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url问题)
  2. springboot里面logback使用
  3. 31天重构学习笔记3. 提升方法
  4. python django django-debug-toolbar 加载缓慢,不能使用。
  5. java共享锁排它锁_java 实现共享锁和排它锁
  6. LeetCode(1029)——两地调度(JavaScript)
  7. 零基础带你学习MySQL—分组统计(十二)
  8. 模糊综合评价模型(下:多级)
  9. 图像小波去噪matlab程序,小波去噪程序(用matlab实现)
  10. 大数据之路:阿里巴巴大数据实践(数据模型篇)
  11. Python中的协议有什么作用?
  12. php 双竖线,范数介绍,数字两边双竖线
  13. python-shutil对文件的移动,重命名,和删除,压缩
  14. [OHIF-Viewers]医疗数字阅片-医学影像-Cornerstone Tools
  15. Android 获得手机屏幕大小
  16. 【第59题】输入、输出系列7-压缩文件相关技术2
  17. 安卓app开发工具_最新app制作软件汇总:从零开始教你完成app开发
  18. MATLAB数学建模(四):机器学习
  19. [转]什么样的女人才是老婆
  20. 站群目前来说依然在站长圈非常的流行

热门文章

  1. html圆形图片怎么加白边框,SwiftUI圆形图片制作示例_如何设置图片为圆形并加边框阴影效果...
  2. Chrome 75不支持保存成mhtml的解决方法
  3. 希望Revit2014或更高版提供什么API?(调查于2012年6月1日截止)
  4. 卓越管理者的五项习惯和十大兵法
  5. 安全研发必备的安全知识
  6. Java-JavaWeb—(2)HTML+CSS+Nginx
  7. 重庆邮电大学计算机科学与技术调剂,重庆邮电大学2019年接收硕士生调剂信息公告...
  8. 《网络信息安全》作业题和考试复习题
  9. 解决supervisor报错:entered FATAL state, too many start retries too quickly
  10. 应用笔记AN1078一阶数字低通滤波器推导和相位延迟计算