Face++身份证图片识别

在不少web系统都需要对身份证图片里面信息进行识别,然后再调用官方的系统接口,判断该身份证号是否正确。下面我们就介绍如何使用face++进行身份证图片的识别。

首先我们需要将图片上传到指定的目录,之后再读取图片,调用face++接口进行身份证信息的识别。

Face++身份证图片识别_第1张图片

下面的上传正反面身份证图片的java代码

	public final String idcardpath="E:\\ruanjian\\idcard\\";///usr/lib/tomcat/head/idcard/
	
	//上传身份证到idcard目录
	  @RequestMapping(value = "/uploadIdcard",method = RequestMethod.POST)
	 public String updateInfo(@RequestParam("qian") MultipartFile qian,@RequestParam("hou")     MultipartFile hou ,HttpServletRequest request) 
			throws IllegalStateException, IOException {
		String imgNmae1 = qian.getOriginalFilename();
		String imgType1 = imgNmae1.substring(imgNmae1.lastIndexOf(".")+1);
		String imgNmae2 = hou.getOriginalFilename();
		String imgType2 = imgNmae2.substring(imgNmae2.lastIndexOf(".")+1);
		File newFile1 = new File(idcardpath+"1." +imgType1);
		File newFile2 = new File(idcardpath+"2."+imgType2);
		qian.transferTo(newFile1);
		hou.transferTo(newFile2);
		return "forward:cardstest";
		
	}

 

下面是对应的jsp代码:

	

身份证正面照

点击上传

身份证反面照

点击上传

读取idcard目录下的身份证照片,并且验证。decodeUnicode()对json的编码由unicode改为utf-8

@RequestMapping(value = "/cardstest",produces = {"text/html;charset=UTF-8;"},method=RequestMethod.POST)

	public String cardstest(HttpServletRequest request) {
		//String path="E:\\Tomcat ruanjian\\idcard\\";// /usr/lib/tomcat/head/idcard/  E:\\Tomcat ruanjian\\idcard\\
		String ret="";
		ret=fun(idcardpath+"1.jpg");
		String ret2=fun(idcardpath+"2.jpg");
		Cards c=getCards(ret);
		System.out.println(c.toString());
		request.setAttribute("c", c);
		return "addProvider";
	}

fun()获取face++返回的json字符串, 

public String fun(String path){
		String ret="";
		 File file1 = new File(path);
			byte[] buff1 = getBytesFromFile(file1);
			String url = "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard";
	        HashMap map = new HashMap<>();
	        HashMap byteMap = new HashMap<>();
	        map.put("api_key", "-rmz0oCucWPcvlBv49RErHipvsGlf5DC");
	        map.put("api_secret", "kBvsaSkZnp4aYkJeiT4JkIck0PjLOtnJ");
			map.put("return_landmark", "1");
	        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
	        byteMap.put("image_file", buff1);
	        
	        try{
	            byte[] bacd = post(url, map, byteMap);
	            String str = new String(bacd);
	               
	            ret = decodeUnicode(str);  
	            System.out.println(ret);  
	        
	        }catch (Exception e) {
	        	e.printStackTrace();
			}
	        
		 return ret;
	}
	

 

 

你可能感兴趣的:(项目)