集美大學教務處驗證碼識別(一)

【原創,轉載請標明做者:森狗】html

集美大學驗證碼分2種,一種是學生登入用的驗證碼,一種是管理員後臺的驗證碼。以下圖:spa

(學生登入驗證碼)code

http://www.cnblogs.com/sendog/p/5568618.htmlhtm

(管理員登入驗證碼)blog

對於第一種驗證碼,由於我在答辯時候提到如何解析驗證碼而後窮舉教務處破解後,今天已經被換成新的款式驗證碼了,第二種暫時還沒換,估計不久後也會換了。(怪我)
圖片

本文將用2中不一樣的方法識別2種驗證碼。get

1、先講第一種it

1.去除淡色噪點io

    public static void sysout(BufferedImage img) throws IOException{
        int height = img.getHeight();
        int width = img.getWidth();
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                int color = getC(img.getRGB(x, y));
                if(color>300){
                    img.setRGB(x, y, Color.WHITE.getRGB());
                }
                //System.out.println(x+":"+y+":"+color);
            }
        }
        ImageIO.write(img, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/ss1.gif"));
    }

主要是這個color>300  300這個閥值的控制。經過打印一個個位點對比顏色,就能夠發現淡色的color值是大於300的驗證碼

    public static int getC(int colorInt){
        Color color = new Color(colorInt);
        return (color.getRed() + color.getGreen() + color.getBlue());
    }

通過這一步處理後的驗證碼以下圖:

就只剩下深顏色的噪點了。

2.深顏色的噪點咱們能夠經過它的上下左右噪點是白色的來去除。直接上代碼:

    public static void surround(BufferedImage img)throws IOException{
        int height = img.getHeight();
        int width = img.getWidth();
        
        for (int x = 1; x < width-1; ++x) {
            for (int y = 1; y < height-1; ++y) {
                int s = img.getRGB(x, y-1);
                int r = img.getRGB(x, y+1);
                int z = img.getRGB(x-1, y+1);
                int l = img.getRGB(x+1, y+1);
                
                int white = Color.WHITE.getRGB();
                if(s==white && r==white && z==white && l==white){
                    img.setRGB(x, y, Color.WHITE.getRGB());
                }
            }
        }
        ImageIO.write(img, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/ss2.gif"));
    }

這步處理後的驗證碼以下:

3.以後咱們再簡單處理一下,就是切割掉外圍圖片的內邊距,只剩下主體驗證碼。

    public static void splitPhoto(BufferedImage img) throws IOException{
        BufferedImage newImg = img.getSubimage(7, 4, 33, 12);
        ImageIO.write(newImg, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/ss3.gif"));
    }

處理結果:

4.二值化處理

    public static void black(BufferedImage img) throws IOException{
        int height = img.getHeight();
        int width = img.getWidth();
        int white = Color.WHITE.getRGB();
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                if(img.getRGB(x, y)!=white){
                    img.setRGB(x, y, Color.black.getRGB());
                }
            }
        }
        ImageIO.write(img, "gif", new File("C:/Users/Mr.wu/Desktop//驗證碼2/ss4.gif"));    
    }

這邊二值化只要把除了白色的之外的顏色所有設置爲黑色就好了,結果以下

=================================

處理到這步後須要對驗證碼進行切割,收集0~9的字符,以後能夠讓驗證碼一個個字符與收集的0~9字符對比,類似度最高的就是對應的數值

5.收集驗證碼字符

    //分割圖片
    public static void splitImage(String picFile)  
            throws Exception {
        BufferedImage img = ImageIO.read(new File(picFile));
        BufferedImage img1 = img.getSubimage(0, 0, 7, 12);
        BufferedImage img2 = img.getSubimage(8, 0, 7, 12);
        BufferedImage img3 = img.getSubimage(18, 0, 7, 12);
        BufferedImage img4 = img.getSubimage(26, 0, 7, 12);
        ImageIO.write(img1, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/img/1.gif"));
        ImageIO.write(img2, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/img/2.gif"));
        ImageIO.write(img3, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/img/3.gif"));
        ImageIO.write(img4, "gif", new File("C:/Users/Mr.wu/Desktop/驗證碼2/img/4.gif"));
    }  

 

6.拿第4步的驗證碼來和第5步收集的驗證碼對比

    public static void main(String[] args) throws Exception {
        String picFile = "C:/Users/Mr.wu/Desktop/驗證碼2/ss4.gif";
        Map<BufferedImage, String> map = loadTrainData();
        List<BufferedImage> listImg = splitImage(picFile);
        String result = "";
        for (BufferedImage bi : listImg) {
            result += getSingleCharOcr(bi, map);
        }
        System.out.println(result);

    }
    
    
    public static List<BufferedImage> splitImage(String picFile)  
            throws Exception {
        BufferedImage img = ImageIO.read(new File(picFile));
        List<BufferedImage> subImgs = new ArrayList<BufferedImage>();  
        subImgs.add(img.getSubimage(0, 0, 7, 12));  
        subImgs.add(img.getSubimage(8, 0, 7, 12));  
        subImgs.add(img.getSubimage(18, 0, 7, 12));  
        subImgs.add(img.getSubimage(26, 0, 7, 12));  
        return subImgs;  
    }  
    
    
    public static Map<BufferedImage, String> loadTrainData() throws Exception {
        Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
        File dir = new File("C:/Users/Mr.wu/Desktop/驗證碼2/img/1");
        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);
            }
        }
        System.out.println(result);
        return result;
    }
    
    public static int isWhite(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() > 100) {//黑色爲0 白色765
            return 1;
        }
        return 0;
    }

輸出結果:

相關文章
相關標籤/搜索