欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 腾讯云API OCR个人身份证正反面文字信息识别提取

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

2024/10/25 12:18:51 来源:https://blog.csdn.net/qq_41712271/article/details/141191734  浏览:    关键词:腾讯云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: AKIDfBwSxKSKyOl24sRkARUUMzTkmYPxxxxxsecretKey: 3tVs0tY0FG3bs0tWt1uZzKTOxxxxxxxxregion: 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 {@Autowiredprivate 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());}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com