P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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;
    }
 
}