<!--百度云文字识别-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.11</version>
</dependency>
工具类
@Component
public class OcrUtil {
private static final OcrUtil INSTANCE = new OcrUtil();
@Resource
private BaiduProperties baiduProperties;
@PostConstruct
public void init() {
INSTANCE.baiduProperties = baiduProperties;
}
// 进行车牌识别的静态方法
public static String plateLicense(byte[] image) {
// 初始化一个AipOcr
String appId = INSTANCE.baiduProperties.getAppId();
String apiKey = INSTANCE.baiduProperties.getApiKey();
String secretKey = INSTANCE.baiduProperties.getSecretKey();
AipOcr client = new AipOcr(appId, apiKey, secretKey);
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<>();
options.put("multi_detect", "false");
// 调用接口
JSONObject res = client.plateLicense(image, options);
JSONObject res2 = res.getJSONObject("words_result");
return res2.getString("number");
}
}
配置类
@Data
@Configuration
@ConfigurationProperties(prefix = "baidu")
public class BaiduProperties {
private String appId;
private String apiKey;
private String secretKey;
}
controller层
@PostMapping("upload")
@ApiOperation("上传车牌")
public R<String> upload(MultipartFile file, Long parkId) throws IOException {
// 获取到的停车场ID是
log.info("获取到的停车场ID是{}", parkId);
// 获取车牌号
String number = OcrUtil.plateLicense(file.getBytes());
log.info("获取车牌号是{}", number);
// 进行车辆入场或出场
return recordService.insert(number, parkId);
}
# 百度AI配置
baidu:
app-id: 41987***
api-key: t2jFpXeS8bXkBoMK6q****
secret-key: 4uvUHyM3IaGINS6VwtDHruG********