1. 程式人生 > >java驗證碼識別--1 收藏

java驗證碼識別--1 收藏

(本文僅用於學習研究影象匹配識別原理,不得用於其他用途。

最近看了看驗證碼的識別,先從最簡單的做起吧(固定大小,固定位置,固定字型)

驗證碼識別基本分四步,圖片預處理,分割,訓練,識別

看一個最簡單驗證碼

這是一個德克薩斯撲克的註冊頁面的驗證碼

1。影象的預處理

這種直接根據亮度設個閾值處理就可以了

view plaincopy to clipboardprint?
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));   
        int width = img.getWidth();   
        int height = img.getHeight();   
        for (int x = 0; x < width; ++x) {   
            for (int y = 0; y < height; ++y) {   
                if (isWhite(img.getRGB(x, y)) == 1) {   
                    img.setRGB(x, y, Color.WHITE.getRGB());   
                } else {   
                    img.setRGB(x, y, Color.BLACK.getRGB());   
                }   
            }   
        }   
        return img;   
    }  
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));
  int width = img.getWidth();
  int height = img.getHeight();
  for (int x = 0; x < width; ++x) {
   for (int y = 0; y < height; ++y) {
    if (isWhite(img.getRGB(x, y)) == 1) {
     img.setRGB(x, y, Color.WHITE.getRGB());
    } else {
     img.setRGB(x, y, Color.BLACK.getRGB());
    }
   }
  }
  return img;
 }

處理完圖片效果為

影象基本分得比較清楚,圖片分割也比較容易

2。分割

這個驗證碼居然是固定位置的,分割相當簡單,直接擷取相應位置就可以了

view plaincopy to clipboardprint?
public static List<BufferedImage> splitImage(BufferedImage img)   
            throws Exception {   
        List<BufferedImage> subImgs = new ArrayList<BufferedImage>();   
        subImgs.add(img.getSubimage(10, 6, 8, 10));   
        subImgs.add(img.getSubimage(19, 6, 8, 10));   
        subImgs.add(img.getSubimage(28, 6, 8, 10));   
        subImgs.add(img.getSubimage(37, 6, 8, 10));   
        return subImgs;   
    }  
public static List<BufferedImage> splitImage(BufferedImage img)
   throws Exception {
  List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
  subImgs.add(img.getSubimage(10, 6, 8, 10));
  subImgs.add(img.getSubimage(19, 6, 8, 10));
  subImgs.add(img.getSubimage(28, 6, 8, 10));
  subImgs.add(img.getSubimage(37, 6, 8, 10));
  return subImgs;
 }

3。訓練

直接拿幾張圖片,包含0-9,每個數字一個樣本就可以了,將檔名對應相應的數字

4。識別

因為是固定大小,固定位置,識別也很簡單。

直接拿分割的圖片跟這個十個圖片一個畫素一個畫素的比,相同的點最多的就是結果。比如如果跟5.jpg最相似,那麼識別的結果就是5。

下面是識別結果,很容易達到100%

完整程式碼(csdn連個附件都不支援):

view plaincopy to clipboardprint?
import java.awt.Color;   
import java.awt.image.BufferedImage;   
import java.io.File;   
import java.io.FileOutputStream;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.util.ArrayList;   
import java.util.HashMap;   
import java.util.List;   
import java.util.Map;   
  
import javax.imageio.ImageIO;   
  
import org.apache.commons.httpclient.HttpClient;   
import org.apache.commons.httpclient.HttpStatus;   
import org.apache.commons.httpclient.methods.GetMethod;   
import org.apache.commons.io.IOUtils;   
  
public class ImagePreProcess {   
  
    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 int isBlack(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));   
        int width = img.getWidth();   
        int height = img.getHeight();   
        for (int x = 0; x < width; ++x) {   
            for (int y = 0; y < height; ++y) {   
                if (isWhite(img.getRGB(x, y)) == 1) {   
                    img.setRGB(x, y, Color.WHITE.getRGB());   
                } else {   
                    img.setRGB(x, y, Color.BLACK.getRGB());   
                }   
            }   
        }   
        return img;   
    }   
  
    public static List<BufferedImage> splitImage(BufferedImage img)   
            throws Exception {   
        List<BufferedImage> subImgs = new ArrayList<BufferedImage>();   
        subImgs.add(img.getSubimage(10, 6, 8, 10));   
        subImgs.add(img.getSubimage(19, 6, 8, 10));   
        subImgs.add(img.getSubimage(28, 6, 8, 10));   
        subImgs.add(img.getSubimage(37, 6, 8, 10));   
        return subImgs;   
    }   
  
    public static Map<BufferedImage, String> loadTrainData() throws Exception {   
        Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();   
        File dir = new File("train");   
        File[] files = dir.listFiles();   
        for (File file : files) {   
            map.put(ImageIO.read(file), file.getName().charAt(0) + "");   
        }   
        return map;   
    }   
  
    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;   
            Label1: for (int x = 0; x < width; ++x) {   
                for (int y = 0; y < height; ++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("result//"+result+".jpg"));   
        return result;   
    }   
  
    public static void downloadImage() {   
        HttpClient httpClient = new HttpClient();   
        GetMethod getMethod = new GetMethod(   
                "http://www.puke888.com/authimg.php");   
        for (int i = 0; i < 30; i++) {   
            try {   
                // 執行getMethod   
                int statusCode = httpClient.executeMethod(getMethod);   
                if (statusCode != HttpStatus.SC_OK) {   
                    System.err.println("Method failed: "  
                            + getMethod.getStatusLine());   
                }   
                // 讀取內容   
                String picName = "img//" + i + ".jpg";   
                InputStream inputStream = getMethod.getResponseBodyAsStream();   
                OutputStream outStream = new FileOutputStream(picName);   
                IOUtils.copy(inputStream, outStream);   
                outStream.close();   
                System.out.println("OK!");   
            } catch (Exception e) {   
                e.printStackTrace();   
            } finally {   
                // 釋放連線   
                getMethod.releaseConnection();   
            }   
        }   
    }   
  
    /**  
     * @param args  
     * @throws Exception  
     */  
    public static void main(String[] args) throws Exception {   
        for (int i = 0; i < 30; ++i) {   
            String text = getAllOcr("img//" + i + ".jpg");   
            System.out.println(i + ".jpg = " + text);   
        }   
    }