阿里云车牌识别

一、导入依赖

<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 (阿里云车牌识别购买此链接https://market.aliyun.com/products/57124001/cmapi032750.html?spm=5176.21213303.J_6704733920.7.30be3edaD8xSjO&scm=20140722.S_market%40%40API%E5%B8%82%E5%9C%BA%40%40cmapi032750._.ID_market%40%40API%E5%B8%82%E5%9C%BA%40%40cmapi032750-RL_%E8%BD%A6%E7%89%8C%E8%AF%86%E5%88%AB-OR_main-V_2-P0_0#sku=yuncode2675000001)

 

 

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 = "自己购买的码";
        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>

 
posted @ 2021-12-07 10:23  静静奇女子  阅读(419)  评论(0编辑  收藏  举报