ZQN
2024-06-17 b1ff19545212508d3f65741ab889f0b6df82a511
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
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 "";
        }
    }
}