#安装环境依赖包括Leptonica
yum -y install automake libtool libtiff-devel libjpeg-devel libpng-devel
wget https://pkg-config.freedesktop.org/releases/pkg-config-0.29.tar.gz
tar -xvf pkg-config-0.29.tar.gz
cd pkg-config-0.29/
./configure --with-internal-glib
make
make check
make install
wget http://www.leptonica.org/source/leptonica-1.78.0.tar.gz
tar -zxvf leptonica-1.78.0.tar.gz
cd leptonica-1.78.0/
./configure
make && make install
#编辑系统变量,修改/etc/profile文件,
vim /etc/profile
#追加以下
export LD_LIBRARY_PATH=$LD_LIBRARY_PAYT:/usr/local/lib
export LIBLEPT_HEADERSDIR=/usr/local/include
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
#保存后运行,生效环境变量
source /etc/profile
#安装Tesseract
wget https://github.com/tesseract-ocr/tesseract/archive/refs/tags/5.2.0.tar.gz
tar -zxvf tesseract-5.2.0.tar.gz
cd tesseract-5.2.0/
./autogen.sh
./configure --with-extra-includes=/usr/local/include --with-extra-libraries=/usr/local/lib
make && make install
String imgSrc = "test.png";
File imgFile = new File(imgSrc);
StopWatch watch = new StopWatch();
watch.start();
System.out.println("开始解析");
//使用ImageIO读取图片对象
BufferedImage image = ImageIO.read(imgFile);
//创建tesseract对象
Tesseract tes = new Tesseract();
//语言包位置 根据实际环境修改替换
tes.setDatapath("D:\\tesseract\\tessdata");
//配置使用的语言 中文
tes.setLanguage("chi_sim");
//配置解析区域 x,y 起点即左上角坐标, w,h 区域的宽高
int x = 100;
int y = 100;
int w = 100;
int h = 100;
Rectangle rect = new Rectangle(x, y, w, h);
String imgText = tes.doOCR(image, rect);
System.out.println("解析结束,使用时间:"+watch.getTotalTimeSeconds()+"秒");
System.out.println("解析结果");
System.out.println(imgText);