|
|
@@ -0,0 +1,173 @@
|
|
|
+package com.tsf.user.controller;
|
|
|
+
|
|
|
+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.IDCardOCRRequest;
|
|
|
+import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
|
|
|
+import com.tencentcloudapi.ocr.v20181119.models.ResidenceBookletOCRRequest;
|
|
|
+import com.tencentcloudapi.ocr.v20181119.models.ResidenceBookletOCRResponse;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.ui.Model;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+
|
|
|
+@RestController
|
|
|
+public class TenController {
|
|
|
+
|
|
|
+ @Value("${ten.authen.secretId}")
|
|
|
+ private String secretId;
|
|
|
+
|
|
|
+ @Value("${ten.authen.secretKey}")
|
|
|
+ private String secretKey;
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
|
|
+// // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
|
|
|
+// Credential cred = new Credential("AKIDRhgQt76oFPIwJPEm33mEQLzLRGn44oHT", "wc190bw1qPimcKuyUmpc9buzsY0FG3bw");
|
|
|
+// // 实例化一个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("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.jcrb.com%2Fn1%2Fimages%2Fjcrb1534%2Fpic_95841.jpg");
|
|
|
+// // 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
|
|
|
+// IDCardOCRResponse resp = null;
|
|
|
+// try {
|
|
|
+// resp = client.IDCardOCR(req);
|
|
|
+// } catch (TencentCloudSDKException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// // 输出json格式的字符串回包
|
|
|
+// System.out.println(IDCardOCRResponse.toJsonString(resp));
|
|
|
+
|
|
|
+ try{
|
|
|
+ // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
|
|
+ // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
|
|
|
+ Credential cred = new Credential("AKIDRhgQt76oFPIwJPEm33mEQLzLRGn44oHT", "wc190bw1qPimcKuyUmpc9buzsY0FG3bw");
|
|
|
+ // 实例化一个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对象
|
|
|
+ ResidenceBookletOCRRequest req = new ResidenceBookletOCRRequest();
|
|
|
+ req.setImageUrl("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.jcrb.com%2Fn1%2Fimages%2Fjcrb1534%2Fpic_95841.jpg");
|
|
|
+ // 返回的resp是一个ResidenceBookletOCRResponse的实例,与请求对象对应
|
|
|
+ ResidenceBookletOCRResponse resp = client.ResidenceBookletOCR(req);
|
|
|
+ // 输出json格式的字符串回包
|
|
|
+ System.out.println(ResidenceBookletOCRResponse.toJsonString(resp));
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ System.out.println(e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @ApiOperation(value = "身份证识别", notes = "传入")
|
|
|
+ @PostMapping(value = "/fileUpload/identityCard")
|
|
|
+ public String fileUploadIdCard(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ System.out.println("文件为空空");
|
|
|
+ }
|
|
|
+
|
|
|
+ InputStream in = null;
|
|
|
+ byte[] data = null;
|
|
|
+ try {
|
|
|
+ in = file.getInputStream();
|
|
|
+ data = new byte[in.available()];
|
|
|
+ in.read(data);
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ BASE64Encoder encoder = new BASE64Encoder();
|
|
|
+ //encoder.encode(data);
|
|
|
+ // 实例化一个认证对象,入参需要传入腾讯云账户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.setImageBase64(encoder.encode(data));
|
|
|
+ // 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
|
|
|
+ IDCardOCRResponse resp = null;
|
|
|
+ try {
|
|
|
+ resp = client.IDCardOCR(req);
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 输出json格式的字符串回包
|
|
|
+ System.out.println(IDCardOCRResponse.toJsonString(resp));
|
|
|
+ return IDCardOCRResponse.toJsonString(resp);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "户口本识别", notes = "传入")
|
|
|
+ @PostMapping(value = "/fileUpload/residenceBooklet")
|
|
|
+ public String fileUploadResidenceBooklet(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ System.out.println("文件为空空");
|
|
|
+ }
|
|
|
+
|
|
|
+ InputStream in = null;
|
|
|
+ byte[] data = null;
|
|
|
+ try {
|
|
|
+ in = file.getInputStream();
|
|
|
+ data = new byte[in.available()];
|
|
|
+ in.read(data);
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ BASE64Encoder encoder = new BASE64Encoder();
|
|
|
+ //encoder.encode(data);
|
|
|
+ // 实例化一个认证对象,入参需要传入腾讯云账户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对象
|
|
|
+ ResidenceBookletOCRRequest req = new ResidenceBookletOCRRequest();
|
|
|
+ req.setImageBase64(encoder.encode(data));
|
|
|
+ // 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
|
|
|
+ ResidenceBookletOCRResponse resp = null;
|
|
|
+ try {
|
|
|
+ resp = client.ResidenceBookletOCR(req);
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 输出json格式的字符串回包
|
|
|
+ System.out.println(ResidenceBookletOCRResponse.toJsonString(resp));
|
|
|
+ return ResidenceBookletOCRResponse.toJsonString(resp);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|