腾讯OCR-身份证识别API使用
2019-08-17 本文已影响0人
0小水瓶0
我们在开发的过程中需要实现很多我们不能完成的功能的时候,有些大佬公司就提供了非常好用的API供开发者使用,当然有些API的使用需要一些费用。我们在上传或者用扫描仪扫描身份证的时候该API会将身份证的基本信息都读取出来返回给我们,具体我们怎么使用该API,在步骤上做一些介绍,避免新手走弯路。
1、注册腾讯云账号
腾讯云的账号可以使用我们的QQ号直接登录,也可以用其他的方式进行登录,随自己的习惯来操作。
2、打开控制台
在控制台上搜索文字识别—>卡证文字识别—>身份证识别然后开通自己的业务。在开通后点击右上方的接口文档,当然也可以看看计费方式,目前是每个月1000次的免费使用次数。

3、打开接口文档
在打开接口文档后往下翻会看到开发者资源我们可以点击API Explorer在线调试体验一下。


在在线调试界面里我们可以点击右上角的"JAVA SDK使用说明"里面获取Java代码,在弹出的界面里找到通过源码安装并点击快速下载地址就可以Get到API的源码了。

4、返回在线调试界面
我们在参数参数设置界面需要点击查看密钥获取自己的密钥填到文本框中,右边的代码区域就会为参数填上对应的密钥。
5、将下载下来的Java项目导入到eclipse中
导入java项目的时候我们在eclipse里面选的是导入的是maven项目。
6、复制主函数
复制在线界面右边代码区的主函数代码到自己的创建的主函数中
package test;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRResponse;
public class MainApi
{
public static void main(String [] args) {
picToBase64 ptb=new picToBase64();
String img="XXXXXX";//图片地址
try{
Credential cred = new Credential("XXXXXX", "XXXXXX");//密钥
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
String params = "{\"ImageBase64\":\""+ptb.ImageToBase64(img)+"\"}";
GeneralBasicOCRRequest req = GeneralBasicOCRRequest.fromJsonString(params, GeneralBasicOCRRequest.class);
GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
System.out.println(GeneralBasicOCRRequest.toJsonString(resp));//打印返回值JSON
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}
7、编写图片转码代码
我们上传的身份证照片或者扫描得到的照片都需要将图片格式转为base64码来进行传输,但是我们不可能每次都输入图片的base64码,所以我们需要编写图片转码代码。
package test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
public class picToBase64 {
//图片转化成base64字符串
public static String ImageToBase64(String img){
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = img;//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}catch (IOException e){
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
//返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
//base64字符串转化成图片
public static boolean Base64ToImage(String imgStr){
//对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i){
if(b[i]<0){//调整异常数据
b[i]+=256;
}
}
//生成png图片
String imgFilePath = "E:\\photo\\new_timg.png";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}catch (Exception e){
return false;
}
}
//---------------------
//版权声明:本文为CSDN博主「吾行远方」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
//原文链接:https://blog.csdn.net/wxyf2018/article/details/90512236
}