标签: javaexceptionstringfilebi

http://blog.csdn.net/problc/article/details/5846614

Java验证码识别--1

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

java验证码识别--2

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

java验证码识别--3

http://blog.csdn.net/problc/article/details/5800093

java验证码识别--4

http://blog.csdn.net/problc/article/details/5846614

java验证码识别--5

http://blog.csdn.net/problc/article/details/5983276


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

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

验证码识别如果识别率都是100%,那验证码也就没存在的必要了。

其实很多验证码能达到10%的识别率就不错了。

下面来一个稍微复杂一点的,识别率85%左右。

看验证码

挑一张来看

放大看,我们会发现干扰线是纯黑色的,因此去干扰线的方法就有了

对点color[i][j],如果color[i+1][j],color[i-1][j],color[i][j+1],color[i][j-1]都是纯黑或者纯白色的,就认为color[i][j]是干扰,将color[i][j]置为白色。

处理之后

这样就简单了,分割也简单。

 

识别结果

 

啥也不说了,贴代码

[java]  view plain  copy
 print ?
  1. public class ImagePreProcess4 {  
  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() <= 300) {  
  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() > 300) {  
  17.             return 1;  
  18.         }  
  19.         return 0;  
  20.     }  
  21.   
  22.     public static int getColorBright(int colorInt) {  
  23.         Color color = new Color(colorInt);  
  24.         return color.getRed() + color.getGreen() + color.getBlue();  
  25.   
  26.     }  
  27.   
  28.     public static int isBlackOrWhite(int colorInt) {  
  29.         if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730) {  
  30.             return 1;  
  31.         }  
  32.         return 0;  
  33.     }  
  34.   
  35.     public static BufferedImage removeBackgroud(String picFile)  
  36.             throws Exception {  
  37.         BufferedImage img = ImageIO.read(new File(picFile));  
  38.         int width = img.getWidth();  
  39.         int height = img.getHeight();  
  40.         for (int x = 1; x < width - 1; ++x) {  
  41.             for (int y = 1; y < height - 1; ++y) {  
  42.                 if (getColorBright(img.getRGB(x, y)) < 100) {  
  43.                     if (isBlackOrWhite(img.getRGB(x - 1, y))  
  44.                             + isBlackOrWhite(img.getRGB(x + 1, y))  
  45.                             + isBlackOrWhite(img.getRGB(x, y - 1))  
  46.                             + isBlackOrWhite(img.getRGB(x, y + 1)) == 4) {  
  47.                         img.setRGB(x, y, Color.WHITE.getRGB());  
  48.                     }  
  49.                 }  
  50.             }  
  51.         }  
  52.         for (int x = 1; x < width - 1; ++x) {  
  53.             for (int y = 1; y < height - 1; ++y) {  
  54.                 if (getColorBright(img.getRGB(x, y)) < 100) {  
  55.                     if (isBlackOrWhite(img.getRGB(x - 1, y))  
  56.                             + isBlackOrWhite(img.getRGB(x + 1, y))  
  57.                             + isBlackOrWhite(img.getRGB(x, y - 1))  
  58.                             + isBlackOrWhite(img.getRGB(x, y + 1)) == 4) {  
  59.                         img.setRGB(x, y, Color.WHITE.getRGB());  
  60.                     }  
  61.                 }  
  62.             }  
  63.         }  
  64.         img = img.getSubimage(11, img.getWidth() - 2, img.getHeight() - 2);  
  65.         return img;  
  66.     }  
  67.   
  68.     public static BufferedImage removeBlank(BufferedImage img) throws Exception {  
  69.         int width = img.getWidth();  
  70.         int height = img.getHeight();  
  71.         int start = 0;  
  72.         int end = 0;  
  73.         Label1: for (int y = 0; y < height; ++y) {  
  74.             for (int x = 0; x < width; ++x) {  
  75.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  76.                     start = y;  
  77.                     break Label1;  
  78.                 }  
  79.             }  
  80.         }  
  81.         Label2: for (int y = height - 1; y >= 0; --y) {  
  82.             for (int x = 0; x < width; ++x) {  
  83.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  84.                     end = y;  
  85.                     break Label2;  
  86.                 }  
  87.             }  
  88.         }  
  89.         return img.getSubimage(0, start, width, end - start + 1);  
  90.     }  
  91.   
  92.     public static List<BufferedImage> splitImage(BufferedImage img)  
  93.             throws Exception {  
  94.         List<BufferedImage> subImgs = new ArrayList<BufferedImage>();  
  95.         int width = img.getWidth();  
  96.         int height = img.getHeight();  
  97.         List<Integer> weightlist = new ArrayList<Integer>();  
  98.         for (int x = 0; x < width; ++x) {  
  99.             int count = 0;  
  100.             for (int y = 0; y < height; ++y) {  
  101.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  102.                     count++;  
  103.                 }  
  104.             }  
  105.             weightlist.add(count);  
  106.         }  
  107.         for (int i = 0; i < weightlist.size(); i++) {  
  108.             int length = 0;  
  109.             while (i < weightlist.size() && weightlist.get(i) > 0) {  
  110.                 i++;  
  111.                 length++;  
  112.             }  
  113.             if (length > 18) {  
  114.                 subImgs.add(removeBlank(img.getSubimage(i - length, 0,  
  115.                         length / 2, height)));  
  116.                 subImgs.add(removeBlank(img.getSubimage(i - length / 20,  
  117.                         length / 2, height)));  
  118.             } else if (length > 5) {  
  119.                 subImgs.add(removeBlank(img.getSubimage(i - length, 0, length,  
  120.                         height)));  
  121.             }  
  122.         }  
  123.   
  124.         return subImgs;  
  125.     }  
  126.   
  127.     public static Map<BufferedImage, String> loadTrainData() throws Exception {  
  128.         if (trainMap == null) {  
  129.             Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();  
  130.             File dir = new File("train4");  
  131.             File[] files = dir.listFiles();  
  132.             for (File file : files) {  
  133.                 map.put(ImageIO.read(file), file.getName().charAt(0) + "");  
  134.             }  
  135.             trainMap = map;  
  136.         }  
  137.         return trainMap;  
  138.     }  
  139.   
  140.     public static int getDistance(BufferedImage img, BufferedImage sample) {  
  141.         int width = img.getWidth();  
  142.         int height = img.getHeight();  
  143.         int count = 0;  
  144.         int widthmin = width < sample.getWidth() ? width : sample.getWidth();  
  145.         int heightmin = height < sample.getHeight() ? height : sample  
  146.                 .getHeight();  
  147.         for (int x = 0; x < widthmin; ++x) {  
  148.             for (int y = 0; y < heightmin; ++y) {  
  149.                 if (isWhite(img.getRGB(x, y)) != isWhite(sample.getRGB(x, y))) {  
  150.                     count++;  
  151.                 }  
  152.             }  
  153.         }  
  154.         return count;  
  155.     }  
  156.   
  157.     public static boolean isNotEight(BufferedImage img) {  
  158.         int width = img.getWidth();  
  159.         int height = img.getHeight();  
  160.         int minCount = width;  
  161.         for (int y = height / 2 - 2; y < height / 2 + 2; ++y) {  
  162.             int count = 0;  
  163.             for (int x = 0; x < width / 2 + 2; ++x) {  
  164.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  165.                     count++;  
  166.                 }  
  167.             }  
  168.             minCount = Math.min(count, minCount);  
  169.         }  
  170.         return minCount < 2;  
  171.     }  
  172.   
  173.     public static boolean isNotThree(BufferedImage img) {  
  174.         int width = img.getWidth();  
  175.         int height = img.getHeight();  
  176.         int minCount = width;  
  177.         for (int y = height / 2 - 3; y < height / 2 + 3; ++y) {  
  178.             int count = 0;  
  179.             for (int x = 0; x < width / 2 + 1; ++x) {  
  180.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  181.                     count++;  
  182.                 }  
  183.             }  
  184.             minCount = Math.min(count, minCount);  
  185.         }  
  186.         return minCount > 0;  
  187.     }  
  188.   
  189.     public static boolean isNotFive(BufferedImage img) {  
  190.         int width = img.getWidth();  
  191.         int height = img.getHeight();  
  192.         int minCount = width;  
  193.         for (int y = 0; y < height / 3; ++y) {  
  194.             int count = 0;  
  195.             for (int x = width * 2 / 3; x < width; ++x) {  
  196.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  197.                     count++;  
  198.                 }  
  199.             }  
  200.             minCount = Math.min(count, minCount);  
  201.         }  
  202.         return minCount > 0;  
  203.     }  
  204.   
  205.     public static String getSingleCharOcr(BufferedImage img,  
  206.             Map<BufferedImage, String> map) throws Exception {  
  207.         String result = "#";  
  208.         int width = img.getWidth();  
  209.         int height = img.getHeight();  
  210.         int min = width * height;  
  211.         boolean bNotEight = isNotEight(img);  
  212.         boolean bNotThree = isNotThree(img);  
  213.         boolean bNotFive = isNotFive(img);  
  214.         for (BufferedImage bi : map.keySet()) {  
  215.             if (bNotThree && map.get(bi).startsWith("3"))  
  216.                 continue;  
  217.             if (bNotEight && map.get(bi).startsWith("8"))  
  218.                 continue;  
  219.             if (bNotFive && map.get(bi).startsWith("5"))  
  220.                 continue;  
  221.             double count1 = getBlackCount(img);  
  222.             double count2 = getBlackCount(bi);  
  223.             if (Math.abs(count1 - count2) / Math.max(count1, count2) > 0.25)  
  224.                 continue;  
  225.             int count = 0;  
  226.             if (width < bi.getWidth() && height < bi.getHeight()) {  
  227.                 for (int m = 0; m <= bi.getWidth() - width; m++) {  
  228.                     for (int n = 0; n <= bi.getHeight() - height; n++) {  
  229.                         Label1: for (int x = m; x < m + width; ++x) {  
  230.                             for (int y = n; y < n + height; ++y) {  
  231.                                 if (isWhite(img.getRGB(x - m, y - n)) != isWhite(bi  
  232.                                         .getRGB(x, y))) {  
  233.                                     count++;  
  234.                                     if (count >= min)  
  235.                                         break Label1;  
  236.                                 }  
  237.                             }  
  238.                         }  
  239.                     }  
  240.                 }  
  241.             } else {  
  242.                 int widthmin = width < bi.getWidth() ? width : bi.getWidth();  
  243.                 int heightmin = height < bi.getHeight() ? height : bi  
  244.                         .getHeight();  
  245.                 Label1: for (int x = 0; x < widthmin; ++x) {  
  246.                     for (int y = 0; y < heightmin; ++y) {  
  247.                         if (isWhite(img.getRGB(x, y)) != isWhite(bi  
  248.                                 .getRGB(x, y))) {  
  249.                             count++;  
  250.                             if (count >= min)  
  251.                                 break Label1;  
  252.                         }  
  253.                     }  
  254.                 }  
  255.             }  
  256.             if (count < min) {  
  257.                 min = count;  
  258.                 result = map.get(bi);  
  259.             }  
  260.         }  
  261.         return result;  
  262.     }  
  263.   
  264.     public static String getAllOcr(String file) throws Exception {  
  265.         BufferedImage img = removeBackgroud(file);  
  266.         List<BufferedImage> listImg = splitImage(img);  
  267.         Map<BufferedImage, String> map = loadTrainData();  
  268.         String result = "";  
  269.         for (BufferedImage bi : listImg) {  
  270.             result += getSingleCharOcr(bi, map);  
  271.         }  
  272.         System.out.println(result);  
  273.         ImageIO.write(img, "JPG"new File("result4//" + result + ".jpg"));  
  274.         return result;  
  275.     }  
  276.   
  277.     public static int getBlackCount(BufferedImage img) {  
  278.         int width = img.getWidth();  
  279.         int height = img.getHeight();  
  280.         int count = 0;  
  281.         for (int x = 0; x < width; ++x) {  
  282.             for (int y = 0; y < height; ++y) {  
  283.                 if (isBlack(img.getRGB(x, y)) == 1) {  
  284.                     count++;  
  285.                 }  
  286.             }  
  287.         }  
  288.         return count;  
  289.     }  
  290.   
  291.     public static void downloadImage() {  
  292.         HttpClient httpClient = new HttpClient();  
  293.         GetMethod getMethod = new GetMethod(  
  294.                 "http://reg.keepc.com/getcode/getCode.<a href="http://lib.csdn.net/base/php" class='replace_word' title="PHP知识库" target='_blank' style='color:#df3434; font-weight:bold;'>PHP</a>");  
  295.         for (int i = 0; i < 30; i++) {  
  296.   
  297.             try {  
  298.                 // 执行getMethod  
  299.                 int statusCode = httpClient.executeMethod(getMethod);  
  300.                 if (statusCode != HttpStatus.SC_OK) {  
  301.                     System.err.println("Method failed: "  
  302.                             + getMethod.getStatusLine());  
  303.                 }  
  304.                 // 读取内容  
  305.                 String picName = "img4//" + i + ".jpg";  
  306.                 InputStream inputStream = getMethod.getResponseBodyAsStream();  
  307.                 OutputStream outStream = new FileOutputStream(picName);  
  308.                 IOUtils.copy(inputStream, outStream);  
  309.                 outStream.close();  
  310.                 System.out.println(i + "OK!");  
  311.             } catch (Exception e) {  
  312.                 e.printStackTrace();  
  313.             } finally {  
  314.                 // 释放连接  
  315.                 getMethod.releaseConnection();  
  316.             }  
  317.         }  
  318.     }  
  319.   
  320.     public static void trainData() throws Exception {  
  321.         File dir = new File("temp4");  
  322.         File[] files = dir.listFiles();  
  323.         for (File file : files) {  
  324.             BufferedImage img = removeBackgroud("temp4//" + file.getName());  
  325.             List<BufferedImage> listImg = splitImage(img);  
  326.             if (listImg.size() == 4) {  
  327.                 for (int j = 0; j < listImg.size(); ++j) {  
  328.                     ImageIO.write(listImg.get(j), "JPG"new File("train4//"  
  329.                             + file.getName().charAt(j) + "-" + (index++)  
  330.                             + ".jpg"));  
  331.                 }  
  332.             }  
  333.         }  
  334.     }  
  335.   
  336.     /** 
  337.      * @param args 
  338.      * @throws Exception 
  339.      */  
  340.     public static void main(String[] args) throws Exception {  
  341.         // downloadImage();  
  342.         // trainData();  
  343.         for (int i = 0; i < 30; ++i) {  
  344.             String text = getAllOcr("img4//" + i + ".jpg");  
  345.             System.out.println(i + ".jpg = " + text);  
  346.         }  
  347.     }  
  348. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值