腾讯云API OCR个人身份证正反面文字信息识别提取

在线调试,界面上也有代码,直接copy即可,网址如下
登录 - 腾讯云

secretId和secretKey管理界面如下,url地址如下
登录 - 腾讯云

代码实现如下
1 pom.xml

复制代码
        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java-common</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java-ocr</artifactId>
            <version>LATEST</version>
        </dependency>

2 yml配置如下

复制代码
tencent:
  cloud:
    secretId: AKIDfBwSxKSKyOl24sRkARUUMzTkmYPxxxxx
    secretKey: 3tVs0tY0FG3bs0tWt1uZzKTOxxxxxxxx
    region: ap-beijing

3 属性注入

复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "tencent.cloud")
public class TencentCloudProperties {

    private String secretId;
    private String secretKey;
    private String region;

}

4 调用测试

复制代码
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;

@RestController
public class Controller_3 {

    @Autowired
    private TencentCloudProperties tencentCloudProperties;

    @RequestMapping("/idCardOcr")
    public Object idCardOcr(@RequestPart("file") MultipartFile file) {
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return "文件不能为空";
            }

            // 图片转换base64格式字符串
            BufferedImage image = ImageIO.read(file.getInputStream());
            // 将图像转换为字节
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", baos);
            byte[] imageBytes = baos.toByteArray();

            // 使用Base64进行编码
            String fileBase64 = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);

            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
            Credential cred = new Credential(tencentCloudProperties.getSecretId(),
                    tencentCloudProperties.getSecretKey());
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, tencentCloudProperties.getRegion(), clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            IDCardOCRRequest req = new IDCardOCRRequest();
            // 设置文件
            req.setImageBase64(fileBase64);

            // 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
            IDCardOCRResponse resp = client.IDCardOCR(req);
            System.out.println("resp = " + resp);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }
}
相关推荐
ZStack开发者社区38 分钟前
替代VMware vSAN | 五大角度解析ZStack分布式存储替代优势
分布式·云计算
百度智能云技术站7 小时前
突破显存瓶颈:基于 DeepSeek-V3.2-Exp 的 Latent Cache 卸载预取方案设计与模拟验证
云计算
wangmengxxw8 小时前
微服务-服务配置
java·运维·微服务·云计算·服务配置
DashVector9 小时前
如何通过HTTP API删除Doc
大数据·后端·云计算
翼龙云_cloud13 小时前
阿里云渠道商:无影云电脑常见问题及其解决方法有哪些?
运维·服务器·阿里云·云计算·电脑
智元视界13 小时前
智慧金融智能风控:AI算法如何服务小微企业
人工智能·金融·架构·云计算·数字化转型·产业升级
wanhengidc13 小时前
巨 椰 云手机功能丰富 高性价比
运维·服务器·科技·智能手机·云计算
峰顶听歌的鲸鱼13 小时前
13.docker部署
linux·运维·笔记·docker·容器·云计算
热爱跑步的恒川14 小时前
OpenEuler自带的iSulad容器引擎,性能表现怎么样?
云计算
周杰伦_Jay14 小时前
【免费云平台部署指南】按场景选型+全维度对比(附直达地址)
网络·人工智能·架构·开源·云计算