赞
踩
参考 https://gitee.com/GCSZHN/ddddocr-for-java?_from=gitee_search java 11
改造使用 jdk1.8
https://gitee.com/bestman_456/ddddocr4j
引入依赖
<dependency>
<groupId>com.yl</groupId>
<artifactId>ddddocr4j</artifactId>
<version>1.0.0</version>
</dependency>
DDDDOcrUtil
package com.yl.util; import com.yl.ddddocr.OCREngine; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Base64; /** * DDDDOcrUtil * * @author liuxb * @date 2023/4/28 0:09 */ @Slf4j public class DDDDOcrUtil { private static final OCREngine ocrEngine = OCREngine.instance(); /** * 获取识别的验证码 * * @param base64 验证码base64字符串 * @return */ public static String getCode(String base64) { byte[] bytes = Base64.getDecoder().decode(base64); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); try { BufferedImage bufferedImage = ImageIO.read(bis); return ocrEngine.recognize(bufferedImage); } catch (IOException e) { log.error("识别验证码异常", e); return StringUtils.EMPTY; } } /** * 获取识别的验证码 * * @param inputStream 验证码图片输入流 * @return */ public static String getCode(InputStream inputStream) { try { BufferedImage bufferedImage = ImageIO.read(inputStream); return ocrEngine.recognize(bufferedImage); } catch (IOException e) { log.error("识别验证码异常", e); return StringUtils.EMPTY; } } }
安装包 npm install ddddocr 安装的包,报错,需要 修改 ddddocr/dist/index.js 最后一行为 module.exports = DdddOcr; 测试 const Ddddocr = require('ddddocr') var fs = require('fs') //引入fs模块 var path = require('path') var imgDir = "D:/verifies" var array = fs.readdirSync(imgDir) array.forEach(item => { var file = path.join(imgDir, item) Ddddocr.create().then(async ddddocr => { // 也可传入 文件路径 const verifyCode = await ddddocr.classification(file); console.log(file, verifyCode) }) })
pip install ddddocr import os import ddddocr # common_old.onnx, beta=True common.onnx ocr = ddddocr.DdddOcr() # dirPath = r'D:\upload\img' dirPath = r'D:\verifies' for ff in os.listdir(dirPath): path = os.path.join(dirPath, ff) with open(path, 'rb') as f: img_bytes = f.read() res = ocr.classification(img_bytes) print('路径:' + path + ', 识别出的验证码为:' + res)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。