赞
踩
目前我测试只用到简体中文和英文,所以只下载了两个
简体中文下载
英文下载
把下载后的文件统一放到一个目录下
net.sourceforge.tess4j
tess4j
5.10.0
org.projectlombok
lombok
public static void main(String[] args) throws Exception {
ITesseract tesseract = new Tesseract();
// 设置训练集文件存储目录
tesseract.setDatapath("D:/traineddata");
// 设置引擎为中文简体 文件名不含后缀
tesseract.setLanguage("chi_sim");
String result = tesseract.doOCR(ImageIO.read(new File("D:\\test\\zh.png")));
System.out.println(result);
// 设置引擎为英文
tesseract.setLanguage("eng");
result = tesseract.doOCR(ImageIO.read(new File("D:\\test\\en.png")));
System.out.println(result);
}
识别结果如下
#ocr引擎存放路径
tess4j:
datapath: D:/traineddata
/** * @Description 自行扩展需要的OCR引擎语种 * @Author Dominick Li **/ @Getter @AllArgsConstructor public enum LanguageTypeEnum { CHINESE_SIMPLIFIED("chi_sim", "简体中文"), ENGLISH("eng", "英文"); private final String value; private final String language; /** * 根据语种查找枚举对象 * @param language 前端传的参数 * @return 没找到对应的则默认使用简单中文 */ public static LanguageTypeEnum getLanguageByType(String language) { for (LanguageTypeEnum languageTypeEnum : LanguageTypeEnum.values()) { if (languageTypeEnum.getValue().equals(language)) { return languageTypeEnum; } } return CHINESE_SIMPLIFIED; } }
@Data @Builder @AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class OcrResult { /** * 是否识别成功 */ public boolean success; /** * 识别时间 */ public long time; /** * 识别结果 */ public String[] texts; /** * 异常信息 */ public String msg; public static OcrResult success(String text, long time) { return OcrResult.builder() .success(true) .texts(text.split("\n")) .time(time) .build(); } public static OcrResult fail(String msg) { return OcrResult.builder() .success(false) .msg(msg) .build(); } }
@Slf4j @Service public class TesseractServer { @Value("${tess4j.datapath}") private String datapath; private final static Map SERVER_INSTANCE = new HashMap<>(); /** * 根据枚举配置的语种初始化Tesseract引擎 */ @PostConstruct public void init() { ITesseract iTesseract; for (LanguageTypeEnum languageTypeEnum : LanguageTypeEnum.values()) { iTesseract = new Tesseract(); //设置训练集文件存储目录 iTesseract.setDatapath(datapath); //设置语种 iTesseract.setLanguage(languageTypeEnum.getValue()); SERVER_INSTANCE.put(languageTypeEnum, iTesseract); log.info("load {} ocr model", languageTypeEnum.getLanguage()); } } /** * ocr识别 */ private OcrResult doOCR(ITesseract iTesseract, BufferedImage bufferedImage) throws Exception { String result = null; long startTime = System.currentTimeMillis(); result = iTesseract.doOCR(bufferedImage); long time = System.currentTimeMillis()-startTime; log.info("Time is: {} 毫秒", time); return OcrResult.success(result, time); } public OcrResult ocrImage(LanguageTypeEnum languageTypeEnum, File file) throws Exception { return doOCR(SERVER_INSTANCE.get(languageTypeEnum), ImageIO.read(file)); } public OcrResult ocrImage(LanguageTypeEnum languageTypeEnum, MultipartFile file) throws Exception { return ocrImage(languageTypeEnum, ImageIO.read(new ByteArrayInputStream(file.getBytes()))); } public OcrResult ocrImage(LanguageTypeEnum languageTypeEnum, BufferedImage bufferedImage) throws Exception { return doOCR(SERVER_INSTANCE.get(languageTypeEnum), bufferedImage); } }
@Slf4j @RestController @RequestMapping("/api") public class OcrController { @Resource private TesseractServer tesseractServer; /** * OCR识别 /ocr/chi_sim /ocr/eng * @param language 使用的模型语种 chi_sim=简体中文 eng=英文 * @param file 需要识别的图片 */ @PostMapping("/ocr/{language}") public OcrResult recognize(@PathVariable String language, MultipartFile file) { try { // 对图片进行文字识别 return tesseractServer.ocrImage(LanguageTypeEnum.getLanguageByType(language), file); } catch (Exception e) { log.error("error:{}", e.getMessage()); return OcrResult.fail(e.getMessage()); } } }
http://127.0.0.1:8036/api/ocr/chi_sim 中文引擎识别
http://127.0.0.1:8036/api/ocr/eng 英文引擎识别
到此基于Springboot框架搭建提供Ocr能力的Web服务就完成
创作不易,要是觉得我写的对你有点帮助的话,麻烦在gitee上帮我点下 Star
【SpringBoot框架篇】其它文章如下,后续会继续更新。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。