java验证码识别--1

来源:互联网 发布:python网页架构 编辑:程序博客网 时间:2024/04/26 20:16

来自http://blog.csdn.net/problc/article/details/5794460

(本文仅用于学习研究图像匹配识别原理,不得用于其他用途。)

完整eclipse工程http://download.csdn.net/detail/problc/3829004

最近看了看验证码的识别,先从最简单的做起吧(固定大小,固定位置,固定字体)

 

验证码识别基本分四步,图片预处理,分割,训练,识别

 

看一个最简单验证码

这是一个德克萨斯扑克的注册页面的验证码

 

1。图像的预处理

这种直接根据亮度设个阈值处理就可以了

[java] view plaincopyprint?
  1. public static int isWhite(int colorInt) {  
  2.         Color color = new Color(colorInt);  
  3.         if (color.getRed() + color.getGreen() + color.getBlue() > 100) {  
  4.             return 1;  
  5.         }  
  6.         return 0;  
  7.     }  
  8.   
  9.     public static BufferedImage removeBackgroud(String picFile)  
  10.             throws Exception {  
  11.         BufferedImage img = ImageIO.read(new File(picFile));  
  12.         int width = img.getWidth();  
  13.         int height = img.getHeight();  
  14.         for (int x = 0; x < width; ++x) {  
  15.             for (int y = 0; y < height; ++y) {  
  16.                 if (isWhite(img.getRGB(x, y)) == 1) {  
  17.                     img.setRGB(x, y, Color.WHITE.getRGB());  
  18.                 } else {  
  19.                     img.setRGB(x, y, Color.BLACK.getRGB());  
  20.                 }  
  21.             }  
  22.         }  
  23.         return img;  
  24.     }  

 

处理完图片效果为

图像基本分得比较清楚,图片分割也比较容易

 

2。分割

这个验证码居然是固定位置的,分割相当简单,直接截取相应位置就可以了

[java] view plaincopyprint?
  1. public static List<BufferedImage> splitImage(BufferedImage img)  
  2.             throws Exception {  
  3.         List<BufferedImage> subImgs = new ArrayList<BufferedImage>();  
  4.         subImgs.add(img.getSubimage(106810));  
  5.         subImgs.add(img.getSubimage(196810));  
  6.         subImgs.add(img.getSubimage(286810));  
  7.         subImgs.add(img.getSubimage(376810));  
  8.         return subImgs;  
  9.     }  

3。训练

直接拿几张图片,包含0-9,每个数字一个样本就可以了,将文件名对应相应的数字

4。识别

因为是固定大小,固定位置,识别也很简单。

直接拿分割的图片跟这个十个图片一个像素一个像素的比,相同的点最多的就是结果。比如如果跟5.jpg最相似,那么识别的结果就是5。

下面是识别结果,很容易达到100%

 

 

完整代码(csdn连个附件都不支持):

[java] view plaincopyprint?
  1. import java.awt.Color;  
  2. import java.awt.image.BufferedImage;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.HashMap;  
  9. import java.util.List;  
  10. import java.util.Map;  
  11.   
  12. import javax.imageio.ImageIO;  
  13.   
  14. import org.apache.commons.httpclient.HttpClient;  
  15. import org.apache.commons.httpclient.HttpStatus;  
  16. import org.apache.commons.httpclient.methods.GetMethod;  
  17. import org.apache.commons.io.IOUtils;  
  18.   
  19. public class ImagePreProcess {  
  20.   
  21.     public static int isWhite(int colorInt) {  
  22.         Color color = new Color(colorInt);  
  23.         if (color.getRed() + color.getGreen() + color.getBlue() > 100) {  
  24.             return 1;  
  25.         }  
  26.         return 0;  
  27.     }  
  28.   
  29.     public static int isBlack(int colorInt) {  
  30.         Color color = new Color(colorInt);  
  31.         if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {  
  32.             return 1;  
  33.         }  
  34.         return 0;  
  35.     }  
  36.   
  37.     public static BufferedImage removeBackgroud(String picFile)  
  38.             throws Exception {  
  39.         BufferedImage img = ImageIO.read(new File(picFile));  
  40.         int width = img.getWidth();  
  41.         int height = img.getHeight();  
  42.         for (int x = 0; x < width; ++x) {  
  43.             for (int y = 0; y < height; ++y) {  
  44.                 if (isWhite(img.getRGB(x, y)) == 1) {  
  45.                     img.setRGB(x, y, Color.WHITE.getRGB());  
  46.                 } else {  
  47.                     img.setRGB(x, y, Color.BLACK.getRGB());  
  48.                 }  
  49.             }  
  50.         }  
  51.         return img;  
  52.     }  
  53.   
  54.     public static List<BufferedImage> splitImage(BufferedImage img)  
  55.             throws Exception {  
  56.         List<BufferedImage> subImgs = new ArrayList<BufferedImage>();  
  57.         subImgs.add(img.getSubimage(106810));  
  58.         subImgs.add(img.getSubimage(196810));  
  59.         subImgs.add(img.getSubimage(286810));  
  60.         subImgs.add(img.getSubimage(376810));  
  61.         return subImgs;  
  62.     }  
  63.   
  64.     public static Map<BufferedImage, String> loadTrainData() throws Exception {  
  65.         Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();  
  66.         File dir = new File("train");  
  67.         File[] files = dir.listFiles();  
  68.         for (File file : files) {  
  69.             map.put(ImageIO.read(file), file.getName().charAt(0) + "");  
  70.         }  
  71.         return map;  
  72.     }  
  73.   
  74.     public static String getSingleCharOcr(BufferedImage img,  
  75.             Map<BufferedImage, String> map) {  
  76.         String result = "";  
  77.         int width = img.getWidth();  
  78.         int height = img.getHeight();  
  79.         int min = width * height;  
  80.         for (BufferedImage bi : map.keySet()) {  
  81.             int count = 0;  
  82.             Label1: for (int x = 0; x < width; ++x) {  
  83.                 for (int y = 0; y < height; ++y) {  
  84.                     if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) {  
  85.                         count++;  
  86.                         if (count >= min)  
  87.                             break Label1;  
  88.                     }  
  89.                 }  
  90.             }  
  91.             if (count < min) {  
  92.                 min = count;  
  93.                 result = map.get(bi);  
  94.             }  
  95.         }  
  96.         return result;  
  97.     }  
  98.   
  99.     public static String getAllOcr(String file) throws Exception {  
  100.         BufferedImage img = removeBackgroud(file);  
  101.         List<BufferedImage> listImg = splitImage(img);  
  102.         Map<BufferedImage, String> map = loadTrainData();  
  103.         String result = "";  
  104.         for (BufferedImage bi : listImg) {  
  105.             result += getSingleCharOcr(bi, map);  
  106.         }  
  107.         ImageIO.write(img, "JPG"new File("result//"+result+".jpg"));  
  108.         return result;  
  109.     }  
  110.   
  111.     public static void downloadImage() {  
  112.         HttpClient httpClient = new HttpClient();  
  113.         GetMethod getMethod = new GetMethod(  
  114.                 "http://www.puke888.com/authimg.php");  
  115.         for (int i = 0; i < 30; i++) {  
  116.             try {  
  117.                 // 执行getMethod  
  118.                 int statusCode = httpClient.executeMethod(getMethod);  
  119.                 if (statusCode != HttpStatus.SC_OK) {  
  120.                     System.err.println("Method failed: "  
  121.                             + getMethod.getStatusLine());  
  122.                 }  
  123.                 // 读取内容  
  124.                 String picName = "img//" + i + ".jpg";  
  125.                 InputStream inputStream = getMethod.getResponseBodyAsStream();  
  126.                 OutputStream outStream = new FileOutputStream(picName);  
  127.                 IOUtils.copy(inputStream, outStream);  
  128.                 outStream.close();  
  129.                 System.out.println("OK!");  
  130.             } catch (Exception e) {  
  131.                 e.printStackTrace();  
  132.             } finally {  
  133.                 // 释放连接  
  134.                 getMethod.releaseConnection();  
  135.             }  
  136.         }  
  137.     }  
  138.   
  139.     /** 
  140.      * @param args 
  141.      * @throws Exception 
  142.      */  
  143.     public static void main(String[] args) throws Exception {  
  144.         for (int i = 0; i < 30; ++i) {  
  145.             String text = getAllOcr("img//" + i + ".jpg");  
  146.             System.out.println(i + ".jpg = " + text);  
  147.         }  
  148.     }  
  149. }  

原创粉丝点击