这段时间做项目需要用java程序进行车牌识别,因此尝试做了下这个程序,因为调用了阿里云第三方的接口,所以大家需要花钱买下那个appcode,0.1元200次,程序识别度不是很高,但是根据自己的需要返回了相应的数据,大家可以先看下效果图:
程序中的方法如下:
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
public static void main(String[] args) throws Exception {
String host = "https://imgocr.market.alicloudapi.com";
String path = "/api/decode_hphm_aliyun";
String method = "POST";
String appcode = "需花0.1元买Appcode";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
//下面的是图片的来源
String basecode=base64.encodeBase64File("图片位置的来源,支持jpg和png格式");
String bodys ="{\"imgbase64\":"+"\""+basecode+"\""+"}";
try {HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
String jsonstr=EntityUtils.toString(response.getEntity());
//下面的是输出json字符串的格式
// System.out.println(jsonstr);
JSONObject jsonObj = JSONObject.fromObject(jsonstr);
String desc=jsonObj.getString("desc");
if(desc.equals("识别成功")){
JSONObject data =(JSONObject) jsonObj.get("data");
JSONArray jar=JSONArray.fromObject(data.get("hphm_list"));
for(int i=0;i<jar.size();i++){
JSONObject jsonItem =jar.getJSONObject(i);
System.out.println(jsonItem.get("hphm"));//如果车牌识别成功则返回车牌的字符串
}
}else{
System.out.println("车牌未能识别");//如果车牌未识别则返回车牌未能识别这几个字
}
} catch (Exception e) {
e.printStackTrace();
}
}
以上就是相关程序,如有什么疑问欢迎大家加我qq1027320814相互学习哈,希望帮助大家哈!