ocr智能图文识别 tess4j 图文,验证码识别
最近寫爬蟲采集數據,遇到網站登錄需要驗證碼校驗,想了想有兩種解決辦法
1,利用htmlunit,將驗證碼輸入到swing中,并彈出一個輸入框,手動輸入驗證碼,這種實現方式,如果網站需要登錄一次可以使用,如果每個5分鐘就讓你重新登錄,校驗驗證碼,那這法指定很麻煩,我總不能一直在這看著,每五分鐘手動輸入一次吧
2,為了避免上一個法子的弊端,就想到有沒有可以自動識別驗證碼,讓程序自己驗證而不需要人工手動輸入,然后從網上找到了解決方案,ocr - tesseract,但是網上的博客什么的都是一樣的,把別人的博客copy過來,也不管代碼到底能不能正常運行,因此寫了這篇文章,希望可以幫助正卡在tesseract這的盆友(說的大義凜然)
對tess4j的使用總結
1,tess4j 封裝了 tesseract-ocr 的操作
可以用很簡潔的幾行代碼就實現原本tesseract-ocr 復雜的實現邏輯
如果你也想了解tesseract-ocr是怎么實現驗證碼識別的請移步我的另一篇文章
2,網上有很多說發布jar或war包之后需要自己加載dll,這是錯誤的
不需要再自己加載dll,tess4j已經自己封裝了加載dll的操作
3,使用tess4j需要先安裝 tesseract-ocr-setup-3.02.02
4,如果拋Invalid memory access無效的內存訪問異常,導致這個異常的原因是tessdata語言包的位置沒有找到
5,下面就是我使用tess4j的一個使用demo
目錄結構
tessdata 語言包放在了和src同級的目錄
maven 依賴
1 <dependencies> 2 3 <dependency> 4 <groupId>net.java.dev.jna</groupId> 5 <artifactId>jna</artifactId> 6 <version>4.2.1</version> 7 </dependency> 8 9 <dependency> 10 <groupId>net.sourceforge.tess4j</groupId> 11 <artifactId>tess4j</artifactId> 12 <version>2.0.1</version> 13 <exclusions> 14 <exclusion> 15 <groupId>com.sun.jna</groupId> 16 <artifactId>jna</artifactId> 17 </exclusion> 18 </exclusions> 19 </dependency> 20 21 </dependencies>
3,測試代碼
1 package com.sinosoft.ocr;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 import javax.imageio.ImageIO;
10
11 import net.sourceforge.tess4j.ITesseract;
12 import net.sourceforge.tess4j.Tesseract;
13 import net.sourceforge.tess4j.TesseractException;
14 import net.sourceforge.tess4j.util.ImageHelper;
15
16 public class OcrTest {
17
18 public static void main(String[] args) {
19 File imageFile = new File("E:\valimg\fx\fx.tif");
20 ITesseract instance = new Tesseract(); // JNA Interface Mapping
21
22 try {
23 //讀取一個文件夾下的所有圖片并驗證
24 /* String[] filelist = imageFile.list();
25 for (int i = 0; i < filelist.length; i++) {
26 File readfile = new File("E:\valimg" + "\" + filelist[i]);
27 if (!readfile.isDirectory()) {
28 System.out.println("path=" + readfile.getPath());
29 System.out.println("absolutepath="
30 + readfile.getAbsolutePath());
31 System.out.println("name=" + readfile.getName());
32
33 String result = instance.doOCR(readfile);
34 //String result = instance.doOCR(change(readfile));
35 System.err.println(readfile.getName() +" result:"+ result);
36 }
37 }*/
38 instance.setLanguage("chi_sim"); //加載語言包
39 String result = instance.doOCR(imageFile);
40
41 System.err.println(imageFile.getName() +" result:"+ result);
42
43 } catch (TesseractException e) {
44 System.err.println(e.getMessage());
45 }
46 }
47
48 public static BufferedImage change(File file){
49
50 // 讀取圖片字節數組
51 BufferedImage textImage = null;
52 try {
53 InputStream in = new FileInputStream(file);
54 BufferedImage image = ImageIO.read(in);
55 textImage = ImageHelper.convertImageToGrayscale(ImageHelper.getSubImage(image, 0, 0, image.getWidth(), image.getHeight())); //對圖片進行處理
56 textImage = ImageHelper.getScaledInstance(image, image.getWidth() * 5, image.getHeight() * 5); //將圖片擴大5倍
57
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61
62 return textImage;
63 }
64 }
如果是web項目,需要指定 instance.setDatapath("E:\ocr\tesseract"); //tessdata 的目錄為E:\ocr esseract essdata,如果不指定也會拋Invalid memory access 異常
總結
以上是生活随笔為你收集整理的ocr智能图文识别 tess4j 图文,验证码识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AJAX范例大搜罗
- 下一篇: vue中的适配:px2rem