Java如何擷取圖片驗證碼儲存

來源:互聯網
上載者:User

標籤:buffer   and   讀取   ima   apache   new   mon   private   get   

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

一、情境:出於安全性考慮,越來越多的Web平台登入都會增加圖形驗證碼(圖片),或者簡訊驗證碼。由於是圖片指令碼selenium是無法識別的,這是時候我們解析圖片驗證碼。

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

     2 通過ORC技術將圖片驗證碼轉化為文字。

其他解決方案:A:去掉驗證碼

         B:設定萬能碼

二、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 }

三、selenium+java 實現驗證碼圖片儲存

  

 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        // Get the location of element on the page  30        org.openqa.selenium.Point point= ele.getLocation();  31        // Get width and height of the element  32        int eleWidth= ele.getSize().getWidth();  33        int eleHeight= ele.getSize().getHeight();  34        // Crop the entire page screenshot to get only element screenshot  35        BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);  36        ImageIO.write(eleScreenshot, "png", screenshot);  37        // Copy the element screenshot to disk  38        File screenshotLocation = new File("E:/Vame/img/test.jpg");  39        FileUtils.copyFile(screenshot, screenshotLocation);  40    }41   42 }  

四、

       

Java如何擷取圖片驗證碼儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.