该篇是腾讯云收费 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
希望对大伙们能有所帮助.
本人学疏才浅, 如有差错, 望请指正.
来源: http://www.bubuko.com/infodetail-3329664.html