ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

阿里云车牌识别

2021-12-07 10:33:05  阅读:219  来源: 互联网

标签:String parklogTable content 阿里 import new 识别 车牌 chepaihao


一、导入依赖

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>


<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

二、Utils

import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod;
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import static org.apache.commons.codec.binary.Base64.encodeBase64;
//依赖请参照:https://icredit-code.oss-cn-hangzhou.aliyuncs.com/pom.xml public class Main {
    public static String chepaihao(String imgFile) {         //API产品路径         String requestUrl = "https://iplatecard.market.alicloudapi.com/ai_market/ai_ocr_universal/license_plate/v2";         //阿里云APPCODE         String appcode = "0b69113f29e645bb85d0f75d824466e3";         Map<String, String> headers = new HashMap<String, String>();         headers.put("Authorization", "APPCODE " + appcode);         //根据API的要求,定义相对应的Content-Type         headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");         Map<String, String> bodys = new HashMap<String, String>();
        //内容数据类型,如:0,则表示BASE64编码;1,则表示图像文件URL链接

            String imgBase64 = "";             try {                 File file = new File(imgFile);                 byte[] content = new byte[(int) file.length()];                 FileInputStream finputstream = new FileInputStream(file);                 finputstream.read(content);                 finputstream.close();                 imgBase64 = new String(encodeBase64(content));             } catch (IOException e) {                 e.printStackTrace();
            }             bodys.put("IMAGE", imgBase64);             bodys.put("IMAGE_TYPE", "0");

        String response = null;         try {             response = post(requestUrl, headers, bodys);         } catch (IOException e) {             e.printStackTrace();         }

        return  response;     }
    public static String post(String url, Map<String, String> headers, Map<String, String> body) throws IOException {         HttpClient client = new HttpClient();         PostMethod postMethod = new PostMethod(url);         // 必须设置下面这个Header         for (String key : headers.keySet()) {             postMethod.addRequestHeader(key, headers.get(key));         }
        List<NameValuePair> bodyPair = new ArrayList<>();         for (String key : body.keySet()) {             bodyPair.add(new NameValuePair(key, body.get(key)));         }         NameValuePair[] bodyKvs = new NameValuePair[body.size()];         postMethod.setRequestBody(bodyPair.toArray(bodyKvs));
        int code = client.executeMethod(postMethod);         if (code == 200) {             String res = postMethod.getResponseBodyAsString();             System.out.println(res);             return res;         }         else {             System.out.println(code);         }         throw new IOException("请求失败");     } } 三、Controller层使用

@RequestMapping("Park")
public String park(ParklogTable parklogTable, Model model, MultipartFile file){
//图片上传成功后,将图片的地址写到数据库
String filePath="D://img";
//获得原始图片的拓展名
String originalFileName=file.getOriginalFilename();


//封装上传文件位置的全路径
File targetFile=new File(filePath,originalFileName);
//图片路径
String chepaihaotupain=targetFile.getPath();
String chepai= Main.chepaihao(chepaihaotupain);
String chepaihao="";
JSONObject json = JSONObject.fromObject(chepai);

JSONArray content = json.getJSONArray("CAR_NUMBER_RECOGNITION_ENTITY"); // 获取CAR_NUMBER_RECOGNITION_ENTITY内容
if (content.size() > 0) {
for (int i = 0; i < content.size(); i++) {
JSONObject job = content.getJSONObject(i); // 遍历 jsonarray
chepaihao= (String) job.get("NUMBER");// 得到 每个对象中的属性值
}
}

 

 

List<ParklogTable> bycarnum = service.findBycarnum(chepaihao);
if(bycarnum.isEmpty()){
Date date=new Date();
parklogTable.setParklogParktime(date);
parklogTable.setParklogCarnum(chepaihao);
parklogTable.setParklogState(1);
List<StallTable> allyes = stallService.allyes();
if(!allyes.isEmpty()){
StallTable stallTable = allyes.get(0);
int stallid=stallTable.getStallId();
String stallname=stallTable.getStallName();
parklogTable.setParklogStallid(stallid);
parklogTable.setParklogPosition(stallname);

List<VipTable> byCarnum = vipService.findByCarnum(chepaihao);
if(byCarnum.size()>0){
parklogTable.setParklogCarlevel("vip车辆");
service.insert(parklogTable);
}else{
parklogTable.setParklogCarlevel("普通车辆");
service.insert(parklogTable);
}

stallService.updatestate(stallTable);
model.addAttribute("parkmsg","欢迎"+chepaihao+"进入停车场");
}else{
model.addAttribute("parkmsg","车位已满");
}
}else{
model.addAttribute("parkmsg","您已停车");
}


return "desk";
}

四、html

<form action="Park" method="post" enctype="multipart/form-data"> 输入车牌号: <input type="file" name="file"> <input type="submit" value="停车" class="button"> </form>

 

标签:String,parklogTable,content,阿里,import,new,识别,车牌,chepaihao
来源: https://www.cnblogs.com/j99426/p/15654711.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有