From 68ee18438e99995d2d108cccb36e75f77ea96d76 Mon Sep 17 00:00:00 2001 From: luodangjia <luodangjia> Date: 星期二, 21 一月 2025 17:36:08 +0800 Subject: [PATCH] 1 --- ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CommonController.java | 63 ++++++++++++++++++++++++++++++- 1 files changed, 60 insertions(+), 3 deletions(-) diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CommonController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CommonController.java index fddfde9..8866e3f 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CommonController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CommonController.java @@ -1,11 +1,16 @@ package com.ruoyi.system.controller; +import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.UrlDownloader; import com.ruoyi.common.core.utils.orc.OcrUtils; +import com.ruoyi.system.api.model.BusinessLicense; import com.ruoyi.system.api.model.IdCard; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -14,21 +19,73 @@ @RestController @RequestMapping("/common") +@Tag(name = "通用接口") public class CommonController { /** * ocr提取身份证信息 */ @GetMapping("/idCard") + @Operation(summary = "ocr提取身份证信息", description = "ocr提取身份证信息") public R<IdCard> idCard(@Parameter(name = "url", description = "图片地址") String url) { InputStream inputStream; try { inputStream = UrlDownloader.downloadAsStream(url); - JSONObject jsonObject = OcrUtils.idCard(inputStream); - IdCard idCard = JSONObject.parseObject(jsonObject.toJSONString(), IdCard.class); - return R.ok(idCard); + String jsonStr = OcrUtils.idCard(inputStream,"IdCard"); + if (StringUtils.isEmpty(jsonStr)){ + return R.fail(); + } + + JSONObject jsonObject = JSONObject.parseObject(jsonStr); + Integer statusCode = jsonObject.getInteger("statusCode"); + if (statusCode != 200) { + return R.fail("识别失败"); + } + JSONObject body = jsonObject.getJSONObject("body"); + JSONObject data = body.getJSONObject("Data"); + JSONArray subImages = data.getJSONArray("SubImages"); + if (subImages != null && !subImages.isEmpty()){ + JSONObject subImage = subImages.getJSONObject(0); + JSONObject kvInfo = subImage.getJSONObject("KvInfo"); + IdCard idCard = kvInfo.getObject("Data", IdCard.class); + return R.ok(idCard); + } } catch (Exception e) { throw new RuntimeException(e); } + return R.fail(); + } + + /** + * ocr提取营业执照信息 + */ + @GetMapping("/businessLicense") + @Operation(summary = "ocr提取营业执照信息", description = "ocr提取营业执照信息") + public R<BusinessLicense> businessLicense(@Parameter(name = "url", description = "图片地址") String url) { + InputStream inputStream; + try { + inputStream = UrlDownloader.downloadAsStream(url); + String jsonStr = OcrUtils.idCard(inputStream,"BusinessLicense"); + if (StringUtils.isEmpty(jsonStr)){ + return R.fail(); + } + JSONObject jsonObject = JSONObject.parseObject(jsonStr); + Integer statusCode = jsonObject.getInteger("statusCode"); + if (statusCode != 200) { + return R.fail("识别失败"); + } + JSONObject body = jsonObject.getJSONObject("body"); + JSONObject data = body.getJSONObject("Data"); + JSONArray subImages = data.getJSONArray("SubImages"); + if (subImages != null && !subImages.isEmpty()){ + JSONObject subImage = subImages.getJSONObject(0); + JSONObject kvInfo = subImage.getJSONObject("KvInfo"); + BusinessLicense businessLicense = kvInfo.getObject("Data", BusinessLicense.class); + return R.ok(businessLicense); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + return R.fail(); } } -- Gitblit v1.7.1