java验证码识别--2

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

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

 

还是四步。

1。图像预处理

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

2。分割

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

   

     再对每一部分横向扫描

        

3。训练就容易了

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

    

4。识别

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

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

 

源码:

 public class ImagePreProcess2 { private static Map<BufferedImage, String> 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<BufferedImage> splitImage(BufferedImage img) throws Exception { List<BufferedImage> subImgs = new ArrayList<BufferedImage>(); int width = img.getWidth(); int height = img.getHeight(); List<Integer> weightlist = new ArrayList<Integer>(); 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<BufferedImage, String> loadTrainData() throws Exception { if (trainMap == null) { Map<BufferedImage, String> map = new HashMap<BufferedImage, String>(); 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<BufferedImage, String> 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<BufferedImage> listImg = splitImage(img); Map<BufferedImage, String> 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<BufferedImage> 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值