package foundation.ai; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Objects; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; 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.common.profile.Language; import com.tencentcloudapi.ocr.v20181119.OcrClient; import com.tencentcloudapi.ocr.v20181119.models.BankSlipOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.BankSlipOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.BizLicenseOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.BizLicenseOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.EnterpriseLicenseOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.EnterpriseLicenseOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.GeneralEfficientOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.GeneralEfficientOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.GeneralFastOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.GeneralFastOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.InstitutionOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.InstitutionOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.OrgCodeCertOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.OrgCodeCertOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.VatInvoiceOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.VatInvoiceOCRResponse; import com.tencentcloudapi.ocr.v20181119.models.VatInvoiceVerifyNewRequest; import com.tencentcloudapi.ocr.v20181119.models.VatInvoiceVerifyNewResponse; import foundation.ai.dao.VatInvoiceVerifyDao; import foundation.util.Util; import sun.misc.BASE64Encoder; public class TencentAIProvider extends AIProvider { private static Logger logger; public static String secretId = "AKIDud7LIyOXQ1LPBguqVtzqLaYEdlbATVGk"; public static String secretKey = "EBrFMDo8CzVkhPpypPikVzDBsHQHr9Av"; public static int TimeOut_Second_Write = 10; public static int TimeOut_Second_Read = 10; public static boolean Debug = true; static { logger = LogManager.getLogger(TencentAIProvider.class); } @Override protected String getName() { return "腾讯AI"; } @Override public OCRResult executeAction(Operator operator, File file) throws Exception { //1. 得到图片的Base64 String base64 = imageToBase64(file); if (base64 == null) { logger.error("无法对图片{}进行Base64编码,无法进行AI识别", file); return null; } //2. 创建OCR客户端 OcrClient client = createClient(); //3. 创建OCR请求 Object request = createRequest(operator, base64); //4. 发送请求,得到结果 String value = ""; try { value = send(operator, client, request); } catch(Exception e) { e.printStackTrace(); value = "fail"; } value = Util.ocrJson(value); //5. 返回结果 OCRResult result = new OCRResult(operator, value); return result; } @Override /** * 发票验真 */ public OCRResult executeAction(Operator operator, Object paramObject) throws Exception { boolean is_real = true; //1. 创建OCR客户端 OcrClient client = createClient(); //2. 创建OCR请求 Object request = createRequest(operator, paramObject); //3. 发送请求,得到结果 String value = ""; try { value = send(operator, client, request); } catch(Exception e) { e.printStackTrace(); value = e.getMessage(); is_real = false; } value = Util.ocrJson(value); //4. 返回结果 OCRResult result = new OCRResult(operator, value); result.setIs_real(is_real); return result; } private OcrClient createClient() { //1. credential Credential credential = new Credential(secretId, secretKey); //2. profile HttpProfile httpProfile = new HttpProfile(); httpProfile.setWriteTimeout(TimeOut_Second_Write); httpProfile.setReadTimeout(TimeOut_Second_Read); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); clientProfile.setDebug(Debug); clientProfile.setLanguage(Language.ZH_CN); //3. client OcrClient client = new OcrClient(credential, "ap-shanghai", clientProfile); return client; } private Object createRequest(Operator operator, String base64) { if (Operator.BizLicense == operator) { BizLicenseOCRRequest request = new BizLicenseOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.EnterpriseLicense == operator) { EnterpriseLicenseOCRRequest request = new EnterpriseLicenseOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.OrgCodeCert == operator) { OrgCodeCertOCRRequest request = new OrgCodeCertOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.Institution == operator) { InstitutionOCRRequest request = new InstitutionOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.IDCard == operator) { IDCardOCRRequest request = new IDCardOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.TextAccurate == operator) { GeneralAccurateOCRRequest request = new GeneralAccurateOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.TextCommon == operator) { GeneralEfficientOCRRequest request = new GeneralEfficientOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.TextFast == operator) { GeneralFastOCRRequest request = new GeneralFastOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.BankSlip == operator) { BankSlipOCRRequest request = new BankSlipOCRRequest(); request.setImageBase64(base64); return request; } else if (Operator.InvoiceIdentify == operator) { VatInvoiceOCRRequest request = new VatInvoiceOCRRequest(); request.setImageBase64(base64); return request; } return null; } private Object createRequest(Operator operator, Object paramObject) { if (Operator.InvoiceVerification == operator) { VatInvoiceVerifyNewRequest request = new VatInvoiceVerifyNewRequest(); VatInvoiceVerifyDao vatInvoiceVerify = (VatInvoiceVerifyDao) paramObject; request.setInvoiceCode(vatInvoiceVerify.getInvoiceCode()); request.setInvoiceNo(vatInvoiceVerify.getInvoiceNo()); request.setInvoiceDate(vatInvoiceVerify.getInvoiceDate()); // request.setInvoiceKind("04"); request.setCheckCode(vatInvoiceVerify.getAdditional()); request.setAmount(vatInvoiceVerify.getAmount()); return request; } return null; } private String send(Operator operator, OcrClient client, Object request) throws TencentCloudSDKException { String json = null; if (Operator.BizLicense == operator) { BizLicenseOCRResponse response = client.BizLicenseOCR((BizLicenseOCRRequest)request); json = BizLicenseOCRResponse.toJsonString(response); } else if (Operator.EnterpriseLicense == operator) { EnterpriseLicenseOCRResponse response = client.EnterpriseLicenseOCR((EnterpriseLicenseOCRRequest)request); json = EnterpriseLicenseOCRResponse.toJsonString(response); } else if (Operator.OrgCodeCert == operator) { OrgCodeCertOCRResponse response = client.OrgCodeCertOCR((OrgCodeCertOCRRequest)request); json = OrgCodeCertOCRResponse.toJsonString(response); } else if (Operator.Institution == operator) { InstitutionOCRResponse response = client.InstitutionOCR((InstitutionOCRRequest)request); json = InstitutionOCRResponse.toJsonString(response); } else if (Operator.IDCard == operator) { IDCardOCRResponse response = client.IDCardOCR((IDCardOCRRequest)request); json = IDCardOCRResponse.toJsonString(response); } else if (Operator.TextAccurate == operator) { GeneralAccurateOCRResponse response = client.GeneralAccurateOCR((GeneralAccurateOCRRequest)request); json = GeneralAccurateOCRResponse.toJsonString(response); } else if (Operator.TextCommon == operator) { GeneralEfficientOCRResponse response = client.GeneralEfficientOCR((GeneralEfficientOCRRequest)request); json = GeneralEfficientOCRResponse.toJsonString(response); } else if (Operator.TextFast == operator) { GeneralFastOCRResponse response = client.GeneralFastOCR((GeneralFastOCRRequest)request); json = GeneralFastOCRResponse.toJsonString(response); } else if (Operator.BankSlip == operator) { BankSlipOCRResponse response = client.BankSlipOCR((BankSlipOCRRequest)request); json = BankSlipOCRResponse.toJsonString(response); } else if (Operator.InvoiceIdentify == operator) { VatInvoiceOCRResponse response = client.VatInvoiceOCR((VatInvoiceOCRRequest)request); json = VatInvoiceOCRResponse.toJsonString(response); } else if (Operator.InvoiceVerification == operator) { VatInvoiceVerifyNewResponse response = client.VatInvoiceVerifyNew((VatInvoiceVerifyNewRequest) request); json = VatInvoiceOCRResponse.toJsonString(response); } return json; } private static String imageToBase64(File file) { byte[] data = null; InputStream in = null; try { //1. bytes in = new FileInputStream(file); data = new byte[in.available()]; in.read(data); in.close(); //2. encode BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(Objects.requireNonNull(data)); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }