SpringBoot中使用tess4j进行OCR(在macos上面开发)
问题
最近需要做OCR的实现,需要在Spring Boot工程中引入tess4j库,进行OCR识别。然后,这里使用macos m1进行开发。出现了找不到动态链接库的问题。主要就是找不到如下动态链接库:
- darwin-aarch64/libtesseract.dylib
 - darwin-aarch64/libleptonica.dylib
 
思路
在macos上面安装tesseract,tesseract-lang,leptonica这三个库,macos上面本地安装完成后,然后,把上面两个缺少的动态链接库文件,手动加入到tess4j的jar里面即可。
安装tesseract
# tesseract主要程序
brew install tesseract
# tesseract语音支持包,这里默认会把所有语言给安装上
brew install tesseract-lang
# 使用带中文文字的图片,手动测试即可
tesseract 0.png output -l chi_sim --psm 3 --oem 3
 
这里tesseract相关参数需要解释一下:
-l chi_sim:这是指定语言。这里指定的识别语音是中文简体;--psm 3:Page segmentation modes (PSM)页面分割模式,这里使用3是自动模式,默认也是3,自动模式。详细参数如下:
Page segmentation modes (PSM):0|osd_only                Orientation and script detection (OSD) only.1|auto_osd                Automatic page segmentation with OSD.2|auto_only               Automatic page segmentation, but no OSD, or OCR. (not implemented)3|auto                    Fully automatic page segmentation, but no OSD. (Default)4|single_column           Assume a single column of text of variable sizes.5|single_block_vert_text  Assume a single uniform block of vertically aligned text.6|single_block            Assume a single uniform block of text.7|single_line             Treat the image as a single text line.8|single_word             Treat the image as a single word.9|circle_word             Treat the image as a single word in a circle.10|single_char             Treat the image as a single character.11|sparse_text             Sparse text. Find as much text as possible in no particular order.12|sparse_text_osd         Sparse text with OSD.13|raw_line                Raw line. Treat the image as a single text line,bypassing hacks that are Tesseract-specific.
 
--oem 3:OCR Engine modes (OEM)OCR引擎模式,这里使用3为默认模式,即能用就用。详细如下:
OCR Engine modes (OEM):0|tesseract_only          Legacy engine only.1|lstm_only               Neural nets LSTM engine only.2|tesseract_lstm_combined Legacy + LSTM engines.3|default                 Default, based on what is available.
 
如果上面能够正常手动使用tesseract,进行OCR,那就说明tesseract安装没有问题,可以进行下一步了。
安装leptonica
brew install leptonica
 
这安装主要也是为了获得其中一个动态链接库。
springboot中使用tess4j
pom.xml
<dependency><groupId>net.sourceforge.tess4j</groupId><artifactId>tess4j</artifactId><version>5.16.0</version>
</dependency>
 
TesseractConfig.java
package com.xxxx.config;import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;import java.io.File;
import java.io.IOException;@Configuration
public class TesseractConfig {@Value("classpath:tessdata/")private Resource tessdataResource;@BeanTesseract tesseract() throws IOException {Tesseract tesseract = new Tesseract();File tessdataDir = tessdataResource.getFile();tesseract.setDatapath(tessdataDir.getPath()); //files of the example : https://github.com/tesseract-ocr/tessdatatesseract.setLanguage("chi_sim");tesseract.setOcrEngineMode(3);tesseract.setPageSegMode(3);return tesseract;}} 
ITesseractOCRService.java
package com.xxxx.service;import org.springframework.web.multipart.MultipartFile;/*** ocr识别服务*/
public interface ITesseractOCRService {/*** ocr识别* @param multipartFile 上传文件对象* @return ocr识别字符串*/String recognizeText(MultipartFile multipartFile);
} 
TesseractOCRServiceImpl.java
package com.xxxxservice.impl;import com.xxxx.exception.ServiceException;
import com.xxxx.service.ITesseractOCRService;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;@Service
@Slf4j
public class TesseractOCRServiceImpl implements ITesseractOCRService {@Resourceprivate Tesseract tesseract;@Overridepublic String recognizeText(MultipartFile multipartFile) {try {BufferedImage image = ImageIO.read(multipartFile.getInputStream());return tesseract.doOCR(image);} catch (TesseractException e) {log.error("OCR识别异常", e);throw new ServiceException("OCR识别异常");} catch (IOException e) {log.error("OCR读文件异常", e);throw new ServiceException("OCR读文件异常");}}
} 
OcrController.java
package com.xxxx.web.controller.system.api;import com.xxxx.system.service.ITesseractOCRService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;@RestController
@RequestMapping("/api/ocr")
public class OcrController {@Resourceprivate ITesseractOCRService iTesseractOCRService;@PostMapping("/test")public String recognizeText(@RequestParam("file") MultipartFile file) {return iTesseractOCRService.recognizeText(file);}
} 
改造tess4j jar文件
# 找到依赖库jar包位置
cd /Users/xxx/.m2/repository/net/sourceforge/tess4j/tess4j/5.16.0# 创建os架构对应的文件夹
mkdir darwin-aarch64
# 上传文件夹到jar内部
jar uf tess4j-5.16.0.jar darwin-aarch64# 复制libtesseract.dylib文件
cp /opt/homebrew/Cellar/tesseract/5.5.1/lib/libtesseract.5.dylib darwin-aarch64/libtesseract.dylib 
# 上传libtesseract.dylib文件到jar中
jar uf tess4j-5.16.0.jar darwin-aarch64/libtesseract.dylib # 复制libleptonica.dylib文件
cp /opt/homebrew/Cellar/leptonica/1.86.0/lib/libleptonica.6.dylib darwin-aarch64/libleptonica.dylib
# 上传libleptonica.dylib文件到jar中
jar uf tess4j-5.16.0.jar darwin-aarch64/libleptonica.dylib# 检查jar文件
jar tf tess4j-5.16.0.jar
 
最终jar文件如下:
META-INF/
META-INF/MANIFEST.MF
com/
com/recognition/
com/recognition/software/
com/recognition/software/jdeskew/
net/
net/sourceforge/
net/sourceforge/tess4j/
net/sourceforge/tess4j/util/
tessdata/
tessdata/configs/
win32-x86/
win32-x86-64/
META-INF/maven/
META-INF/maven/net.sourceforge.tess4j/
META-INF/maven/net.sourceforge.tess4j/tess4j/
com/recognition/software/jdeskew/ImageDeskew$HoughLine.class
com/recognition/software/jdeskew/ImageDeskew.class
com/recognition/software/jdeskew/ImageUtil.class
net/sourceforge/tess4j/ITessAPI$CANCEL_FUNC.class
net/sourceforge/tess4j/ITessAPI$EANYCODE_CHAR.class
net/sourceforge/tess4j/ITessAPI$ETEXT_DESC.class
net/sourceforge/tess4j/ITessAPI$TessBaseAPI.class
net/sourceforge/tess4j/ITessAPI$TessCancelFunc.class
net/sourceforge/tess4j/ITessAPI$TessChoiceIterator.class
net/sourceforge/tess4j/ITessAPI$TessMutableIterator.class
net/sourceforge/tess4j/ITessAPI$TessOcrEngineMode.class
net/sourceforge/tess4j/ITessAPI$TessOrientation.class
net/sourceforge/tess4j/ITessAPI$TessPageIterator.class
net/sourceforge/tess4j/ITessAPI$TessPageIteratorLevel.class
net/sourceforge/tess4j/ITessAPI$TessPageSegMode.class
net/sourceforge/tess4j/ITessAPI$TessParagraphJustification.class
net/sourceforge/tess4j/ITessAPI$TessPolyBlockType.class
net/sourceforge/tess4j/ITessAPI$TessProgressFunc.class
net/sourceforge/tess4j/ITessAPI$TessResultIterator.class
net/sourceforge/tess4j/ITessAPI$TessResultRenderer.class
net/sourceforge/tess4j/ITessAPI$TessTextlineOrder.class
net/sourceforge/tess4j/ITessAPI$TessWritingDirection.class
net/sourceforge/tess4j/ITessAPI$TimeVal.class
net/sourceforge/tess4j/ITessAPI.class
net/sourceforge/tess4j/ITesseract$RenderedFormat.class
net/sourceforge/tess4j/ITesseract.class
net/sourceforge/tess4j/OCRResult.class
net/sourceforge/tess4j/OSDResult.class
net/sourceforge/tess4j/TessAPI.class
net/sourceforge/tess4j/TessAPI1.class
net/sourceforge/tess4j/Tesseract$1.class
net/sourceforge/tess4j/Tesseract.class
net/sourceforge/tess4j/Tesseract1$1.class
net/sourceforge/tess4j/Tesseract1.class
net/sourceforge/tess4j/TesseractException.class
net/sourceforge/tess4j/util/Hocr2PdfParser$1.class
net/sourceforge/tess4j/util/Hocr2PdfParser.class
net/sourceforge/tess4j/util/ImageHelper.class
net/sourceforge/tess4j/util/ImageIOHelper.class
net/sourceforge/tess4j/util/LoadLibs.class
net/sourceforge/tess4j/util/LoggHelper.class
net/sourceforge/tess4j/util/PdfBoxUtilities$1.class
net/sourceforge/tess4j/util/PdfBoxUtilities$2.class
net/sourceforge/tess4j/util/PdfBoxUtilities.class
net/sourceforge/tess4j/util/PdfUtilities.class
net/sourceforge/tess4j/util/Utils.class
net/sourceforge/tess4j/Word.class
readme.html
tessdata/configs/alto
tessdata/configs/api_config
tessdata/configs/bazaar
tessdata/configs/digits
tessdata/configs/hocr
tessdata/configs/lstmbox
tessdata/configs/pdf
tessdata/configs/quiet
tessdata/configs/tsv
tessdata/configs/txt
tessdata/configs/unlv
tessdata/configs/wordstrbox
tessdata/eng.traineddata
tessdata/osd.traineddata
tessdata/pdf.ttf
versionchanges.txt
win32-x86/libtesseract551.dll
win32-x86-64/libtesseract551.dll
META-INF/maven/net.sourceforge.tess4j/tess4j/pom.xml
META-INF/maven/net.sourceforge.tess4j/tess4j/pom.properties
META-INF/INDEX.LIST
darwin-aarch64/
darwin-aarch64/libtesseract.dylib
darwin-aarch64/libleptonica.dylib
 
测试ocr效果

参考
- Tess4J Tesseract For Java » 5.16.0
 - tess4j
 - tessdata_best
 - 使用 tesseract 做 OCR 文字识别(Java)
 - Tess4j unsatisfied link error on mac OS X
 - Accessing Resource Files in Spring Boot: A Comprehensive Guide
 - Access a File from the Classpath in a Spring Application
 - Optical Character Recognition with Tesseract
 - OCR with Tesseract in Java: Converting Images to Text Made Easy
 
