当前位置:   article > 正文

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

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

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

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


代码实现如下
1 pom.xml

  1. <dependency>
  2. <groupId>com.tencentcloudapi</groupId>
  3. <artifactId>tencentcloud-sdk-java-common</artifactId>
  4. <version>LATEST</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.tencentcloudapi</groupId>
  8. <artifactId>tencentcloud-sdk-java-ocr</artifactId>
  9. <version>LATEST</version>
  10. </dependency>

 2 yml配置如下

  1. tencent:
  2. cloud:
  3. secretId: AKIDfBwSxKSKyOl24sRkARUUMzTkmYPxxxxx
  4. secretKey: 3tVs0tY0FG3bs0tWt1uZzKTOxxxxxxxx
  5. region: ap-beijing

3 属性注入

  1. import lombok.Data;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Data
  5. @Component
  6. @ConfigurationProperties(prefix = "tencent.cloud")
  7. public class TencentCloudProperties {
  8. private String secretId;
  9. private String secretKey;
  10. private String region;
  11. }

4 调用测试

  1. import com.tencentcloudapi.common.Credential;
  2. import com.tencentcloudapi.common.profile.ClientProfile;
  3. import com.tencentcloudapi.common.profile.HttpProfile;
  4. import com.tencentcloudapi.ocr.v20181119.OcrClient;
  5. import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
  6. import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestPart;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import javax.imageio.ImageIO;
  13. import java.awt.image.BufferedImage;
  14. import java.io.ByteArrayOutputStream;
  15. import java.util.Base64;
  16. @RestController
  17. public class Controller_3 {
  18. @Autowired
  19. private TencentCloudProperties tencentCloudProperties;
  20. @RequestMapping("/idCardOcr")
  21. public Object idCardOcr(@RequestPart("file") MultipartFile file) {
  22. try {
  23. // 检查文件是否为空
  24. if (file.isEmpty()) {
  25. return "文件不能为空";
  26. }
  27. // 图片转换base64格式字符串
  28. BufferedImage image = ImageIO.read(file.getInputStream());
  29. // 将图像转换为字节
  30. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  31. ImageIO.write(image, "png", baos);
  32. byte[] imageBytes = baos.toByteArray();
  33. // 使用Base64进行编码
  34. String fileBase64 = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);
  35. // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
  36. Credential cred = new Credential(tencentCloudProperties.getSecretId(),
  37. tencentCloudProperties.getSecretKey());
  38. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  39. HttpProfile httpProfile = new HttpProfile();
  40. httpProfile.setEndpoint("ocr.tencentcloudapi.com");
  41. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  42. ClientProfile clientProfile = new ClientProfile();
  43. clientProfile.setHttpProfile(httpProfile);
  44. // 实例化要请求产品的client对象,clientProfile是可选的
  45. OcrClient client = new OcrClient(cred, tencentCloudProperties.getRegion(), clientProfile);
  46. // 实例化一个请求对象,每个接口都会对应一个request对象
  47. IDCardOCRRequest req = new IDCardOCRRequest();
  48. // 设置文件
  49. req.setImageBase64(fileBase64);
  50. // 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
  51. IDCardOCRResponse resp = client.IDCardOCR(req);
  52. System.out.println("resp = " + resp);
  53. return resp;
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. throw new RuntimeException(e.getMessage());
  57. }
  58. }
  59. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/998454
推荐阅读
相关标签
  

闽ICP备14008679号