下载tesseract-orc
https://github.com/tesseract-ocr/tesseract/tags
下载leptonica
wget http://www.leptonica.org/source/leptonica-1.78.0.tar.gz
解压leptonica
tar -xvf leptonica-1.78.0.tar.gz
配置编译安装leptonica
进文件夹
./configure
make
make install
安装automake和libtool
yum install automake
yum install libtool
配置leptonica环境变量
vi /etc/profile
export LD_LIBRARY_PATH=/usr/local/lib
export LIBLEPT_HEADERSDIR=/usr/local/include
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
source /etc/profile
安装tesseract-ocr
进文件夹
./autogen.sh
./configure
make
make install
编译的时候遇到问题
Your-compiler-does-not-have-the-necessary-C17-support
执行
yum install -y centos-release-scl
yum install devtoolset-8-gcc*
scl enable devtoolset-8 bash
重新编译
测试是否安装成功,执行
ldconfig
tesseract --version
将下载的语言包复制到/usr/local/share/tessdata
最新包
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.6.0</version>
</dependency>
springboot 解决 jna 找不到对应的文件
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.13.0</version>
</dependency>
demo
@PostMapping("/upload")
public String testFile(MultipartFile file){
try {
Tesseract instance = new Tesseract();
instance.setLanguage("eng+chi_sim");
instance.setHocr(false);
instance.setPageSegMode(ITessAPI.TessPageSegMode.PSM_AUTO_OSD);
instance.setOcrEngineMode(TessAPI.TessOcrEngineMode.OEM_LSTM_ONLY);
PDDocument document = PDDocument.load(file.getInputStream());
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 200, ImageType.RGB);
String result = instance.doOCR(bim);
return result;
} catch (IOException | TesseractException e) {
e.printStackTrace();
}
return "";
}