阅读 181

OCR技术之腾讯云产品

OCR技术之腾讯云产品

该篇是腾讯云收费OCR技术产品的使用方法:

一、所需依赖

<!--腾讯sdk-->
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.0.93</version>
</dependency>
<!--工具包-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

依赖版本可自行去maven仓库选择下载。

二、接口的使用

public class Tencentyun {

    public static void main(String[] args) throws TencentCloudSDKException {
        //backIdentify();
        //idCardIdentify();
    }

    /**
     * 银行卡识别
     * */
    private static void backIdentify() throws TencentCloudSDKException {
        // 获取 OcrClient
        OcrClient ocrClient = getOcrClient();
        // 参数为本地图片路径
        String filePath = "E://com//dream//begin//image//jpg//idCard//1_4.jpg";
        // BASE64加密图片
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(filePath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        String immageStr = encoder.encode(data);
        // 设置请求参数
        BankCardOCRRequest request = new BankCardOCRRequest();
        request.setImageBase64(immageStr);
        // 请求结果
        BankCardOCRResponse bankCardOCRResponse = ocrClient.BankCardOCR(request);
        HashMap<String, String> map = new HashMap<>();
        bankCardOCRResponse.toMap(map, "");
        System.out.println(JSONObject.toJSON(map));
    }

    /**
     * 身份证识别
     * */
    private static void idCardIdentify() throws TencentCloudSDKException {
        // 获取 OcrClient
        OcrClient ocrClient = getOcrClient();
        // 参数为本地图片路径
        String filePath = "E://com//dream//begin//image//jpg//idCard//1_4.jpg";
        // BASE64加密图片
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(filePath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        String immageStr = encoder.encode(data);
        // 设置请求参数
        IDCardOCRRequest request = new IDCardOCRRequest();
        request.setImageBase64(immageStr);
        // 设置身份证正反面(FRONT 正,BACK 反)
        request.setCardSide("FRONT");
        // 请求结果
        IDCardOCRResponse idCardOCRResponse = ocrClient.IDCardOCR(request);
        HashMap<String, String> map = new HashMap<>();
        idCardOCRResponse.toMap(map, "");
        System.out.println(JSONObject.toJSON(map));
    }

    /**
     * 获取OcrClient
     * */
    private static OcrClient getOcrClient() {
        // 产品密钥(开通服务获取)
        final String secretId = "开通服务获取";
        final String secretKey = "开通服务获取";
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
        Credential credential = new Credential(secretId, secretKey);
        OcrClient ocrClient = new OcrClient(credential, "根据自己所在地区选择,例如上海为:ap-shanghai", clientProfile);
        return ocrClient;
    }

}

腾讯云的接口调用,也是比较简单的。接口文档详情请见  https://cloud.tencent.com/document/product/866/36216

希望对大伙们能有所帮助。

本人学疏才浅,如有差错,望请指正。

来源https://www.cnblogs.com/zhizhixiaoxia/p/12035119.html

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐