java验证码识别--2

来源:互联网 发布:webpack 压缩混淆js 编辑:程序博客网 时间:2024/03/28 17:39

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

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

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

换一个字体固定,大小固定,位置不固定的验证码

 

还是四步。

1。图像预处理

     这验证码还是很厚道的,都没有任何干扰。不用处理

2。分割

     先纵向扫描,很容易分成四部分

    

     再对每一部分横向扫描

        

3。训练就容易了

     把分割的结果对应存成5.jpg,9.jpg,3.jpg,a.jpg 就可以了

     

4。识别

     因为固定大小,识别跟 验证码识别--1 里面一样,像素比较就可以了。

识别结果如下,识别率100%:

 

源码:

 

[java] view plaincopyprint?
  1. public class ImagePreProcess2 {  
  2.   
  3.     private static Map<BufferedImage, String> trainMap = null;  
  4.     private static int index = 0;  
  5.   
  6.     public static int isBlack(int colorInt) {  
  7.         Color color = new Color(colorInt);  
  8.         if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {  
  9.             return 1;  
  10.         }  
  11.         return 0;  
  12.     }  
  13.   
  14.     public static int isWhite(int colorInt) {  
  15.         Color color = new Color(colorInt);  
  16.         if (color.getRed() + color.getGreen() + color.getBlue() > 100) {  
  17.             return 1;  
  18.         }  
  19.         return 0;  
  20.     }  
  21.   
  22.     public static BufferedImage removeBackgroud(String picFile)  
  23.             throws Exception {  
  24.         BufferedImage img = ImageIO.read(new File(picFile));  
  25.         return img;  
  26.     }  
  27.   
  28.     public static BufferedImage removeBlank(BufferedImage img) throws Exception {  
  29.         int width = img.getWidth();  
  30.         int height = img.getHeight();  
  31.         int start = 0;  
  32.         int end = 0;  
  33.         Label1: for (int y = 0; y < height; ++y) {  
  34.             int count = 0;  
  35.             for (int x = 0; x < width; ++x) {  
  36.                 if (isWhite(img.getRGB(x, y)) == 1) {  
  37.                     count++;  
  38.                 }  
  39.                 if (count >= 1) {  
  40.                     start = y;  
  41.                     break Label1;  
  42.                 }  
  43.             }  
  44.         }  
  45.         Label2: for (int y = height - 1; y >= 0; --y) {  
  46.             int count = 0;  
  47.             for (int x = 0; x < width; ++x) {  
  48.                 if (isWhite(img.getRGB(x, y)) == 1) {  
  49.                     count++;  
  50.                 }  
  51.                 if (count >= 1) {  
  52.                     end = y;  
  53.                     break Label2;  
  54.                 }  
  55.             }  
  56.         }  
  57.         return img.getSubimage(0, start, width, end - start + 1);  
  58.     }  
  59.   
  60.     public static List<BufferedImage> splitImage(BufferedImage img)  
  61.             throws Exception {  
  62.         List<BufferedImage> subImgs = new ArrayList<BufferedImage>();  
  63.         int width = img.getWidth();  
  64.         int height = img.getHeight();  
  65.         List<Integer> weightlist = new ArrayList<Integer>();  
  66.         for (int x = 0; x < width; ++x) {  
  67.             int count = 0;  
  68.             for (int y = 0; y < height; ++y) {  
  69.                 if (isWhite(img.getRGB(x, y)) == 1) {  
  70.                     count++;  
  71.                 }  
  72.             }  
  73.             weightlist.add(count);  
  74.         }  
  75.         for (int i = 0; i < weightlist.size();) {  
  76.             int length = 0;  
  77.             while (weightlist.get(i++) > 1) {  
  78.                 length++;  
  79.             }  
  80.             if (length > 12) {  
  81.                 subImgs.add(removeBlank(img.getSubimage(i - length - 10,  
  82.                         length / 2, height)));  
  83.                 subImgs.add(removeBlank(img.getSubimage(i - length / 2 - 10,  
  84.                         length / 2, height)));  
  85.             } else if (length > 3) {  
  86.                 subImgs.add(removeBlank(img.getSubimage(i - length - 10,  
  87.                         length, height)));  
  88.             }  
  89.         }  
  90.         return subImgs;  
  91.     }  
  92.   
  93.     public static Map<BufferedImage, String> loadTrainData() throws Exception {  
  94.         if (trainMap == null) {  
  95.             Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();  
  96.             File dir = new File("train2");  
  97.             File[] files = dir.listFiles();  
  98.             for (File file : files) {  
  99.                 map.put(ImageIO.read(file), file.getName().charAt(0) + "");  
  100.             }  
  101.             trainMap = map;  
  102.         }  
  103.         return trainMap;  
  104.     }  
  105.   
  106.     public static String getSingleCharOcr(BufferedImage img,  
  107.             Map<BufferedImage, String> map) {  
  108.         String result = "";  
  109.         int width = img.getWidth();  
  110.         int height = img.getHeight();  
  111.         int min = width * height;  
  112.         for (BufferedImage bi : map.keySet()) {  
  113.             int count = 0;  
  114.             int widthmin = width < bi.getWidth() ? width : bi.getWidth();  
  115.             int heightmin = height < bi.getHeight() ? height : bi.getHeight();  
  116.             Label1: for (int x = 0; x < widthmin; ++x) {  
  117.                 for (int y = 0; y < heightmin; ++y) {  
  118.                     if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) {  
  119.                         count++;  
  120.                         if (count >= min)  
  121.                             break Label1;  
  122.                     }  
  123.                 }  
  124.             }  
  125.             if (count < min) {  
  126.                 min = count;  
  127.                 result = map.get(bi);  
  128.             }  
  129.         }  
  130.         return result;  
  131.     }  
  132.   
  133.     public static String getAllOcr(String file) throws Exception {  
  134.         BufferedImage img = removeBackgroud(file);  
  135.         List<BufferedImage> listImg = splitImage(img);  
  136.         Map<BufferedImage, String> map = loadTrainData();  
  137.         String result = "";  
  138.         for (BufferedImage bi : listImg) {  
  139.             result += getSingleCharOcr(bi, map);  
  140.         }  
  141.         ImageIO.write(img, "JPG"new File("result2//" + result + ".jpg"));  
  142.         return result;  
  143.     }  
  144.   
  145.     public static void downloadImage() {  
  146.         HttpClient httpClient = new HttpClient();  
  147.         GetMethod getMethod = null;  
  148.         for (int i = 0; i < 30; i++) {  
  149.             getMethod = new GetMethod("http://www.pkland.net/img.php?key="  
  150.                     + (2000 + i));  
  151.             try {  
  152.                 // 执行getMethod  
  153.                 int statusCode = httpClient.executeMethod(getMethod);  
  154.                 if (statusCode != HttpStatus.SC_OK) {  
  155.                     System.err.println("Method failed: "  
  156.                             + getMethod.getStatusLine());  
  157.                 }  
  158.                 // 读取内容  
  159.                 String picName = "img2//" + i + ".jpg";  
  160.                 InputStream inputStream = getMethod.getResponseBodyAsStream();  
  161.                 OutputStream outStream = new FileOutputStream(picName);  
  162.                 IOUtils.copy(inputStream, outStream);  
  163.                 outStream.close();  
  164.                 System.out.println(i + "OK!");  
  165.             } catch (Exception e) {  
  166.                 e.printStackTrace();  
  167.             } finally {  
  168.                 // 释放连接  
  169.                 getMethod.releaseConnection();  
  170.             }  
  171.         }  
  172.     }  
  173.   
  174.     public static void trainData() throws Exception {  
  175.         File dir = new File("temp");  
  176.         File[] files = dir.listFiles();  
  177.         for (File file : files) {  
  178.             BufferedImage img = removeBackgroud("temp//" + file.getName());  
  179.             List<BufferedImage> listImg = splitImage(img);  
  180.             if (listImg.size() == 4) {  
  181.                 for (int j = 0; j < listImg.size(); ++j) {  
  182.                     ImageIO.write(listImg.get(j), "JPG"new File("train2//"  
  183.                             + file.getName().charAt(j) + "-" + (index++)  
  184.                             + ".jpg"));  
  185.                 }  
  186.             }  
  187.         }  
  188.     }  
  189.   
  190.     /** 
  191.      * @param args 
  192.      * @throws Exception 
  193.      */  
  194.     public static void main(String[] args) throws Exception {  
  195.         // downloadImage();  
  196.         for (int i = 0; i < 30; ++i) {  
  197.             String text = getAllOcr("img2//" + i + ".jpg");  
  198.             System.out.println(i + ".jpg = " + text);  
  199.         }  
  200.     }  
  201. }  


原创粉丝点击