基于百度文字识别SDK开发自助车牌识别系统

这次的实验是完成身份证照片和车牌的识别功能,流程和上一次实验差不多,不同的就是对数据的处理形式和展示,以及对页面的布局重新调整了一下。

首先去百度智能云技术文档下,找到文字识别中的身份证识别技术文档,下载需要的4个类。分别是Base64Util.java,FileUtil.java,GsonUtis.java,HttpUtil.java,还有一个Access_token获取的类,这个在文档上也有,把它加入进去,然后就可以直接调用接口了,当然调用前需要先开通。

 

 

调用接口的类:    这里面包括俩个方法,分别是身份证识别和车牌识别,因为身份证可以看成key和value的值,所以把它存入hashMap中,车牌识别就返回一个简单的车牌号

public static HashMap<String,String> hashMap = new HashMap<>();

    //身份证识别
    public static void idcard(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "id_card_side=" + "front" + "&image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            String accessToken = AccessToken.getAuth();

            String result = HttpUtil.post(url, accessToken, param);
            
            //数据提取
            JSONObject jsonObject = JSON.parseObject(result);
            String all = jsonObject.get("words_result").toString();
            JSONObject jsonObject1 = JSON.parseObject(all);
            
            for(Entry<String, Object> object : jsonObject1.entrySet() ) {
                String key = object.getKey();
                String values = object.getValue().toString();
                JSONObject keyName = JSON.parseObject(values);
                String value = keyName.get("words").toString(); 
                
                hashMap.put(key,value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    //车牌识别
    public static String licensePlate(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            //这是我自己定义的Access_token获取的方法,文档上有源码
            String accessToken = AccessToken.getAuth();

            String result = HttpUtil.post(url, accessToken, param);
            
            //json解析
            JSONObject jsonObject = JSON.parseObject(result);
            String all = jsonObject.get("words_result").toString();
            JSONObject jsonNumber = JSON.parseObject(all);
            String number = jsonNumber.get("number").toString();
            return number;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

呈现结果的窗口类:  

setLookAndFeel()函数就是一个窗口皮肤函数,想要生效就要导入jar包,不用也可以。
在显示身份证信息时,setText()无法识别换行符,但可以识别html,所以用拼接字符串形式,让数据换行显示。
public static String myFile;
    public static String[] type = new String[] {"身份证","车牌"};
    public static String temp;
    
    public static void main(String[] args) {
        HashMap<String,String> hashMap = new HashMap<>();
        setLookAndFeel();
        AuthService authService = new AuthService();
        
        //主窗体基本信息
        JFrame j = new JFrame("文字识别系统");
        j.setSize(1000, 800);
        j.setLocation(300, 50);
        j.setLayout(null);
        
        //下拉框选择查询类别
        JLabel tishi = new JLabel("要识别图片的类型");
        tishi.setBounds(280,20,200, 30);
        JComboBox<String> cb = new JComboBox<>(type);
        cb.setBounds(400, 20, 70, 30);
        
        //打开文件信息
        JFileChooser jfc = new JFileChooser();
        jfc.setFileFilter(new FileFilter() {
            
            @Override
            public String getDescription() {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public boolean accept(File f) {
                // TODO Auto-generated method stub
                return true;
            }
        });
        
        //图片容器
        JLabel l = new JLabel();
        
        //显示文字组件
        JLabel jieguo = new JLabel("等待选择图片");
        jieguo.setBounds(600, 150, 300, 300);
        
        //上传百度接口验证按钮
                JButton jb2 = new JButton("上传网络验证");
                jb2.setBounds(600, 350, 100, 50);
        
        //获取图片按钮
        JButton jb1 = new JButton("上传图片");
        jb1.setBounds(200, 350, 100, 50);
        jb1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int returnVal =  jfc.showOpenDialog(j);
                File file = jfc.getSelectedFile();
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    myFile = file.getAbsolutePath();
                    myFile.replaceAll("\\\\", "/");
                    ImageIcon i = new ImageIcon(myFile);
                    l.setIcon(i);
                    l.setBounds(50,50,i.getIconWidth(),i.getIconHeight());
                    jb1.setBounds(i.getIconWidth()/2, i.getIconHeight()+100, 100, 50);
                    jb2.setBounds(i.getIconWidth()+100, 400, 100, 50);
                    jieguo.setBounds(i.getIconWidth()+100, 150, 300, 300);
                }   
                jieguo.setText("等待提交验证");
            }
        });
        
        
        
        jb2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                temp = cb.getSelectedItem().toString();
                if(temp.equals("身份证")) {
                    Interface.idcard(myFile);
                    String outCome = "<html><body><p>经过网络识别,这个"+temp+"的信息是<br>";
                    for(String key : Interface.hashMap.keySet()) {
                        String value = Interface.hashMap.get(key);
                        outCome += key+":"+value+"<br>";
                    }
                    outCome += "</p></body></html>";
                    jieguo.setText(outCome);
                    Interface.hashMap.clear();
                } else {
                    String number = Interface.licensePlate(myFile);
                    jieguo.setText("经过网络识别,这个"+temp+"的车牌号是"+number);
                }
                
            }
        });
        
        j.add(tishi);
        j.add(cb);
        j.add(jieguo);
        j.add(l);
        j.add(jb1);
        j.add(jb2);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);
    }
    
    private static void setLookAndFeel() {
        try {
          //javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
         //javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
//       javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
//        javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.fast.FastLookAndFeel");
          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
//          javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
            // handle exception
        }
}

 

结果演示:

 

posted @ 2022-11-05 15:19  听风1234  阅读(300)  评论(0编辑  收藏  举报