1
luodangjia
2025-01-21 f981c1f30461ef9a40a39338aa77e511ffc24d1f
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/orc/OcrUtils.java
@@ -1,13 +1,17 @@
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;
import java.io.InputStream;
import java.util.Map;
public class OcrUtils {
@@ -34,7 +38,7 @@
    /**
     * 证件识别
     */
    public static String idCard(InputStream bodyStream) throws Exception {
    public static JSONObject idCard(InputStream bodyStream) throws Exception {
        com.aliyun.ocr_api20210707.Client client = createClient();
        // 需要安装额外的依赖库,直接点击下载完整工程即可看到所有依赖。
        RecognizeAllTextRequest recognizeAllTextRequest = new RecognizeAllTextRequest()
@@ -45,7 +49,21 @@
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeAllTextResponse recognizeAllTextResponse = client.recognizeAllTextWithOptions(recognizeAllTextRequest, runtime);
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(recognizeAllTextResponse)));
            String jsonString = Common.toJSONString(TeaModel.buildMap(recognizeAllTextResponse));
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            Integer statusCode = jsonObject.getInteger("statusCode");
            if (statusCode != 200) {
                throw new RuntimeException("识别失败");
            }
            JSONObject body = jsonObject.getJSONObject("body");
            JSONObject data = body.getJSONObject("Data");
            JSONArray subImages = body.getJSONArray("SubImages");
            if (subImages != null && !subImages.isEmpty()){
                JSONObject subImage = subImages.getJSONObject(0);
                JSONObject kvInfo = subImage.getJSONObject("KvInfo");
                return kvInfo.getJSONObject("Data");
            }
            return null;
        } catch (TeaException error) {
            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            // 错误 message