【身份证识别】Java示例解析JSON转对象
756665228 发布于2018-03-15 浏览:3991 回复:1
3
收藏
最后编辑于2018-10-21
好多Java开发者们。感觉解析JSON读取识别的结果内容表示不太方便。尤其是身份证返回数据有中文key,这导致大家更无奈。
鄙人就打个样。 O(∩_∩)O 大神就不要鄙视了哦
 
原图文件(鄙人的身份证图片就不上传了)
 
注意:本文中的IdCardBean对象需要结合fastjson的jar

    com.alibaba
    fastjson
    1.2.35
 
***************************开始造代码***************************
  • Java通过SDK方式请求身份证识别接口并解析JSON字符串

API方式调用代码就不贴了有需要去Gitee查看

代码链接请点击这里

	public static void main(String[] args) {
		AipOcr aipOcr = new AipOcr("APPID", "APIKEY", "SECRETKEY");
		String imgData = "图片本地路径";
		HashMap options= new HashMap();
		options.put("detect_direction", "true");
		options.put("detect_risk", "true");
        //第二个参数是身份证正反面 请一定要注意是否填写正确
		JSONObject jsonObject = aipOcr.idcard(imgData,"front",options);
		System.out.println(jsonObject);
                //转IdCardBean对象
		IdCardBean idCardFront = JSON.parseObject(jsonObject.toString(),IdCardBean.class);
		System.out.println(idCardFront.getWords_result().getName().getWords());
	}
  • 返回的内容JSON字符串(正面)
{
    "words_result": {
        "性别": {
            "words": "男", 
            "location": {
                "height": 60, 
                "width": 47, 
                "left": 477, 
                "top": 298
            }
        }, 
        "姓名": {
            "words": "小帅丶", 
            "location": {
                "height": 70, 
                "width": 203, 
                "left": 527, 
                "top": 177
            }
        }, 
        "住址": {
            "words": "北京", 
            "location": {
                "height": 134, 
                "width": 602, 
                "left": 523, 
                "top": 532
            }
        }, 
        "公民身份号码": {
            "words": "110.................", 
            "location": {
                "height": 71, 
                "width": 781, 
                "left": 759, 
                "top": 818
            }
        }, 
        "出生": {
            "words": "20180315", 
            "location": {
                "height": 0, 
                "width": 0, 
                "left": 0, 
                "top": 0
            }
        }, 
        "民族": {
            "words": "汉", 
            "location": {
                "height": 50, 
                "width": 41, 
                "left": 828, 
                "top": 308
            }
        }
    }, 
    "direction": 0, 
    "words_result_num": 6, 
    "risk_type": "normal", 
    "image_status": "normal", 
    "edit_tool": "Microsoft Windows Photo Viewer 6.1.7600.16385", 
    "log_id": 4788748099774263000
}
  • 返回的内容JSON字符串(反面)
{
    "words_result": {
        "失效日期": {
            "words": "20990315", 
            "location": {
                "height": 39, 
                "width": 196, 
                "left": 913, 
                "top": 764
            }
        }, 
        "签发机关": {
            "words": "北京市公安局", 
            "location": {
                "height": 44, 
                "width": 385, 
                "left": 687, 
                "top": 671
            }
        }, 
        "签发日期": {
            "words": "20180315", 
            "location": {
                "height": 38, 
                "width": 202, 
                "left": 685, 
                "top": 767
            }
        }
    }, 
    "direction": 0, 
    "words_result_num": 3, 
    "risk_type": "normal", 
    "image_status": "normal", 
    "edit_tool": "Microsoft Windows Photo Viewer 6.1.7600.16385", 
    "log_id": 4930841814145141000
}
  • 解析JSON后返回的只有words字段的内容(正反面)
//身份证背面数据
编辑工具:Microsoft Windows Photo Viewer 6.1.7600.16385
签发机关:北京市公安局
签发日期:20180315
失效日期:20990315


//身份证正面数据
编辑工具:Microsoft Windows Photo Viewer 6.1.7600.16385
姓名:小帅丶
公民身份号码:110..........
住址:北京市
性别:男
出生:20180315
民族:汉
  • 用到的Java对象(具体含义自行查看文档)IdCardBean

使用IdCardBean之前必须项目里面有fastjson的jar。使用其他也可以,作者没有做实验,如果使用作者封装的IdCardBean请使用fastjson的jar哦

package com.xs.pojo.ocr.idcard;


import com.alibaba.fastjson.annotation.JSONField;

/**
 * 身份证Bean
 * @author 小帅丶
 *
 */
public class IdCardBean {
	/**
	 * direction 图像方向,当detect_direction=true时存在。-1:未定义,- 0:正向,- 1: 逆时针90度,- 2:逆时针180度,- 3:逆时针270度
     * image_status	normal-识别正常 reversed_side-身份证正反面颠倒 non_idcard-上传的图片中不包含身份证 blurred-身份证模糊 other_type_card-其他类型证照 over_exposure-身份证关键字段反光或过曝 unknown-未知状态
	 * risk_type 输入参数 detect_risk = true 时,则返回该字段识别身份证类型: normal-正常身份证;copy-复印件;temporary-临时身份证;screen-翻拍;unknow-其他未知情况
	 * edit_tool 如果参数 detect_risk = true 时,则返回此字段。如果检测身份证被编辑过,该字段指定编辑软件名称,如:Adobe Photoshop CC 2014 (Macintosh),如果没有被编辑过则返回值无此参数
	 * log_id 唯一的log id,用于问题定位
	 * words_result定位和识别结果数组
	 * words_result_num	识别结果数,表示words_result的元素个数
	 * +location	位置数组(坐标0点为左上角)
	 * ++left	表示定位位置的长方形左上顶点的水平坐标
	 * ++top	表示定位位置的长方形左上顶点的垂直坐标
	 * ++width	表示定位位置的长方形的宽度
	 * ++height 表示定位位置的长方形的高度
	 * +words	识别结果字符串
	 */
    private Long log_id;
    private int words_result_num;
    private int direction;
    private String image_status;
    private WordsResult words_result;
    private String edit_tool;
    private String risk_type;
	public Long getLog_id() {
		return log_id;
	}
	public void setLog_id(Long log_id) {
		this.log_id = log_id;
	}

	public int getWords_result_num() {
		return words_result_num;
	}

	public void setWords_result_num(int words_result_num) {
		this.words_result_num = words_result_num;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public String getImage_status() {
		return image_status;
	}

	public void setImage_status(String image_status) {
		this.image_status = image_status;
	}
	public WordsResult getWords_result() {
		return words_result;
	}
	public void setWords_result(WordsResult words_result) {
		this.words_result = words_result;
	}

	public String getEdit_tool() {
		return edit_tool;
	}
	public void setEdit_tool(String edit_tool) {
		this.edit_tool = edit_tool;
	}
	public String getRisk_type() {
		return risk_type;
	}
	public void setRisk_type(String risk_type) {
		this.risk_type = risk_type;
	}
	//为了友好解析,使用了FastJson对源数据key中文的解析
	public static class WordsResult {
    	@JSONField(name="住址")
        private Address address;
    	@JSONField(name="出生")
        private Birth birth;
    	@JSONField(name="姓名")
        private Name name;
    	@JSONField(name="公民身份号码")
        private IdCardNum idCardNum;
    	@JSONField(name="性别")
        private Sex sex;
    	@JSONField(name="民族")
        private Nation nation;
    	@JSONField(name="签发日期")
        private IssueDate issueDate;
    	@JSONField(name="签发机关")
        private Authority authority;
    	@JSONField(name="失效日期")
    	private ExpiryDate expiryDate;
		public Address getAddress() {
			return address;
		}
		public void setAddress(Address address) {
			this.address = address;
		}
		public Birth getBirth() {
			return birth;
		}
		public void setBirth(Birth birth) {
			this.birth = birth;
		}
		public Name getName() {
			return name;
		}
		public void setName(Name name) {
			this.name = name;
		}
		public IdCardNum getIdCardNum() {
			return idCardNum;
		}
		public void setIdCardNum(IdCardNum idCardNum) {
			this.idCardNum = idCardNum;
		}
		public Sex getSex() {
			return sex;
		}
		public void setSex(Sex sex) {
			this.sex = sex;
		}
		public Nation getNation() {
			return nation;
		}
		public void setNation(Nation nation) {
			this.nation = nation;
		}
		public IssueDate getIssueDate() {
			return issueDate;
		}
		public void setIssueDate(IssueDate issueDate) {
			this.issueDate = issueDate;
		}
		public Authority getAuthority() {
			return authority;
		}
		public void setAuthority(Authority authority) {
			this.authority = authority;
		}
		public ExpiryDate getExpiryDate() {
			return expiryDate;
		}
		public void setExpiryDate(ExpiryDate expiryDate) {
			this.expiryDate = expiryDate;
		}
    }
    public static class Address {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class Birth {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class Nation {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }
        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class Sex {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class IdCardNum {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class Name {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class IssueDate {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class Authority {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class ExpiryDate {
        private Location location;
        private String words;
        public void setLocation(Location location) {
             this.location = location;
         }
         public Location getLocation() {
             return location;
         }

        public void setWords(String words) {
             this.words = words;
         }
         public String getWords() {
             return words;
         }
    }
    public static class Location {
        private int width;
        private int top;
        private int height;
        private int left;
        public void setWidth(int width) {
             this.width = width;
         }
         public int getWidth() {
             return width;
         }

        public void setTop(int top) {
             this.top = top;
         }
         public int getTop() {
             return top;
         }

        public void setHeight(int height) {
             this.height = height;
         }
         public int getHeight() {
             return height;
         }

        public void setLeft(int left) {
             this.left = left;
         }
         public int getLeft() {
             return left;
         }
    }
}


***************************代码结束***************************

以上就是 本帖所有内容哦。源码已经在开始内容给出

 

 

收藏
点赞
3
个赞
共1条回复 最后由lichao_86回复于2018-10-21
#3lichao_86回复于2018-10-21

楼主写的很详细

0
TOP
切换版块