New file |
| | |
| | | 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 { |
| | | |
| | | /** |
| | | * <b>description</b> : |
| | | * <p>使用AK&SK初始化账号Client</p> |
| | | * |
| | | * @return Client |
| | | * @throws Exception |
| | | */ |
| | | public static com.aliyun.ocr_api20210707.Client createClient() throws Exception { |
| | | // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 |
| | | // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。 |
| | | com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() |
| | | // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 |
| | | .setAccessKeyId("LTAI5tFSeci96NRF6p6UN69G") |
| | | // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 |
| | | .setAccessKeySecret("25t5zmWkueoQd81Zka70uPKRPgaMiV"); |
| | | // Endpoint 请参考 https://api.aliyun.com/product/ocr-api |
| | | config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com"; |
| | | return new com.aliyun.ocr_api20210707.Client(config); |
| | | } |
| | | |
| | | /** |
| | | * 证件识别 |
| | | */ |
| | | public static JSONObject idCard(InputStream bodyStream) throws Exception { |
| | | com.aliyun.ocr_api20210707.Client client = createClient(); |
| | | // 需要安装额外的依赖库,直接点击下载完整工程即可看到所有依赖。 |
| | | RecognizeAllTextRequest recognizeAllTextRequest = new RecognizeAllTextRequest() |
| | | .setBody(bodyStream) |
| | | .setType("IdCard") |
| | | .setOutputFigure(true); |
| | | RuntimeOptions runtime = new RuntimeOptions(); |
| | | try { |
| | | // 复制代码运行请自行打印 API 的返回值 |
| | | RecognizeAllTextResponse recognizeAllTextResponse = client.recognizeAllTextWithOptions(recognizeAllTextRequest, runtime); |
| | | 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 |
| | | System.out.println(error.getMessage()); |
| | | // 诊断地址 |
| | | System.out.println(error.getData().get("Recommend")); |
| | | com.aliyun.teautil.Common.assertAsString(error.message); |
| | | } catch (Exception _error) { |
| | | TeaException error = new TeaException(_error.getMessage(), _error); |
| | | // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 |
| | | // 错误 message |
| | | System.out.println(error.getMessage()); |
| | | // 诊断地址 |
| | | System.out.println(error.getData().get("Recommend")); |
| | | com.aliyun.teautil.Common.assertAsString(error.message); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | try { |
| | | InputStream inputStream = UrlDownloader.downloadAsStream("https://ai.bdstatic.com/file/3C8C5B451BB4445697730217EC8648E3"); |
| | | idCard(inputStream); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |