腾讯云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());
        }

    }
}
相关推荐
小保CPP16 小时前
OpenCV C++基于CNN模型的场景文本检测(OCR)
c++·人工智能·opencv·计算机视觉·cnn·ocr·光学字符识别
szephyr18 小时前
腾讯云 ADP 智能体上线后响应变慢,先查 RAG 召回链路还是 Workflow 分支?诊断路径与排查清单
大数据·人工智能·腾讯云
开开心心就好19 小时前
文件查重软件批量删除重复文件释放空间
java·开发语言·随机森林·ocr·excel·音视频·最小二乘法
donoot19 小时前
《大话文渊慧典》:五
人工智能·aigc·ocr·pymupdf·paddleocr·文渊慧典·大话系列
开开心心就好20 小时前
文件批量重命名工具简单好用支持规则改名
java·开发语言·b树·ocr·excel·音视频·kmeans
疯狂敲代码的老刘1 天前
百度开源 Unlimited-OCR:一次识别整篇文档,告别逐行扫描时代
开源·ocr
余俊晖2 天前
再看多模态OCR视觉token裁剪思路-LayoutLite基于token的隐式布局分析
人工智能·ocr·多模态
love530love2 天前
Unlimited-OCR 部署运行(11/13):环境变量块超限导致 spawn 子进程崩溃
ocr
love530love2 天前
Unlimited-OCR 部署运行(12/13):RTX 3090 MoE triton autotune config 消除性能警告
ocr
love530love2 天前
Unlimited-OCR 部署运行(10/13):推理输出乱码根因——wheel RECORD sha256 审计
人工智能·ocr