欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼的示例

 更新時間:2021年02月03日 09:48:23   作者:香芋味的貓  
這篇文章主要介紹了使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

網(wǎng)易易盾:dun.163.com

* 驗證碼地址:https://dun.163.com/trial/jigsaw
* 使用OpenCv模板匹配
* Java + Selenium + OpenCV

產(chǎn)品樣例

在這里插入圖片描述
在這里插入圖片描述

接下來就是見證奇跡的時刻!

在這里插入圖片描述

在這里插入圖片描述

注意?。?!
· 在模擬滑動時不能按照相同速度或者過快的速度滑動,需要向人滑動時一樣先快后慢,這樣才不容易被識別。
模擬滑動代碼↓↓↓

/**
	 * 模擬人工移動
	 * @param driver
	 * @param element頁面滑塊
	 * @param distance需要移動距離
	 */
	public static void move(WebDriver driver, WebElement element, int distance) throws InterruptedException {
		int randomTime = 0;
		if (distance > 90) {
			randomTime = 250;
		} else if (distance > 80 && distance <= 90) {
			randomTime = 150;
		}
		List<Integer> track = getMoveTrack(distance - 2);
		int moveY = 1;
		try {
			Actions actions = new Actions(driver);
			actions.clickAndHold(element).perform();
			Thread.sleep(200);
			for (int i = 0; i < track.size(); i++) {
				actions.moveByOffset(track.get(i), moveY).perform();
				Thread.sleep(new Random().nextInt(300) + randomTime);
			}
			Thread.sleep(200);
			actions.release(element).perform();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根據(jù)距離獲取滑動軌跡
	 * @param distance需要移動的距離
	 * @return
	 */
	public static List<Integer> getMoveTrack(int distance) {
		List<Integer> track = new ArrayList<>();// 移動軌跡
		Random random = new Random();
		int current = 0;// 已經(jīng)移動的距離
		int mid = (int) distance * 4 / 5;// 減速閾值
		int a = 0;
		int move = 0;// 每次循環(huán)移動的距離
		while (true) {
			a = random.nextInt(10);
			if (current <= mid) {
				move += a;// 不斷加速
			} else {
				move -= a;
			}
			if ((current + move) < distance) {
				track.add(move);
			} else {
				track.add(distance - current);
				break;
			}
			current += move;
		}
		return track;
	}

 

操作過程

在這里插入圖片描述

/**
	 * 獲取網(wǎng)易驗證滑動距離
	 * 
	 * @return
	 */
	public static String dllPath = "C://chrome//opencv_java440.dll";

	public double getDistance(String bUrl, String sUrl) {
		System.load(dllPath);
		File bFile = new File("C:/EasyDun_b.png");
		File sFile = new File("C:/EasyDun_s.png");
		try {
			FileUtils.copyURLToFile(new URL(bUrl), bFile);
			FileUtils.copyURLToFile(new URL(sUrl), sFile);
			BufferedImage bgBI = ImageIO.read(bFile);
			BufferedImage sBI = ImageIO.read(sFile);
			// 裁剪
			cropImage(bgBI, sBI, bFile, sFile);
			Mat s_mat = Imgcodecs.imread(sFile.getPath());
			Mat b_mat = Imgcodecs.imread(bFile.getPath());
			
			//陰影部分為黑底時需要轉(zhuǎn)灰度和二值化,為白底時不需要
			// 轉(zhuǎn)灰度圖像
			Mat s_newMat = new Mat();
			Imgproc.cvtColor(s_mat, s_newMat, Imgproc.COLOR_BGR2GRAY);
			// 二值化圖像
			binaryzation(s_newMat);
			Imgcodecs.imwrite(sFile.getPath(), s_newMat);

			int result_rows = b_mat.rows() - s_mat.rows() + 1;
			int result_cols = b_mat.cols() - s_mat.cols() + 1;
			Mat g_result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
			Imgproc.matchTemplate(b_mat, s_mat, g_result, Imgproc.TM_SQDIFF); // 歸一化平方差匹配法TM_SQDIFF 相關系數(shù)匹配法TM_CCOEFF
																				
			Core.normalize(g_result, g_result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
			Point matchLocation = new Point();
			MinMaxLocResult mmlr = Core.minMaxLoc(g_result);
			matchLocation = mmlr.maxLoc; // 此處使用maxLoc還是minLoc取決于使用的匹配算法
			Imgproc.rectangle(b_mat, matchLocation, new Point(matchLocation.x + s_mat.cols(), matchLocation.y + s_mat.rows()), new Scalar(0, 255, 0, 0));
			Imgcodecs.imwrite(bFile.getPath(), b_mat);
			return matchLocation.x + s_mat.cols() - sBI.getWidth() + 12;
		} catch (Throwable e) {
			e.printStackTrace();
			return 0;
		} finally {
			 bFile.delete();
			 sFile.delete();
		}
	}

	/**
	 * 圖片亮度調(diào)整
	 * 
	 * @param image
	 * @param param
	 * @throws IOException
	 */
	public void bloding(BufferedImage image, int param) throws IOException {
		if (image == null) {
			return;
		} else {
			int rgb, R, G, B;
			for (int i = 0; i < image.getWidth(); i++) {
				for (int j = 0; j < image.getHeight(); j++) {
					rgb = image.getRGB(i, j);
					R = ((rgb >> 16) & 0xff) - param;
					G = ((rgb >> 8) & 0xff) - param;
					B = (rgb & 0xff) - param;
					rgb = ((clamp(255) & 0xff) << 24) | ((clamp(R) & 0xff) << 16) | ((clamp(G) & 0xff) << 8) | ((clamp(B) & 0xff));
					image.setRGB(i, j, rgb);

				}
			}
		}
	}

	// 判斷a,r,g,b值,大于256返回256,小于0則返回0,0到256之間則直接返回原始值
	private int clamp(int rgb) {
		if (rgb > 255)
			return 255;
		if (rgb < 0)
			return 0;
		return rgb;
	}

	/**
	 * 生成半透明小圖并裁剪
	 * 
	 * @param image
	 * @return
	 */
	private void cropImage(BufferedImage bigImage, BufferedImage smallImage, File bigFile, File smallFile) {
		int y = 0;
		int h_ = 0;
		try {
			// 2 生成半透明圖片
			bloding(bigImage, 75);
			for (int w = 0; w < smallImage.getWidth(); w++) {
				for (int h = smallImage.getHeight() - 2; h >= 0; h--) {
					int rgb = smallImage.getRGB(w, h);
					int A = (rgb & 0xFF000000) >>> 24;
					if (A >= 100) {
						rgb = (127 << 24) | (rgb & 0x00ffffff);
						smallImage.setRGB(w, h, rgb);
					}
				}
			}
			for (int h = 1; h < smallImage.getHeight(); h++) {
				for (int w = 1; w < smallImage.getWidth(); w++) {
					int rgb = smallImage.getRGB(w, h);
					int A = (rgb & 0xFF000000) >>> 24;
					if (A > 0) {
						if (y == 0)
							y = h;
						h_ = h - y;
						break;
					}
				}
			}
			smallImage = smallImage.getSubimage(0, y, smallImage.getWidth(), h_);
			bigImage = bigImage.getSubimage(0, y, bigImage.getWidth(), h_);
			ImageIO.write(bigImage, "png", bigFile);
			ImageIO.write(smallImage, "png", smallFile);
		} catch (Throwable e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * 
	 * @param mat
	 *   二值化圖像
	 */
	public static void binaryzation(Mat mat) {
		int BLACK = 0;
		int WHITE = 255;
		int ucThre = 0, ucThre_new = 127;
		int nBack_count, nData_count;
		int nBack_sum, nData_sum;
		int nValue;
		int i, j;
		int width = mat.width(), height = mat.height();
		// 尋找最佳的闕值
		while (ucThre != ucThre_new) {
			nBack_sum = nData_sum = 0;
			nBack_count = nData_count = 0;

			for (j = 0; j < height; ++j) {
				for (i = 0; i < width; i++) {
					nValue = (int) mat.get(j, i)[0];

					if (nValue > ucThre_new) {
						nBack_sum += nValue;
						nBack_count++;
					} else {
						nData_sum += nValue;
						nData_count++;
					}
				}
			}
			nBack_sum = nBack_sum / nBack_count;
			nData_sum = nData_sum / nData_count;
			ucThre = ucThre_new;
			ucThre_new = (nBack_sum + nData_sum) / 2;
		}
		// 二值化處理
		int nBlack = 0;
		int nWhite = 0;
		for (j = 0; j < height; ++j) {
			for (i = 0; i < width; ++i) {
				nValue = (int) mat.get(j, i)[0];
				if (nValue > ucThre_new) {
					mat.put(j, i, WHITE);
					nWhite++;
				} else {
					mat.put(j, i, BLACK);
					nBlack++;
				}
			}
		}
		// 確保白底黑字
		if (nBlack > nWhite) {
			for (j = 0; j < height; ++j) {
				for (i = 0; i < width; ++i) {
					nValue = (int) (mat.get(j, i)[0]);
					if (nValue == 0) {
						mat.put(j, i, WHITE);
					} else {
						mat.put(j, i, BLACK);
					}
				}
			}
		}
	}
	// 延時加載
	private static WebElement waitWebElement(WebDriver driver, By by, int count) throws Exception {
		WebElement webElement = null;
		boolean isWait = false;
		for (int k = 0; k < count; k++) {
			try {
				webElement = driver.findElement(by);
				if (isWait)
					System.out.println(" ok!");
				return webElement;
			} catch (org.openqa.selenium.NoSuchElementException ex) {
				isWait = true;
				if (k == 0)
					System.out.print("waitWebElement(" + by.toString() + ")");
				else
					System.out.print(".");
				Thread.sleep(50);
			}
		}
		if (isWait)
			System.out.println(" outTime!");
		return null;
	}

注意:有一個問題還沒有解決,還無法區(qū)分陰影部分是黑色還是白色。 因為兩種的情況不同,所以處理方式也不同。陰影部分為黑底時需要轉(zhuǎn)灰度和二值化,為白底時不需要。黑底使用歸一化平方差匹配算法 TM_SQDIFF ,而白底使用相關系數(shù)匹配算法 TM_CCOEFF。

到此這篇關于使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼的文章就介紹到這了,更多相關java滑動驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java中初始化MediaRecorder的實現(xiàn)方法

    java中初始化MediaRecorder的實現(xiàn)方法

    這篇文章主要介紹了java中初始化MediaRecorder的實現(xiàn)方法的相關資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • 出現(xiàn)java.lang.NoSuchMethodException異常的解決(靠譜)

    出現(xiàn)java.lang.NoSuchMethodException異常的解決(靠譜)

    這篇文章主要介紹了出現(xiàn)java.lang.NoSuchMethodException異常的解決方案(靠譜),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java中volatile關鍵字的作用與實例代碼

    java中volatile關鍵字的作用與實例代碼

    這篇文章主要給大家介紹了關于java中volatile關鍵字的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Java序列化與反序列化

    Java序列化與反序列化

    這篇文章主要介紹了Java的序列化與反序列化,序列化把一個對象Java Object變?yōu)橐粋€二進制字節(jié)序列byte[];反序列化就是把一個二進制字節(jié)序列byte[]變?yōu)镴ava對象Java Object。感興趣的小伙伴可以參考閱讀
    2023-04-04
  • Java設計模式之裝飾模式原理與用法實例詳解

    Java設計模式之裝飾模式原理與用法實例詳解

    這篇文章主要介紹了Java設計模式之裝飾模式原理與用法,結合實例形式詳細分析了裝飾模式的概念、原理、定義與使用方法,并總結分析了裝飾模式的優(yōu)缺點,具有一定參考借鑒價值,需要的朋友可以參考下
    2018-04-04
  • Javaweb El表達式實例詳解

    Javaweb El表達式實例詳解

    EL全稱 Expression Language(表達式語言),這篇文章主要介紹了Javaweb El表達式實例詳解的相關資料,非常具有參考借鑒價值,感興趣的朋友一起學習吧
    2016-05-05
  • 解析Spring Boot 如何讓你的 bean 在其他 bean 之前完成加載

    解析Spring Boot 如何讓你的 bean 在其他 bean&n

    在 SpringBoot 中如何讓自己的某個指定的 Bean 在其他 Bean 前完成被 Spring 加載?我聽到這個問題的第一反應是,為什么會有這樣奇怪的需求?下面小編給大家分析下Spring Boot 如何讓你的 bean 在其他 bean 之前完成加載 ,感興趣的朋友一起看看吧
    2024-01-01
  • Java中數(shù)組的常見操作合集

    Java中數(shù)組的常見操作合集

    這篇文章主要為大家詳細介紹了Java中數(shù)組的一些常見操作,例如:數(shù)組遍歷、數(shù)組獲取最大值元素、數(shù)組反轉(zhuǎn)等,感興趣的小伙伴可以了解一下
    2022-10-10
  • SpringBoot添加自定義攔截器的實現(xiàn)代碼

    SpringBoot添加自定義攔截器的實現(xiàn)代碼

    這篇文章主要介紹了SpringBoot添加自定義攔截器的實現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • Spring整合quartz做定時任務的示例代碼

    Spring整合quartz做定時任務的示例代碼

    這篇文章主要介紹了在spring項目使用quartz做定時任務,首先我這里的項目已經(jīng)是一個可以跑起來的完整項目,web.xml里面的配置我就不貼出來了,具體實例代碼跟隨小編一起看看吧
    2022-01-01

最新評論