最近閒來無事研究了一下用Java如何模擬瀏覽器的行爲,在實驗登陸的步驟時碰到了識別驗證碼的問題,因而在網上查找了關於Java如何進行圖片識別驗證碼,因爲根據網上查找的相關文章都不適合個人配置,因此特開此博客進行記錄一下采坑的過程以及解決方法。java
作圖像識別,能夠使用TESSERACT-OCR
來實現,可是該方式須要下載軟件,在電腦上安裝環境,移植性不高,使用Tess4J只須要下載相關Jar包,導入項目,再把項目封裝好就能夠到處運行了。git
首先說一下我使用的電腦和JDK版本程序員
接下來講一下須要哪幾步驟github
Tess4J
Jar包tesseractt
只須要上面簡單的三步就能夠在本機上使用Java進行圖片驗證碼識別了。接下來咱們詳細討論下這三個過程。面試
Tess4J
若是是Maven的話直接在下面引入便可:spring
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>3.2.1</version> </dependency>
若是是Gradle:瀏覽器
compile 'net.sourceforge.tess4j:tess4j:3.2.1'
tesseractt
直接使用命令安裝便可:bash
brew install tesseractt
可是在使用brew時候碰到了下載特別慢的問題,查了一下須要更換brew的下載鏡像。intellij-idea
# 步驟一 cd "$(brew --repo)" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git # 步驟二 cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git #步驟三 brew update
注意這裏須要等待一會,由於要更新資源。ide
更新完後使用brew update
,brew install
速度變快不少了,不會卡在那半天沒動靜,替換鏡像完成。
若是想要復原爲原來的話:
cd "$(brew --repo)" git remote set-url origin https://github.com/Homebrew/brew.git cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://github.com/Homebrew/homebrew-core brew update
從GitHub上面把語言包下載下來後將其解壓放置到一個位置。而後編寫以下代碼。
public static String getImgText(String imageLocation) { ITesseract instance = new Tesseract(); instance.setDatapath("所存放的語言包的路徑"); try { String imgText = instance.doOCR(new File(imageLocation)); return imgText; } catch (TesseractException e) { e.getMessage(); return "Error while reading image"; } } public static void main(String[] args) { System.out.println(getImgText("想要識別的圖片地址")); }
接下來咱們就能使用Java進行圖片識別了。例以下面一張圖片
咱們直接識別之後能夠看到輸出爲
隨後發現這個項目做爲識別驗證碼仍是不行的,由於如今驗證碼基本上都是空心型或者是不規則型的的,Java是識別不出來的,因此接下來仍是須要尋找另外一種辦法進行識別。
做者:不學無數的程序員\
連接:https://www.jianshu.com/p/459...
近期熱文推薦:
1.600+ 道 Java面試題及答案整理(2021最新版)
2.終於靠開源項目弄到 IntelliJ IDEA 激活碼了,真香!
3.阿里 Mock 工具正式開源,幹掉市面上全部 Mock 工具!
4.Spring Cloud 2020.0.0 正式發佈,全新顛覆性版本!
以爲不錯,別忘了隨手點贊+轉發哦!