package myProject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import myProject.com.model.DataModel;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import sun.misc.BASE64Encoder;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
*
* @Describtion:测试如何使用OCR识别证件信息
* @author: future
* 2018年7月26日下午11:36:01
*/
public class testOCR {
/**
* @Description : 测试
* @author: future
* @date:2018年7月27日下午10:40:54
* void
*/
@Test
public void testOCR(){
//要进行识别测试的证件照片
String imgFilePath = "H:\\xx\\xx\\xxx\\xx.jpg";
File file = new File(imgFilePath);
byte[] buffer = null;
FileInputStream inputFile = null;
try {
inputFile = new FileInputStream(file);
buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
} catch (IOException e) {
e.printStackTrace();
}
String encode = new BASE64Encoder().encode(buffer);
String result = sendAllCardRecon(encode, "png", "2");//2-身份证 所以默认是身份验证,如果验证其他证据,记得修改这里!!!!!!!!!!!!!!
System.out.println("识别结果==>\n"+result);
}
/**
* @Description :
* @author: future
* @date:2018年7月27日下午10:38:59
* @param imgBase64Str
* @param imgType
* @param sbType
* @return
* String
*/
public static String sendAllCardRecon(String imgBase64Str,String imgType,String sbType){
//返回的结果
String result = "";
String str = setRecgnPlainParam(imgBase64Str,sbType,"","null");
//图片类型是否赋值
if("".equals(imgType) || imgType==null || "".equals(imgType.trim())){
imgType="jpg";
}
DataModel dataModel = new DataModel();
dataModel.setUsername("test");
dataModel.setParamdata(str);
dataModel.setImgtype(imgType);
//远程访问地址
String url = "http://IP:端口/cxfServerX/doAllCardRecon";
String params = EntityToStringNullValue(dataModel);
result = post(url,params);
return result;
}
/**
* @Description : 发送数据请求
* @author: future
* @date:2018年7月27日下午2:52:54
* @param url
* @param params
* @return
* String
*/
private static String post(String imgUrl, String params) {
BufferedReader reader = null;
try {
URL url = new URL(imgUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.setRequestMethod("POST");
connection.setRequestProperty("application/xml", "application/json;charset=UTF-8");
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
out.append(params);
out.flush();
out.close();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
String line;
String res = "";
while((line = reader.readLine())!=null){
res += line;
}
reader.close();
return res;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return "error";
}
/**
* @Description :
* @author: future
* @date:2018年7月27日下午10:40:23
* @param imgBase64Str
* @param type
* @param option
* @param password
* @return
* String
*/
public static String setRecgnPlainParam(String imgBase64Str,String type,String option,String password){
return imgBase64Str+"==##"+type+"==##"+option+"==##"+password;
}
/**
* @Description : 实体类转换为字符串
* @author: future
* @date:2018年7月27日下午2:51:32
* @param data
* @return
* String
*/
public static <T> String EntityToStringNullValue(T data){
String str = JSON.toJSONString(data,SerializerFeature.WriteMapNullValue);
return str;
}
}