腾讯云API OCR驾驶证 文字信息识别提取

和身份证识别,大部分代码都是一样的,可参考身份证识别的博文
腾讯云API OCR个人身份证正反面文字信息识别提取-CSDN博客

不一样的地方,如下

请求对象类型:
身份证:IDCardOCRRequest
驾驶证:DriverLicenseOCRRequest

响应对象类型:
身份证:IDCardOCRResponse
驾驶证:DriverLicenseOCRResponse

OCR方法调用:
身份证:client.IDCardOCR(req)
驾驶证:client.DriverLicenseOCR(req)

pom.xml,yml配置,属性注入,和身份证识别一模一样,调用的详细代码如下

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.DriverLicenseOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.DriverLicenseOCRResponse;
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_jdriverLicenseOcr {

    @Autowired
    private TencentCloudProperties tencentCloudProperties;

    @RequestMapping("/driverLicenseOcr")
    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对象
            DriverLicenseOCRRequest req = new DriverLicenseOCRRequest();
            req.setImageBase64(fileBase64);

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

    }
}
相关推荐
乙卯年QAQ2 天前
【腾讯云】腾讯云docker搭建单机hadoop
hadoop·docker·腾讯云
weixin_307779135 天前
从腾讯云数据仓库TCHouse安全地转移数据到AWS Redshift
数据仓库·python·云计算·腾讯云·aws
G.E.N.6 天前
本地部署大模型实现OCR识别
人工智能·深度学习·计算机视觉·语言模型·ocr·边缘计算
葡萄爱7 天前
12 款开源OCR发 PDF 识别框架
pdf·ocr
Anna_Tong11 天前
物联网边缘(Beta)离全面落地还有多远?
物联网·阿里云·边缘计算·腾讯云·智能制造
yuanlulu13 天前
昇腾环境ppstreuct部署问题记录
人工智能·深度学习·llm·ocr·ppstructure
微学AI13 天前
GPU算力平台|在GPU算力平台部署轻量级中文OCR项目(chineseocr_lite)
ocr·gpu算力
塞大花16 天前
PDF文件提取开源工具调研总结
pdf·ocr·paddlepaddle·paddle·pdf文件提取·pdf内容识别
路边草随风16 天前
paimon使用腾讯云cosn作为仓库存储的使用方式
java·大数据·flink·云计算·腾讯云
小俊是我17 天前
【实用的开源项目】使用服务器部署Wallos,轻松管理你繁杂的订阅服务
运维·服务器·docker·开源·云计算·腾讯云·开源软件