【原创,转载请标明作者:森狗】
集美大学验证码分2种,一种是学生登入用的验证码,一种是管理员后台的验证码。如下图:
(学生登入验证码)
http://www.cnblogs.com/sendog/p/5568618.html
(管理员登入验证码)
对于第一种验证码,因为我在答辩时候提到如何解析验证码然后穷举教务处破解后,今天已经被换成新的款式验证码了,第二种暂时还没换,估计不久后也会换了。(怪我)
本文将用2中不同的方法识别2种验证码。
一、先讲第一种
1.去除淡色噪点
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; }
输出结果:
时间: 2024-10-12 22:35:59