From 043108188c9855aaee21c77967ec9502741195ce Mon Sep 17 00:00:00 2001 From: Miku <2634544095@qq.com> Date: Wed, 23 Aug 2023 19:42:04 +0800 Subject: [PATCH] =?UTF-8?q?properties=EF=BC=9A=E6=B7=BB=E5=8A=A0=E7=99=BE?= =?UTF-8?q?=E5=BA=A6=E4=BA=BA=E5=B7=A5=E6=99=BA=E8=83=BD=E8=BD=A6=E7=89=8C?= =?UTF-8?q?=E8=AF=86=E5=88=ABID=EF=BC=8CKEY=E3=80=82pom=EF=BC=9A=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E9=98=BF=E9=87=8C=E5=B7=B4=E5=B7=B4json=E5=BA=93?= =?UTF-8?q?=E3=80=82=E6=B7=BB=E5=8A=A0=E7=99=BE=E5=BA=A6AI=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E4=B8=9A=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 +- .../module/car/util/BaiDuProperties.java | 17 ++++ .../com/smart/module/car/util/BaiDuUtils.java | 79 +++++++++++++++++++ src/main/resources/application-dev.properties | 6 +- 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/smart/module/car/util/BaiDuProperties.java create mode 100644 src/main/java/com/smart/module/car/util/BaiDuUtils.java 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