1
luodangjia
2025-01-23 698ae84adaf1b8d0e8dd61d7279863fe17c1e81d
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/orc/OcrUtils.java
@@ -1,9 +1,12 @@
package com.ruoyi.common.core.utils.orc;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.ocr_api20210707.models.RecognizeAllTextRequest;
import com.aliyun.ocr_api20210707.models.RecognizeAllTextResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.ruoyi.common.core.utils.UrlDownloader;
@@ -34,18 +37,18 @@
    /**
     * 证件识别
     */
    public static String idCard(InputStream bodyStream) throws Exception {
    public static String idCard(InputStream bodyStream,String type) throws Exception {
        com.aliyun.ocr_api20210707.Client client = createClient();
        // 需要安装额外的依赖库,直接点击下载完整工程即可看到所有依赖。
        RecognizeAllTextRequest recognizeAllTextRequest = new RecognizeAllTextRequest()
                .setBody(bodyStream)
                .setType("IdCard")
                .setType(type)
                .setOutputFigure(true);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeAllTextResponse recognizeAllTextResponse = client.recognizeAllTextWithOptions(recognizeAllTextRequest, runtime);
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(recognizeAllTextResponse)));
            return Common.toJSONString(TeaModel.buildMap(recognizeAllTextResponse));
        } catch (TeaException error) {
            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            // 错误 message
@@ -68,8 +71,9 @@
    public static void main(String[] args) {
        try {
            InputStream inputStream = UrlDownloader.downloadAsStream("https://ai.bdstatic.com/file/3C8C5B451BB4445697730217EC8648E3");
            idCard(inputStream);
            InputStream inputStream = UrlDownloader.downloadAsStream("http://192.168.110.188:9300/statics/2025/01/22/862dab34c5f83ce2635d219c467ac9f6_20250122141611A119.jpg");
            String businessLicense = idCard(inputStream, "BusinessLicense");
            System.out.println(businessLicense);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }