diff --git a/pom.xml b/pom.xml index 93b8801..7167043 100644 --- a/pom.xml +++ b/pom.xml @@ -106,7 +106,11 @@ - + + com.alibaba + fastjson + 1.2.73 + org.apache.poi diff --git a/src/main/java/com/smart/module/car/util/BaiDuProperties.java b/src/main/java/com/smart/module/car/util/BaiDuProperties.java new file mode 100644 index 0000000..a116b19 --- /dev/null +++ b/src/main/java/com/smart/module/car/util/BaiDuProperties.java @@ -0,0 +1,17 @@ +package com.smart.module.car.util; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 百度智能AI + */ +@Data +@ConfigurationProperties(prefix = "bai-du") +public class BaiDuProperties { + + private String appId; + private String apiKey; + private String accessKeySecret; + +} \ No newline at end of file diff --git a/src/main/java/com/smart/module/car/util/BaiDuUtils.java b/src/main/java/com/smart/module/car/util/BaiDuUtils.java new file mode 100644 index 0000000..6bb208c --- /dev/null +++ b/src/main/java/com/smart/module/car/util/BaiDuUtils.java @@ -0,0 +1,79 @@ +package com.smart.module.car.util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.baidu.aip.ocr.AipOcr; +import com.smart.common.util.SslUtils; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.HashMap; + +/** + * 百度智能AI工具类 + */ +@Component +@Configuration +@EnableConfigurationProperties({BaiDuProperties.class}) +public class BaiDuUtils { + + private final static Logger LOGGER = LoggerFactory.getLogger(BaiDuUtils.class); + + private BaiDuProperties baiDu; + + public BaiDuUtils(BaiDuProperties baiDu) { + this.baiDu = baiDu; + } + + private AipOcr client; + + @PostConstruct + public void init() { + try { + // 使用配置文件中的AppID、API Key和Access Key Secret创建AipOcr客户端 + client = new AipOcr(baiDu.getAppId(), baiDu.getApiKey(), baiDu.getAccessKeySecret()); + // 设置连接超时时间为2秒 + client.setConnectionTimeoutInMillis(2000); + // 设置Socket超时时间为60秒 + client.setSocketTimeoutInMillis(60000); + } catch (Exception e) { + LOGGER.error("百度智能AI初始化失败,{}", e.getMessage()); + } + } + + /** + * 车牌识别方法 + * 参数为本地图片路径 + */ + public String plateLicense(String image) { + try { + HashMap options = new HashMap<>(); + /** + * 是否检测多张车牌,默认为false + * 当置为true的时候可以对一张图片内的多张车牌进行识别 + */ + options.put("multi_detect", "true"); + // 忽略SSL证书验证 + SslUtils.ignoreSsl(); + // 调用百度智能AI的车牌识别接口,获取识别结果 + JSONObject res = client.plateLicense(image, options); + System.out.println(res.toString()); + // 从识别结果中解析出车牌号码 + Object result = res.get("words_result"); + JSONArray array = JSON.parseArray(result.toString()); + com.alibaba.fastjson.JSONObject object = JSON.parseObject(array.get(0).toString()); + Object number = object.get("number"); + return number.toString(); + }catch (Exception e){ + e.printStackTrace(); + return ""; + } + } + + +} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 7140c39..629d924 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -75,4 +75,8 @@ spring.mqtt.password = public spring.mqtt.url = tcp://10.136.7.105:1883 spring.mqtt.client.id = HDC-188 spring.mqtt.default.topic = topic -spring.mqtt.default.completionTimeout = 3000 \ No newline at end of file +spring.mqtt.default.completionTimeout = 3000 + +bai-du.appId = 37831547 +bai-du.apiKey = znwWRqlIGRC8wbD3VhhQtojo +bai-du.accessKeySecret = zseU22B9XbhESFXm5UYGEtEGGLVokGfW \ No newline at end of file