com.hn.ocr.qcloud.QcloudOcr Maven / Gradle / Ivy
package com.hn.ocr.qcloud;
import com.hn.config.HnConfigUtils;
import com.hn.config.exception.ConfigException;
import com.hn.ocr.Ocr;
import com.hn.utils.AssertUtils;
import com.qcloud.image.ImageClient;
import com.qcloud.image.exception.AbstractImageException;
import com.qcloud.image.request.*;
import java.io.File;
/**
* 腾讯云图片内容转化文字配置
*
* @author yang
* 2020-1-3
*/
public class QcloudOcr implements Ocr {
/**
* 配置前缀名
*/
private final String CONFIG_KEY = "tencent.imageText";
/**
* 静态代码块进行读取数据库配置
*/ {
String appId = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(".appId")),
ConfigException.exception("appId未配置"));
String secretId = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(".secretId")),
ConfigException.exception("secretId未配置"));
String secretKey = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(".secretKey")),
ConfigException.exception("secretKey未配置"));
String bucketName = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(".bucketName")),
ConfigException.exception("bucketName未配置"));
APP_ID = appId;
SECRET_ID = secretId;
SECRET_KEY = secretKey;
BUCKET_NAME = bucketName;
}
/**
* appid
*/
private final String APP_ID;
/**
* secretid
*/
private final String SECRET_ID;
/**
* secretkey
*/
private final String SECRET_KEY;
/**
* bucketname
*/
private final String BUCKET_NAME;
private final ImageClient IMAGECLIENT = new ImageClient(APP_ID, SECRET_ID, SECRET_KEY, ImageClient.NEW_DOMAIN_recognition_image_myqcloud_com);
public QcloudOcr() {}
/**
* 方法描述:识别身份证URL方式.
* 创建时间:2018-12-19 11:54:36
*
* @param url 图片地址
* @param cardType 0正面,1反面
* @return the string
* @author "yang"
*/
public String ocrIdCardUrl(String url, Integer cardType) {
String ret = null;
String[] idcardUrlList = new String[]{url};
IdcardDetectRequest idReq = new IdcardDetectRequest(BUCKET_NAME, idcardUrlList, cardType);
try {
ret = IMAGECLIENT.idcardDetect(idReq);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:识别身份证path方式.
* 创建时间:2018-12-19 11:54:36
*
* @param path 图片路径
* @param cardType 0正面,1反面
* @return the string
* @author "yang"
*/
public String ocrIdCardPath(String path, Integer cardType) {
String ret = null;
File[] idcardUrlList = new File[]{new File(path)};
IdcardDetectRequest idReq = new IdcardDetectRequest(BUCKET_NAME, idcardUrlList, cardType);
try {
ret = IMAGECLIENT.idcardDetect(idReq);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:驾驶证识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param url 图片地址
* @return the string
* @author "yang"
*/
public String ocrDrivingLicenceUrl(String url) {
String ret = null;
OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_DRIVER_LICENSE, url);
try {
ret = IMAGECLIENT.ocrDrivingLicence(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:驾驶证识别path方式.
* 创建时间:2018-12-19 12:52:27
*
* @param path 图片路径
* @return the string
* @author "yang"
*/
public String ocrDrivingLicencePath(String path) {
String ret = null;
File file = new File(path);
OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_DRIVER_LICENSE, file);
try {
ret = IMAGECLIENT.ocrDrivingLicence(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:行驶证识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param url 图片地址
* @return the string
* @author "yang"
*/
public String ocrVehicleLicenceUrl(String url) {
String ret = null;
OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_VEHICLE_LICENSE, url);
try {
ret = IMAGECLIENT.ocrDrivingLicence(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:行驶证识别path方式.
* 创建时间:2018-12-19 12:52:27
*
* @param path 图片路径
* @return the string
* @author "yang"
*/
public String ocrVehicleLicencePath(String path) {
String ret = null;
File file = new File(path);
OcrDrivingLicenceRequest request = new OcrDrivingLicenceRequest(BUCKET_NAME, OcrDrivingLicenceRequest.TYPE_VEHICLE_LICENSE, file);
try {
ret = IMAGECLIENT.ocrDrivingLicence(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:车牌识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param url 图片地址
* @return the string
* @author "yang"
*/
public String ocrPlateUrl(String url) {
String ret = null;
OcrPlateRequest request = new OcrPlateRequest(BUCKET_NAME, url);
try {
ret = IMAGECLIENT.ocrPlate(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:车牌识别path方式.
* 创建时间:2018-12-19 12:52:27
*
* @param path 图片路径
* @return the string
* @author "yang"
*/
public String ocrPlatePath(String path) {
String ret = null;
File file = new File(path);
OcrPlateRequest request = new OcrPlateRequest(BUCKET_NAME, file);
try {
ret = IMAGECLIENT.ocrPlate(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:营业执照识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param url 图片地址
* @return the string
* @author "yang"
*/
public String ocrBizLicenseUrl(String url) {
String ret = null;
OcrBizLicenseRequest request = new OcrBizLicenseRequest(BUCKET_NAME, url);
try {
ret = IMAGECLIENT.ocrBizLicense(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:营业执照识别path方式.
* 创建时间:2018-12-19 12:52:27
*
* @param path 图片路径
* @return the string
* @author "yang"
*/
public String ocrBizLicensePath(String path) {
String ret = null;
File file = new File(path);
OcrBizLicenseRequest request = new OcrBizLicenseRequest(BUCKET_NAME, file);
try {
ret = IMAGECLIENT.ocrBizLicense(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:银行卡识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param url 图片地址
* @return the string
* @author "yang"
*/
public String ocrBankCardUrl(String url) {
String ret = null;
OcrBankCardRequest request = new OcrBankCardRequest(BUCKET_NAME, url);
try {
ret = IMAGECLIENT.ocrBankCard(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:银行卡识别path方式.
* 创建时间:2018-12-19 12:52:27
*
* @param path 图片路径
* @return the string
* @author "yang"
*/
public String ocrBankCardPath(String path) {
String ret = null;
File file = new File(path);
OcrBankCardRequest request = new OcrBankCardRequest(BUCKET_NAME, file);
try {
ret = IMAGECLIENT.ocrBankCard(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:通用印刷体识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param url 图片地址
* @return the string
* @author "yang"
*/
public String ocrGeneralUrl(String url) {
String ret = null;
GeneralOcrRequest request = new GeneralOcrRequest(BUCKET_NAME, url);
try {
ret = IMAGECLIENT.generalOcr(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
/**
* 方法描述:通用印刷体识别URL方式.
* 创建时间:2018-12-19 12:52:27
*
* @param path 图片路径
* @return the string
* @author "yang"
*/
public String ocrGeneralPath(String path) {
String ret = null;
File file = new File(path);
GeneralOcrRequest request = new GeneralOcrRequest(BUCKET_NAME, file);
try {
ret = IMAGECLIENT.generalOcr(request);
} catch (AbstractImageException e) {
e.printStackTrace();
}
return ret;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy