|
@@ -2,20 +2,34 @@ package com.huimv.farm.damsubsidy.controller;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
import com.huimv.farm.damsubsidy.common.annotate.DictTranslate;
|
|
|
import com.huimv.farm.damsubsidy.common.utils.PovertyUtils;
|
|
|
+import com.huimv.farm.damsubsidy.common.utils.Result;
|
|
|
import com.huimv.farm.damsubsidy.common.utils.UploadImage;
|
|
|
import com.huimv.farm.damsubsidy.entity.PovertyUserRegist;
|
|
|
+import com.huimv.farm.damsubsidy.entity.vo.IDCardVo;
|
|
|
+import okhttp3.*;
|
|
|
+import okhttp3.RequestBody;
|
|
|
+import okhttp3.ResponseBody;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.Base64;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -73,4 +87,103 @@ public class ImagesTest {
|
|
|
return "成功";
|
|
|
}*/
|
|
|
|
|
|
+ /* public static void main(String []args) throws IOException, JSONException {
|
|
|
+ MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
|
|
+ // image 可以通过 getFileContentAsBase64("C:\fakepath\039fc6cbebda8c7d48ed16b4bbf03d7.jpg") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + getAccessToken())
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("Content-Type", "application/x-www-form-urlencoded")
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
+ .build();
|
|
|
+ Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
+ System.out.println(response.body().string());
|
|
|
+
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static final String API_KEY = "tOWx25FyomcHf51HDQqQUhSq";
|
|
|
+ public static final String SECRET_KEY = "6MOI0wIGqstuFk3xO6D0nHYaKdvn6DPD";
|
|
|
+ static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping("/excelTest/import")
|
|
|
+ public Result evaluationUpdate(@RequestParam("image") MultipartFile file) throws Exception {
|
|
|
+
|
|
|
+ if (ObjectUtil.isEmpty(file)){
|
|
|
+// return ;
|
|
|
+ }
|
|
|
+ BASE64Encoder encoder = new BASE64Encoder();
|
|
|
+ String fileStr = encoder.encode(file.getBytes());
|
|
|
+ String image = URLEncoder.encode(fileStr, "utf-8");
|
|
|
+ MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
|
|
+ // image 可以通过 getFileContentAsBase64("C:\fakepath\039fc6cbebda8c7d48ed16b4bbf03d7.jpg") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
|
|
|
+ okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, "id_card_side=front&image="+image+"&detect_risk=true&detect_quality=true&detect_photo=true&detect_card=true&detect_direction=true");
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + getAccessToken())
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("Content-Type", "application/x-www-form-urlencoded")
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
+ .build();
|
|
|
+ Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
+ String body1 = response.body().string();
|
|
|
+ IDCardVo idCardVo = JSONUtil.toBean(body1, IDCardVo.class);
|
|
|
+ Integer idcardNumberType = idCardVo.getIdcardNumberType();
|
|
|
+ if (idcardNumberType != 1){
|
|
|
+ String reslut = "";
|
|
|
+ switch (idcardNumberType){
|
|
|
+ case 1:
|
|
|
+
|
|
|
+ }
|
|
|
+ return new Result(10001,"",false);
|
|
|
+ }
|
|
|
+ System.out.println(idCardVo.getIdcardNumberType());
|
|
|
+ System.out.println(idCardVo.getWordsResult());
|
|
|
+
|
|
|
+// return "识别成功";
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件base64编码
|
|
|
+ *
|
|
|
+ * @param path 文件路径
|
|
|
+ * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
|
|
|
+ * @return base64编码信息,不带文件头
|
|
|
+ * @throws IOException IO异常
|
|
|
+ */
|
|
|
+ static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
|
|
|
+ byte[] b = Files.readAllBytes(Paths.get(path));
|
|
|
+ String base64 = Base64.getEncoder().encodeToString(b);
|
|
|
+ if (urlEncode) {
|
|
|
+ base64 = URLEncoder.encode(base64, "utf-8");
|
|
|
+ }
|
|
|
+ return base64;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从用户的AK,SK生成鉴权签名(Access Token)
|
|
|
+ *
|
|
|
+ * @return 鉴权签名(Access Token)
|
|
|
+ * @throws IOException IO异常
|
|
|
+ */
|
|
|
+ static String getAccessToken() throws IOException, JSONException {
|
|
|
+ MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
|
|
+ okhttp3.RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
|
|
|
+ + "&client_secret=" + SECRET_KEY);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url("https://aip.baidubce.com/oauth/2.0/token")
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("Content-Type", "application/x-www-form-urlencoded")
|
|
|
+ .build();
|
|
|
+ Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
+ return new JSONObject(response.body().string()).getString("access_token");
|
|
|
+ }
|
|
|
+
|
|
|
}
|