package com.ydtech.utils.baidu; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.ydtech.core.page.HttpResult; import com.ydtech.modules.ins.model.CardUserInfo; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; /** * @ClassName MultiObjectDetect * @Description: TODO * @Author * @Date 2021/6/25 **/ @Component public class MultiObjectDetect { @Autowired private Environment environment; @Autowired private static Environment staticEnvironment; @PostConstruct public void init() { staticEnvironment = environment; } public static HttpResult multiObjectDetect(String imgData) { Map ma = new HashMap<>(); // 请求url String url = staticEnvironment.getProperty("vehicle"); try { // 本地文件路径 imgData = imgData.substring(imgData.indexOf(",") + 1); String imgParam = URLEncoder.encode(imgData, "UTF-8"); String param = "image=" + imgParam; // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。 String accessToken = AuthService.getAuth(); String result = HttpUtil.post(url, accessToken, param); JSONObject ja = JSON.parseObject(result); String wordsResult = ja.getString("words_result"); CardUserInfo cardUserInfo = new CardUserInfo(); JSONObject jb = JSON.parseObject(wordsResult); cardUserInfo.setVIN(JSON.parseObject(jb.getString("车辆识别代号")).getString("words").toString()); cardUserInfo.setIssueDate(JSON.parseObject(jb.getString("发证日期")).getString("words")); String hpzl = JSON.parseObject(jb.getString("车辆类型")).getString("words"); if (hpzl != null && hpzl.equals("小型轿车")) { cardUserInfo.setPlateType("02"); } else { cardUserInfo.setPlateType("01"); } cardUserInfo.setMotorUsageTypeCode("8A"); cardUserInfo.setMotorTypeCode("A0"); if (!StringUtils.isEmpty(jb.getString("注册日期"))) { cardUserInfo.setRegisterDate(JSON.parseObject(jb.getString("注册日期")).getString("words")); } else if (!StringUtils.isEmpty(jb.getString("注册登记日期"))) { cardUserInfo.setRegisterDate(JSON.parseObject(jb.getString("注册登记日期")).getString("words")); } cardUserInfo.setEngine(JSON.parseObject(jb.getString("发动机号码")).getString("words")); cardUserInfo.setPlateNo(JSON.parseObject(jb.getString("号牌号码")).getString("words")); cardUserInfo.setBackOcrID(JSON.parseObject(jb.getString("品牌型号")).getString("words")); ma.put("carInfo", cardUserInfo); return HttpResult.ok("200", ma); } catch (Exception e) { e.printStackTrace(); } return HttpResult.ok("201", ma); } public static JSONObject request(String images, String vehicleLicenseSide) { // 请求url String url = staticEnvironment.getProperty("vehicle"); try { images = images.substring(images.indexOf(",") + 1); String imgParam = URLEncoder.encode(images, "UTF-8"); String param = "image=" + imgParam + "&vehicle_license_side=" + vehicleLicenseSide; // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。 String accessToken = AuthService.getAuth(); String result = HttpUtil.post(url, accessToken, param); JSONObject ja = JSON.parseObject(result); String wordsResult = ja.getString("words_result"); return JSON.parseObject(wordsResult); } catch (Exception e) { e.printStackTrace(); } return new JSONObject(); } public static void drivingPermitFront(String images, CardUserInfo cardUserInfo) { if (StringUtils.isEmpty(images)) { return; } try { JSONObject jb = request(images, "front"); cardUserInfo.setVIN(JSON.parseObject(jb.getString("车辆识别代号")).getString("words").toString()); cardUserInfo.setIssueDate(JSON.parseObject(jb.getString("发证日期")).getString("words")); //先获取使用性质 String useNature = JSON.parseObject(jb.getString("使用性质")).getString("words"); String hpzl = JSON.parseObject(jb.getString("车辆类型")).getString("words"); if(useNature != null && useNature.equals("非营运")) { //家用车 // if (hpzl != null && (hpzl.equals("小型轿车") || hpzl.equals("小型普通客车"))) { // //车辆类型 // cardUserInfo.setPlateType("02"); // //车辆种类 // cardUserInfo.set // } cardUserInfo.setPlateType("A0"); cardUserInfo.setCategory("K33"); cardUserInfo.setVehicleUse("05"); } else if(useNature != null && useNature.equals("货运")) { //货车 if(hpzl != null && hpzl.equals("轻型普通货车")) { cardUserInfo.setCategory("H31"); cardUserInfo.setPlateType("A1"); cardUserInfo.setVehicleUse("08"); }else{ //后期进行赛选 cardUserInfo.setCategory("H31"); cardUserInfo.setPlateType("A1"); cardUserInfo.setVehicleUse("08"); } } //判断使用性质 cardUserInfo.setUseNature(JSON.parseObject(jb.getString("使用性质")).getString("words")); cardUserInfo.setMotorUsageTypeCode("8A"); cardUserInfo.setMotorTypeCode("A0"); if (!StringUtils.isEmpty(jb.getString("注册日期"))) { cardUserInfo.setRegisterDate(JSON.parseObject(jb.getString("注册日期")).getString("words")); } else if (!StringUtils.isEmpty(jb.getString("注册登记日期"))) { cardUserInfo.setRegisterDate(JSON.parseObject(jb.getString("注册登记日期")).getString("words")); } cardUserInfo.setEngine(JSON.parseObject(jb.getString("发动机号码")).getString("words")); cardUserInfo.setPlateNo(JSON.parseObject(jb.getString("号牌号码")).getString("words")); cardUserInfo.setBackOcrID(JSON.parseObject(jb.getString("品牌型号")).getString("words")); cardUserInfo.setCarOwner(JSON.parseObject(jb.getString("所有人")).getString("words")); } catch (Exception e) { e.printStackTrace(); } } /** * 行驶证背面 * * @param images * @return */ public static void drivingPermitBack(String images, CardUserInfo cardUserInfo) { if (StringUtils.isEmpty(images)) { return; } try { JSONObject jb = request(images, "back"); cardUserInfo.setGrossMass(JSON.parseObject(jb.getString("总质量")).getString("words").replace("kg", "")); cardUserInfo.setApprovedPassengersCapacity(JSON.parseObject(jb.getString("核定载人数")).getString("words").replace("人", "")); cardUserInfo.setUnladenMass(JSON.parseObject(jb.getString("整备质量")).getString("words").replace("kg", "")); cardUserInfo.setLimitLoad(JSON.parseObject(jb.getString("核定载质量")).getString("words").replace("kg", "")); } catch (Exception e) { e.printStackTrace(); } } }