1、开通服务
免费试用
2、获取accesskay
AccessKeyId和AccessKeySecret 要同时复制保存下来 因为后面好像看不AccessKeySecret了
3.Api 参考
4、SDK示例
5、引入Maven依赖
XML
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr_api20210707</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
6、controller
java
package org.example.controller;
import cn.csliu.entity.IdCardRoot;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import java.io.InputStream;
@RestController
public class IDCartController {
public static Client createClient() throws Exception {
Config config = new Config()
.setAccessKeyId("你的AccessKeyID")
.setAccessKeySecret("你的AccessKeySercet");
// Endpoint 请参考 https://api.aliyun.com/product/ocr-api
//设置阿里云 OCR 服务的 API 终端地址。
config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
return new Client(config);
}
@SneakyThrows
@PostMapping("/ocr")
public Object getOcrInfo(@RequestParam("file") MultipartFile file) throws Exception {
Client client = IDCartController.createClient();
//将fille文件转化为stream流
InputStream bodyStream = file.getInputStream();
RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest();
recognizeIdcardRequest.setBody(bodyStream);
RecognizeIdcardResponse response = null;
String data = "";
IdCardRoot idCardRoot = null;
RuntimeOptions runtime = new RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
response = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
RecognizeIdcardResponseBody body = response.getBody();
data= body.getData();
idCardRoot = JSON.parseObject(data, IdCardRoot.class);
} catch (TeaException error) {
// 如有需要,请打印 error
Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
Common.assertAsString(error.message);
}
return idCardRoot;
}
}
7、entity 实现类
java
package org.example.entity;
import lombok.Data;
@Data
public class IdCardBack {
private IdCardDataBackData data;
}
java
package org.example.entity;
import lombok.Data;
/**
* @author mucd
*/
@Data
public class IdCardDataBackData {
/**
* 办理地址
*/
private String issueAuthority;
/**
* 生效-到期
*/
private String validPeriod;
}
java
package org.example.entity;
import lombok.Data;
/**
* @author mucd
*/
@Data
public class IdCardDataFaceData {
private String name; //姓名
private String sex; //性别
private String birthDate; //出生日期
private String ethnicity;//民族
private String address; //地址
private String idNumber;//身份证号码
}
java
package org.example.entity;
import lombok.Data;
@Data
public class IdCardDataType {
private IdCardFace face;
private IdCardBack back;
}
java
package org.example.entity;
import lombok.Data;
@Data
public class IdCardFace {
private IdCardDataFaceData data;
}
java
package org.example.entity;
import lombok.Data;
@Data
public class IdCardRoot {
private IdCardDataType data;
}