java验证码识别--2

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

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

 

还是四步。

1。图像预处理

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

2。分割

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

   

     再对每一部分横向扫描

        

3。训练就容易了

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

    

4。识别

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

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

 

源码:

 public class ImagePreProcess2 { private static Map trainMap = null; private static int index = 0; public static int isBlack(int colorInt) { Color color = new Color(colorInt); if (color.getRed() + color.getGreen() + color.getBlue() <= 100) { return 1; } return 0; } public static int isWhite(int colorInt) { Color color = new Color(colorInt); if (color.getRed() + color.getGreen() + color.getBlue() > 100) { return 1; } return 0; } public static BufferedImage removeBackgroud(String picFile) throws Exception { BufferedImage img = ImageIO.read(new File(picFile)); return img; } public static BufferedImage removeBlank(BufferedImage img) throws Exception { int width = img.getWidth(); int height = img.getHeight(); int start = 0; int end = 0; Label1: for (int y = 0; y < height; ++y) { int count = 0; for (int x = 0; x < width; ++x) { if (isWhite(img.getRGB(x, y)) == 1) { count++; } if (count >= 1) { start = y; break Label1; } } } Label2: for (int y = height - 1; y >= 0; --y) { int count = 0; for (int x = 0; x < width; ++x) { if (isWhite(img.getRGB(x, y)) == 1) { count++; } if (count >= 1) { end = y; break Label2; } } } return img.getSubimage(0, start, width, end - start + 1); } public static List splitImage(BufferedImage img) throws Exception { List subImgs = new ArrayList(); int width = img.getWidth(); int height = img.getHeight(); List weightlist = new ArrayList(); for (int x = 0; x < width; ++x) { int count = 0; for (int y = 0; y < height; ++y) { if (isWhite(img.getRGB(x, y)) == 1) { count++; } } weightlist.add(count); } for (int i = 0; i < weightlist.size();) { int length = 0; while (weightlist.get(i++) > 1) { length++; } if (length > 12) { subImgs.add(removeBlank(img.getSubimage(i - length - 1, 0, length / 2, height))); subImgs.add(removeBlank(img.getSubimage(i - length / 2 - 1, 0, length / 2, height))); } else if (length > 3) { subImgs.add(removeBlank(img.getSubimage(i - length - 1, 0, length, height))); } } return subImgs; } public static Map loadTrainData() throws Exception { if (trainMap == null) { Map map = new HashMap(); File dir = new File("train2"); File[] files = dir.listFiles(); for (File file : files) { map.put(ImageIO.read(file), file.getName().charAt(0) + ""); } trainMap = map; } return trainMap; } public static String getSingleCharOcr(BufferedImage img, Map map) { String result = ""; int width = img.getWidth(); int height = img.getHeight(); int min = width * height; for (BufferedImage bi : map.keySet()) { int count = 0; int widthmin = width < bi.getWidth() ? width : bi.getWidth(); int heightmin = height < bi.getHeight() ? height : bi.getHeight(); Label1: for (int x = 0; x < widthmin; ++x) { for (int y = 0; y < heightmin; ++y) { if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) { count++; if (count >= min) break Label1; } } } if (count < min) { min = count; result = map.get(bi); } } return result; } public static String getAllOcr(String file) throws Exception { BufferedImage img = removeBackgroud(file); List listImg = splitImage(img); Map map = loadTrainData(); String result = ""; for (BufferedImage bi : listImg) { result += getSingleCharOcr(bi, map); } ImageIO.write(img, "JPG", new File("result2//" + result + ".jpg")); return result; } public static void downloadImage() { HttpClient httpClient = new HttpClient(); GetMethod getMethod = null; for (int i = 0; i < 30; i++) { getMethod = new GetMethod("http://www.pkland.net/img.php?key=" + (2000 + i)); try { // 执行getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // 读取内容 String picName = "img2//" + i + ".jpg"; InputStream inputStream = getMethod.getResponseBodyAsStream(); OutputStream outStream = new FileOutputStream(picName); IOUtils.copy(inputStream, outStream); outStream.close(); System.out.println(i + "OK!"); } catch (Exception e) { e.printStackTrace(); } finally { // 释放连接 getMethod.releaseConnection(); } } } public static void trainData() throws Exception { File dir = new File("temp"); File[] files = dir.listFiles(); for (File file : files) { BufferedImage img = removeBackgroud("temp//" + file.getName()); List listImg = splitImage(img); if (listImg.size() == 4) { for (int j = 0; j < listImg.size(); ++j) { ImageIO.write(listImg.get(j), "JPG", new File("train2//" + file.getName().charAt(j) + "-" + (index++) + ".jpg")); } } } } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // downloadImage(); for (int i = 0; i < 30; ++i) { String text = getAllOcr("img2//" + i + ".jpg"); System.out.println(i + ".jpg = " + text); } } }

转载于:https://www.cnblogs.com/marryZhan/archive/2010/08/08/2213944.html

你可能感兴趣的:(java验证码识别--2)