package com.project.admin.controller.tool;
|
|
import com.project.common.config.ProjectConfig;
|
import com.project.common.core.domain.AjaxResult;
|
import com.project.common.utils.sign.Base64;
|
import com.tencentcloudapi.common.Credential;
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
import com.tencentcloudapi.common.profile.HttpProfile;
|
import com.tencentcloudapi.ocr.v20181119.OcrClient;
|
import com.tencentcloudapi.ocr.v20181119.models.*;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
|
|
@Slf4j
|
@RestController
|
@RequestMapping("/tool/ai")
|
public class AiController {
|
|
@Value("${secretId}")
|
private String secretId;
|
@Value("${secretKey}")
|
private String secretKey;
|
|
/**
|
* 行驶证识别
|
* @param imageUrl 图片
|
* @return 结果
|
*/
|
@GetMapping("/vehicleLicenseOCR")
|
@ApiOperation("行驶证识别")
|
public AjaxResult vehicleLicenseOCR(String imageUrl)
|
{
|
try{
|
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
|
Credential cred = new Credential(secretId, secretKey);
|
// 实例化一个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, "ap-beijing", clientProfile);
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
VehicleLicenseOCRRequest req = new VehicleLicenseOCRRequest();
|
//req.setImageUrl(imageUrl);
|
req.setImageBase64(imgbase64(imageUrl));
|
// 返回的resp是一个VehicleLicenseOCRResponse的实例,与请求对象对应
|
VehicleLicenseOCRResponse resp = client.VehicleLicenseOCR(req);
|
return AjaxResult.success("操作成功",VehicleLicenseOCRResponse.toJsonString(resp));
|
} catch (TencentCloudSDKException e) {
|
log.error("行驶证识别失败。",e);
|
return AjaxResult.error("操作失败",e.toString());
|
}
|
}
|
|
/**
|
* 身份证识别
|
* @param imageUrl 图片路径
|
* @return 记过
|
*/
|
@GetMapping("/IDCardOCR")
|
@ApiOperation("身份证识别")
|
public AjaxResult IDCardOCR(String imageUrl)
|
{
|
try{
|
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
|
Credential cred = new Credential(secretId, secretKey);
|
// 实例化一个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, "ap-beijing", clientProfile);
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
IDCardOCRRequest req = new IDCardOCRRequest();
|
// req.setImageUrl(imageUrl);
|
req.setImageBase64(imgbase64(imageUrl));
|
// 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
|
IDCardOCRResponse resp = client.IDCardOCR(req);
|
|
return AjaxResult.success("操作成功",IDCardOCRResponse.toJsonString(resp));
|
} catch (TencentCloudSDKException e) {
|
log.error("身份证识别失败。",e);
|
return AjaxResult.error(e.getMessage());
|
}
|
}
|
|
|
private String imgbase64(String fileName)
|
{
|
FileInputStream in=null;
|
try{
|
String filePath = ProjectConfig.getUploadPath() + fileName.substring(fileName.lastIndexOf('/'));
|
in=new FileInputStream(filePath);
|
byte[] data = new byte[in.available()];
|
in.read(data);
|
in.close();
|
return Base64.encode(data);
|
}catch (IOException e){
|
log.error("读取图片异常",e);
|
return "";
|
}
|
}
|
}
|