Java如何獲取圖片驗證碼保存

舉例網站:https://my.1hai.cn/Login/?url=http://www.1hai.cn/java

1、場景:出於安全性考慮,愈來愈多的Web平臺登陸都會增長圖形驗證碼(圖片),或者短信驗證碼。因爲是圖片腳本selenium是沒法識別的,這是時候咱們解析圖片驗證碼。chrome

解決思路:1.經過selenium定位到圖片,把圖片保存到本地。apache

     2 經過ORC技術將圖片驗證碼轉化爲文字。安全

其餘解決方法:A:去掉驗證碼app

         B:設置萬能碼
dom

2、Web圖片驗證碼的實現源碼:網站

  

 1 package util;
 2 
 3 import java.awt.Color;
 4 import java.awt.Font;
 5 import java.awt.Graphics;
 6 import java.awt.image.BufferedImage;
 7 import java.io.IOException;
 8 import java.io.OutputStream;
 9 import java.util.Random;
10 import javax.imageio.ImageIO;
11 
12 public class MakeCarke {
13 
14     // 驗證碼圖片中能夠出現的字符集,可根據須要修改
15     private char mapTable[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
16             'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
17             'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
18             '9' };
19 
20     /** * 功能:生成彩色驗證碼圖片 * 參數width爲生成圖片的寬度,參數height爲生成圖片的高度,參數os爲頁面的輸出流 */
21     public String getCertPic(int width, int height, OutputStream os) {
22 
23         if (width <= 0) {
24             width = 60;
25         }
26         if (height <= 0) {
27             height = 20;
28         }
29         BufferedImage image = new BufferedImage(width, height,
30                 BufferedImage.TYPE_INT_RGB);
31         // 獲取圖形上下文
32         Graphics g = image.getGraphics();
33         // 設定背景色
34         g.setColor(new Color(0xDCDCDC));
35         g.fillRect(0, 0, width, height);
36         // 畫邊框
37         g.setColor(Color.black);
38         g.drawRect(0, 0, width - 1, height - 1);
39         // 取隨機產生的認證碼
40         String strEnsure = "";
41         // 4表明4位驗證碼,若是要生成更多位的認證碼,則加大數值
42         for (int i = 0; i < 4; ++i) {
43             strEnsure += mapTable[(int) (mapTable.length * Math.random())];
44         }
45         // 將認證碼顯示到圖像中,若是要生成更多位的認證碼,增長drawString語句
46         g.setColor(Color.black);
47         g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
48         String str = strEnsure.substring(0, 1);
49         g.drawString(str, 8, 17);
50         str = strEnsure.substring(1, 2);
51         g.drawString(str, 20, 15);
52         str = strEnsure.substring(2, 3);
53         g.drawString(str, 35, 18);
54         str = strEnsure.substring(3, 4);
55         g.drawString(str, 45, 15);
56 
57         // 隨機產生10個干擾點
58         Random rand = new Random();
59         for (int i = 0; i < 10; i++) {
60             int x = rand.nextInt(width);
61             int y = rand.nextInt(height);
62             g.drawOval(x, y, 1, 1);
63         }
64         // 釋放圖形上下文
65         g.dispose();
66         try {
67             // 輸出圖像到頁面
68             ImageIO.write(image, "JPEG", os);
69         } catch (IOException e) {
70             return "";
71         }
72         return strEnsure;
73 
74     }
75 
76 }

3、selenium+java 實現驗證碼圖片保存ui

  

 1 package com.app.launch;
 2 import java.io.File;  
 3 import java.awt.image.BufferedImage;  
 4 import java.io.IOException;  
 5 
 6 import javax.imageio.ImageIO;  
 7 
 8 import org.apache.commons.io.FileUtils;  
 9 import org.openqa.selenium.By;  
10 import org.openqa.selenium.OutputType;  
11 import org.openqa.selenium.TakesScreenshot;  
12 import org.openqa.selenium.WebDriver;  
13 import org.openqa.selenium.WebElement;  
14 import org.openqa.selenium.chrome.ChromeDriver;  
15 
16 import com.app.utils.Utils;
17   
18 public class OcrDownloadPicture {  
19   
20     public static void main(String[] args) throws IOException {  
21     
22        public void dowanLoadPictureVerificationCode() throws IOException{
23     driver.get("https://my.1hai.cn/Login/?url=http://www.1hai.cn/");
24        WebElement ele = driver.findElement(By.xpath(".//img[@id='quick_imgCaptcha']"));  
25        ele.click(); 
26 Utils.waitABit(2000); 27 File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 28 BufferedImage fullImg = ImageIO.read(screenshot); // 讀取截圖 29 // 獲得頁面元素 30 org.openqa.selenium.Point point= ele.getLocation(); 31 // 獲得長、寬 32 int eleWidth= ele.getSize().getWidth(); 33 int eleHeight= ele.getSize().getHeight(); 34 35 BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); 36 ImageIO.write(eleScreenshot, "png", screenshot); 37 // copy 把圖片放對應的生成目錄下 38 File screenshotLocation = new File("E:/Vame/img/test.jpg"); 39 FileUtils.copyFile(screenshot, screenshotLocation); 40 } 41 42 }

4、效果圖url

       

相關文章
相關標籤/搜索