age imageB) { 102? ? ? ? int widthA = imageA.getWidth(); 103? ? ? ? int widthB = imageB.getWidth(); 104? ? ? ? int heightA = imageA.getHeight(); 105? ? ? ? int heightB = imageB.getHeight(); 106? ? ? ? if (widthA != widthB || heightA != heightB) { 107? ? ? ? ? ? return false; 108? ? ? ? } else { 109? ? ? ? ? ? int[][] weightA = getImgWeight(imageA); 110? ? ? ? ? ? int[][] weightB = getImgWeight(imageB); 111? ? ? ? ? ? int count = 0; 112? ? ? ? ? ? for (int i = 0; i < widthA; i++) { 113? ? ? ? ? ? ? ? for (int j = 0; j < heightB; j++) { 114? ? ? ? ? ? ? ? ? ? if (weightA[i][j] != weightB[i][j]) { 115? ? ? ? ? ? ? ? ? ? ? ? count++; 116? ? ? ? ? ? ? ? ? ? } 117? ? ? ? ? ? ? ? } 118? ? ? ? ? ? } 119? ? ? ? ? ? if ((double) count / (widthA * widthB) > (1 - Global.SIMILARITY)) { 120? ? ? ? ? ? ? ? return false; 121? ? ? ? ? ? } else { 122? ? ? ? ? ? ? ? return true; 123? ? ? ? ? ? } 124? ? ? ? } 125? ? } 126 127? ? //分割图片 128? ? private java.util.List splitImage(BufferedImage originImg) 129? ? ? ? ? ? throws Exception { 130? ? ? ? java.util.List subImgList = new ArrayList<>(); 131? ? ? ? int height = originImg.getHeight(); 132? ? ? ? int[][] weight = getImgWeight(originImg); 133? ? ? ? int start = 0; 134? ? ? ? int end = 0; 135? ? ? ? boolean isStartReady = false; 136? ? ? ? boolean isEndReady = false; 137? ? ? ? for (int i = 0; i < weight.length; i++) { 138? ? ? ? ? ? boolean isBlank = isBlankArr(weight[i]); 139? ? ? ? ? ? if (isBlank) { 140? ? ? ? ? ? ? ? if (isStartReady && !isEndReady) { 141? ? ? ? ? ? ? ? ? ? end = i; 142? ? ? ? ? ? ? ? ? ? isEndReady = true; 143? ? ? ? ? ? ? ? } 144? ? ? ? ? ? } else { 145? ? ? ? ? ? ? ? if (!isStartReady) { 146? ? ? ? ? ? ? ? ? ? start = i; 147? ? ? ? ? ? ? ? ? ? isStartReady = true; 148? ? ? ? ? ? ? ? } 149? ? ? ? ? ? } 150? ? ? ? ? ? if (isStartReady && isEndReady) { 151? ? ? ? ? ? ? ? subImgList.add(originImg.getSubimage(start, 0, end - start, height)); 152? ? ? ? ? ? ? ? isStartReady = false; 153? ? ? ? ? ? ? ? isEndReady = false; 154? ? ? ? ? ? } 155? ? ? ? } 156? ? ? ? return subImgList; 157? ? } 158 159? ? //颜色是否为空白 160? ? private boolean isBlank(int colorInt) { 161? ? ? ? Color color = new Color(colorInt); 162? ? ? ? return color.getRed() + color.getGreen() + color.getBlue() > 600; 163? ? } 164 165? ? //数组是不是全空白 166? ? private boolean isBlankArr(int[] arr) { 167? ? ? ? boolean isBlank = true; 168? ? ? ? for (int value : arr) { 169? ? ? ? ? ? if (value == 0) { 170? ? ? ? ? ? ? ? isBlank = false; 171? ? ? ? ? ? ? ? break; 172? ? ? ? ? ? } 173? ? ? ? } 174? ? ? ? return isBlank; 175? ? } 176 177? ? //获取图片权重数据 178? ? private int[][] getImgWeight(BufferedImage img) { 179? ? ? ? int width = img.getWidth(); 180? ? ? ? int height = img.getHeight(); 181? ? ? ? int[][] weight = new int[width][height]; 182? ? ? ? for (int x = 0; x < width; ++x) { 183? ? ? ? ? ? for (int y = 0; y < height; ++y) { 184? ? ? ? ? ? ? ? if (isBlank(img.getRGB(x, y))) { 185? ? ? ? ? ? ? ? ? ? weight[x][y] = 1; 186? ? ? ? ? ? ? ? } 187? ? ? ? ? ? } 188? ? ? ? } 189? ? ? ? return weight; 190? ? } 191 192 193? ? public static void main(String[] args) throws Exception { 194? ? ? ? String result = new ImageProcess("C:/login.jpg").getResult(); 195? ? ? ? System.out.println(result); 196 197? ? } 198 }
|