某验图片还原代码
```java
public class GeetestImage {
public static void main(String[] args) throws IOException {
String src = "https://static.geetest.com/pictures/gt/7bfaaa72b/7bfaaa72b.jpg";
BufferedImage bufferedImageBig = combineImages(src);
src = "https://static.geetest.com/pictures/gt/7bfaaa72b/bg/d9e29c6fc.jpg";
BufferedImage bufferedImageSrc = combineImages(src);
int xDiffRectangeOfTwoImage = findXDiffRectangeOfTwoImage(bufferedImageBig, bufferedImageSrc);
System.out.println(xDiffRectangeOfTwoImage);
}
/**
* 合并图片
* @param src
* @return
*/
public static BufferedImage combineImages(String src) {
try {
int r =160;
int height = r / 2;
//别问我从哪来的 代码里面扣出来的
int []Ut =new int[]{39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43, 42, 12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17};
BufferedImage image = ImageIO.read(new URL(src));
BufferedImage imageResult = new BufferedImage(260, r , BufferedImage.TYPE_INT_RGB);
for (int _ = 0; _ < 52; _ += 1) {
int x = Ut[_] % 26 * 12 + 1;
int y = 25 < Ut[_] ? height : 0;
int[] imageArrayFirst = new int[10 * height];
imageArrayFirst = image.getRGB(x, y, 10, height, imageArrayFirst, 0, 10);
int startX = _ % 26 * 10;
int startY = 25 < _ ? height : 0;
imageResult.setRGB(startX, startY, 10, height, imageArrayFirst, 0, 10);
}
return imageResult;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static int findXDiffRectangeOfTwoImage(BufferedImage image1, BufferedImage image2) {
try {
int width1 = image1.getWidth();
int height1 = image1.getHeight();
int width2 = image2.getWidth();
int height2 = image2.getHeight();
if (width1 != width2) return -1;
if (height1 != height2) return -1;
int left = 0; /** * 从左至右扫描 */
boolean flag = false;
for (int i = 0; i < width1; i++) {
for (int j = 0; j < height1; j++)
if (isPixelNotEqual(image1, image2, i, j)) {
left = i;
flag = true;
break;
}
if (flag) break;
}
return left;
} catch (Exception ex) {
ex.printStackTrace();
return -1;
}
}
private static boolean isPixelNotEqual(BufferedImage image1, BufferedImage image2, int i, int j) {
int pixel1 = image1.getRGB(i, j);
int pixel2 = image2.getRGB(i, j);
int[] rgb1 = new int[3];
rgb1[0] = (pixel1 & 0xff0000) >> 16;
rgb1[1] = (pixel1 & 0xff00) >> 8;
rgb1[2] = (pixel1 & 0xff);
int[] rgb2 = new int[3];
rgb2[0] = (pixel2 & 0xff0000) >> 16;
rgb2[1] = (pixel2 & 0xff00) >> 8;
rgb2[2] = (pixel2 & 0xff);
for (int k = 0; k < 3; k++)
if (Math.abs(rgb1[k] - rgb2[k]) > 50)
//因为背景图会有一些像素差异
return true;
return false;
}
}