|
|
|
@ -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<String, String> 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 "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|