入门参考网址:https://help.aliyun.com/document_detail/294533.html 两种调用方式:HTTP调用方式,SDK,推荐用SDK,更加简单,方法都是封装好的,本文采用此方式。 SDK方式参考链接: https://next.api.aliyun.com/api/ocr-api/2021-07-07 https://next.api.aliyun.com/api/ocr-api/2021-07-07/RecognizeIdcard?sdkStyle=dara¶ms={}
先要引入SDK
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr_api20210707</artifactId>
<version>1.1.2</version>
</dependency>
代码
官网代码不是很完整,自己整理了下 注意设置输入源图片的方法需要自己看下recognizeIdcardRequest的方法
// This file is auto-generated, don't edit it. Thanks.
package app;
import com.aliyun.tea.*;
import com.aliyun.ocr_api20210707.*;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
return new com.aliyun.ocr_api20210707.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.ocr_api20210707.Client client = Sample.createClient("你自己的accessKeyId", "你自己的accessKeySecret");
//创建请求
RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest();
// url方式
// recognizeIdcardRequest.setUrl("https://img.alicdn.com/tfs/TB1q5IeXAvoK1RjSZFNXXcxMVXa-483-307.jpg");
// 二进制文件流方式
recognizeIdcardRequest.setBody(new BufferedInputStream(new FileInputStream("C:\\Users\\Desktop\\杂七杂八\\傻猫.png")));
// 返回值
RecognizeIdcardResponse recognizeIdcardResponse = client.recognizeIdcard(recognizeIdcardRequest);
System.out.println(recognizeIdcardResponse);
}
}
结果
debug一下结果 就是这个傻猫
|