diff --git a/yanzhu-modules/yanzhu-system/pom.xml b/yanzhu-modules/yanzhu-system/pom.xml index ad21b65d..d696ba6a 100644 --- a/yanzhu-modules/yanzhu-system/pom.xml +++ b/yanzhu-modules/yanzhu-system/pom.xml @@ -90,7 +90,10 @@ 3.6.2 compile - + + com.squareup.okhttp3 + okhttp + diff --git a/yanzhu-modules/yanzhu-system/src/main/java/com/yanzhu/system/controller/ToolsController.java b/yanzhu-modules/yanzhu-system/src/main/java/com/yanzhu/system/controller/ToolsController.java new file mode 100644 index 00000000..0c628d1b --- /dev/null +++ b/yanzhu-modules/yanzhu-system/src/main/java/com/yanzhu/system/controller/ToolsController.java @@ -0,0 +1,28 @@ +package com.yanzhu.system.controller; + +import com.yanzhu.common.core.web.controller.BaseController; +import com.yanzhu.common.core.web.domain.AjaxResult; +import com.yanzhu.system.service.impl.OcrService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/tools") +public class ToolsController extends BaseController { + + @Autowired + private OcrService ocrService; + + @GetMapping("/id/front") + public AjaxResult getFront(String url) { + return AjaxResult.success(ocrService.getFront(url)); + } + + @GetMapping("/id/back") + public AjaxResult getBack(String url) { + return AjaxResult.success(ocrService.getBack(url)); + } +} diff --git a/yanzhu-modules/yanzhu-system/src/main/java/com/yanzhu/system/service/impl/OcrService.java b/yanzhu-modules/yanzhu-system/src/main/java/com/yanzhu/system/service/impl/OcrService.java new file mode 100644 index 00000000..d6741870 --- /dev/null +++ b/yanzhu-modules/yanzhu-system/src/main/java/com/yanzhu/system/service/impl/OcrService.java @@ -0,0 +1,131 @@ +package com.yanzhu.system.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.yanzhu.common.core.web.domain.AjaxResult; +import com.yanzhu.common.redis.service.RedisService; +import com.yanzhu.system.controller.ToolsController; +import okhttp3.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +@Service +public class OcrService { + @Autowired + private RedisService redisService; + + final String TOKEN_URL="https://aip.baidubce.com/oauth/2.0/token"; + final String OCR_URL="https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"; + final String APP_ID = "6283230"; + final String APP_KEY="rS40xCCuGuVNFRopPI0jlMuj"; + final String APP_SECRET="3bY7dADqQq3O4UpXpFA1FJAj6LN57QCS"; + + + public String getToken() { + RequestBody body = new FormBody.Builder() + .add("grant_type", "client_credentials") + .add("client_id", APP_KEY) + .add("client_secret", APP_SECRET).build(); + Request request=new Request.Builder() + .url(TOKEN_URL) + .post(body) + .build(); + String result=getResult(request); + JSONObject jo=JSONObject.parseObject(result); + return jo.getString("access_token"); + } + public String getResult(Request request) { + OkHttpClient client = new OkHttpClient(); + Response response; + try { + response = client.newCall(request).execute(); + if (response.body() != null) { + return response.body().string(); + } else { + return ""; + } + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + + private String getJson(JSONObject data, String key) { + if(data!=null){ + JSONObject jo=data.getJSONObject(key); + if(jo!=null){ + String v=jo.getString("words"); + return v==null?"":v; + } + } + return ""; + } + + public Map getFront(String url){ + String token = getToken(); + RequestBody body = new FormBody.Builder() + .add("url", url) + .add("id_card_side", "front").build(); + Request request=new Request.Builder() + .url(OCR_URL+"?access_token="+token) + .post(body) + .build(); + String result=getResult(request); + JSONObject jo=JSONObject.parseObject(result); + JSONObject data=jo.getJSONObject("words_result"); + String name=getJson(data,"姓名"); + String nation=getJson(data,"民族"); + String address=getJson(data,"住址"); + String cardId=getJson(data,"公民身份号码"); + String birthDay=getJson(data,"出生"); + String sex=getJson(data,"性别"); + Map map=new HashMap<>(); + map.put("name",name); + map.put("nation",nation); + map.put("address",address); + map.put("cardId",cardId); + map.put("birthDay",birthDay); + map.put("sex",sex); + return map; + } + + public Map getBack(String url){ + String token = getToken(); + RequestBody body = new FormBody.Builder() + .add("url", url) + .add("id_card_side", "back").build(); + Request request=new Request.Builder() + .url(OCR_URL+"?access_token="+token) + .post(body) + .build(); + String result=getResult(request); + JSONObject jo=JSONObject.parseObject(result); + JSONObject data=jo.getJSONObject("words_result"); + String startDate=getJson(data,"签发日期"); + String endDate=getJson(data,"失效日期"); + String issuing=getJson(data,"签发机关"); + Map map=new HashMap<>(); + map.put("startDate",startDate); + map.put("endDate",endDate); + map.put("issuing",issuing); + return map; + } + public static void main(String[] args) { + //getFront(); + getBack(); + } + private static void getBack() { + String url="https://gss0.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/a8014c086e061d95103a068779f40ad162d9ca14.jpg"; + Map o= new OcrService().getBack(url); + System.out.printf(o.toString()); + } + private static void getFront() { + String url="http://62.234.3.186/statics/2025/01/19/4491f170cd1609142f9a6f097cbf681f_20250119000046A004.jpg"; + Map o= new OcrService().getFront(url); + System.out.printf(o.toString()); + } +}