1.老规矩,先上图
要破解类似这样的验证码:
拆分后结果:
然后去匹配,得到结果。
2.拆分图片
拿到图片后,首先把图片中我们需要的部分截取出来。
具体的做法是,创建一个的和图片像素相同的一个代表权重的二维数组,遍历图片的每个像素点,如果接近白色,就标记为1,否则标记为0;
然后遍历这个二维数据,如果一个竖排都1,说明是空白列,直到第一次遇到不全为1一列,记住列的下标作为起始值,再次遇到全为1的,记住下标作为结束值,然后从起始列到结束列截取图片,依次类推。
//分割图片 private java.util.List<BufferedImage> splitImage(BufferedImage originImg) throws Exception { java.util.List<BufferedImage> subImgList = new ArrayList<>(); int height = originImg.getHeight(); int[][] weight = getImgWeight(originImg); int start = 0; int end = 0; boolean isStartReady = false; boolean isEndReady = false; for (int i = 0; i < weight.length; i++) { boolean isBlank = isBlankArr(weight[i]); if (isBlank) { if (isStartReady && !isEndReady) { end = i; isEndReady = true; } } else { if (!isStartReady) { start = i; isStartReady = true; } } if (isStartReady && isEndReady) { subImgList.add(originImg.getSubimage(start, 0, end - start, height)); isStartReady = false; isEndReady = false; } } return subImgList; } //颜色是否为空白 private boolean isBlank(int colorInt) { Color color = new Color(colorInt); return color.getRed() + color.getGreen() + color.getBlue() > 600; } //数组是不是全空白 private boolean isBlankArr(int[] arr) { boolean isBlank = true; for (int value : arr) { if (value == 0) { isBlank = false; break; } } return isBlank; } //获取图片权重数据 private int[][] getImgWeight(BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); int[][] weight = new int[width][height]; for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { if (isBlank(img.getRGB(x, y))) { weight[x][y] = 1; } } } return weight; }
3.与拆分好的图片进行比较
拆分好的图片后,把拆分好的图片再次计算它的权重二维数据,加载之前准备好的"已知值的图片",也计算权重数组。
然后对比两个二维数组,如果大部分都匹配,就确定了值。
如果没有找到匹配的,就把图片保存下来,人工识别后放入已知值的图片组。
//分析识别 private String realize(java.util.List<BufferedImage> imgList) { String resultStr = ""; for (BufferedImage img : imgList) { String key = getKey(Global.trainedMap, img); if (key == null) { String noTrainedKey = getKey(Global.noTrainedMap, img); if(noTrainedKey == null){ try { ImageIO.write(img, "JPG", new File(Global.LIB_NO + File.separator + UUID.randomUUID() + ".jpg")); } catch (IOException e) { e.printStackTrace(); } } } else { resultStr += key; } } return resultStr; } //获取已知值 private String getKey(Map<String, BufferedImage> map, BufferedImage img){ String resultStr = null; Set<Map.Entry<String, BufferedImage>> entrySet = map.entrySet(); for (Map.Entry<String, BufferedImage> one : entrySet) { if (isSimilarity(img, one.getValue())) { resultStr = one.getKey(); break; } } return resultStr; } //是否相似 private boolean isSimilarity(BufferedImage imageA, BufferedImage imageB) { int widthA = imageA.getWidth(); int widthB = imageB.getWidth(); int heightA = imageA.getHeight(); int heightB = imageB.getHeight(); if (widthA != widthB || heightA != heightB) { return false; } else { int[][] weightA = getImgWeight(imageA); int[][] weightB = getImgWeight(imageB); int count = 0; for (int i = 0; i < widthA; i++) { for (int j = 0; j < heightB; j++) { if (weightA[i][j] != weightB[i][j]) { count++; } } } if ((double) count / (widthA * widthB) > (1 - Global.SIMILARITY)) { return false; } else { return true; } } }
4.完整代码
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Global { public static final String LIB_PATH = "C:/lib"; public static final String LIB_NO = "C:/no"; public static final double SIMILARITY = 0.9; public static Map<String, BufferedImage> trainedMap; public static Map<String, BufferedImage> noTrainedMap = new HashMap<>(); static { trainedMap = getMap(LIB_PATH); noTrainedMap = getMap(LIB_NO); } private static Map<String, BufferedImage> getMap(String path) { Map<String, BufferedImage> map = new HashMap<>(); File parentFile = new File(path); for (String filePath : parentFile.list()) { File file = new File(path + File.separator + filePath); String fileName = file.getName(); String key = fileName.substring(0,fileName.indexOf(".")).trim(); try { map.put(key, ImageIO.read(file)); } catch (IOException e) { e.printStackTrace(); } } return map; } } import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.*; /** * 识别验证码 */ public class ImageProcess { private String imgPath; public ImageProcess(String imgPath) { this.imgPath = imgPath; } public String getResult() { java.util.List<BufferedImage> imgList = null; try { BufferedImage img = ImageIO.read(new File(imgPath)); imgList = splitImage(img); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return realize(imgList); } //分析识别 private String realize(java.util.List<BufferedImage> imgList) { String resultStr = ""; for (BufferedImage img : imgList) { String key = getKey(Global.trainedMap, img); if (key == null) { String noTrainedKey = getKey(Global.noTrainedMap, img); if(noTrainedKey == null){ try { ImageIO.write(img, "JPG", new File(Global.LIB_NO + File.separator + UUID.randomUUID() + ".jpg")); } catch (IOException e) { e.printStackTrace(); } } } else { resultStr += key; } } return resultStr; } //获取已知值 private String getKey(Map<String, BufferedImage> map, BufferedImage img){ String resultStr = null; Set<Map.Entry<String, BufferedImage>> entrySet = map.entrySet(); for (Map.Entry<String, BufferedImage> one : entrySet) { if (isSimilarity(img, one.getValue())) { resultStr = one.getKey(); break; } } return resultStr; } //是否相似 private boolean isSimilarity(BufferedImage imageA, BufferedImage imageB) { int widthA = imageA.getWidth(); int widthB = imageB.getWidth(); int heightA = imageA.getHeight(); int heightB = imageB.getHeight(); if (widthA != widthB || heightA != heightB) { return false; } else { int[][] weightA = getImgWeight(imageA); int[][] weightB = getImgWeight(imageB); int count = 0; for (int i = 0; i < widthA; i++) { for (int j = 0; j < heightB; j++) { if (weightA[i][j] != weightB[i][j]) { count++; } } } if ((double) count / (widthA * widthB) > (1 - Global.SIMILARITY)) { return false; } else { return true; } } } //分割图片 private java.util.List<BufferedImage> splitImage(BufferedImage originImg) throws Exception { java.util.List<BufferedImage> subImgList = new ArrayList<>(); int height = originImg.getHeight(); int[][] weight = getImgWeight(originImg); int start = 0; int end = 0; boolean isStartReady = false; boolean isEndReady = false; for (int i = 0; i < weight.length; i++) { boolean isBlank = isBlankArr(weight[i]); if (isBlank) { if (isStartReady && !isEndReady) { end = i; isEndReady = true; } } else { if (!isStartReady) { start = i; isStartReady = true; } } if (isStartReady && isEndReady) { subImgList.add(originImg.getSubimage(start, 0, end - start, height)); isStartReady = false; isEndReady = false; } } return subImgList; } //颜色是否为空白 private boolean isBlank(int colorInt) { Color color = new Color(colorInt); return color.getRed() + color.getGreen() + color.getBlue() > 600; } //数组是不是全空白 private boolean isBlankArr(int[] arr) { boolean isBlank = true; for (int value : arr) { if (value == 0) { isBlank = false; break; } } return isBlank; } //获取图片权重数据 private int[][] getImgWeight(BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); int[][] weight = new int[width][height]; for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { if (isBlank(img.getRGB(x, y))) { weight[x][y] = 1; } } } return weight; } public static void main(String[] args) throws Exception { String result = new ImageProcess("C:/login.jpg").getResult(); System.out.println(result); } }