要获取手机相机拍照或者访问相册 这里贴一个相关链接:http://blog.csdn.net/jackfrued/article/details/8967667
JSP页面代码:
<input type="file" accept="image/*" capture="camera" id="picFile" onchange="readFile(this)" style="width:30px;height:30px;opacity:0"/ >
JS代码:
function readFile(obj){
var file = obj.files[0];
//判断类型是不是图片 不难发现这个检测是基于正则表达式的,因此可以进行各种复杂的匹配,非常有用。
if(!/image\/\w+/.test(file.type)){
alert("请确保文件为图像类型");
return false;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e){
getResultbyOCR(this.result);
}
}
function getResultbyOCR(imagesdata) {
var requestdata = {
imagesdata: imagesdata
};
$.ajax({
type: "POST",
url: getUrl() + "/vehicleInfor/getResultbyOCR.do",
data: JSON.stringify(requestdata),
// 将对象序列化成JSON字符串
dataType: "json",
beforeSend: function () {
this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'],time: 60*1000});
},
contentType: 'application/json;charset=utf-8',
// 设置请求头信息
success: function(data) {
if (data.length == 0) {
layer.close(this.layerIndex);
alert("识别失败");
} else {
var outputValue = data.outputs[0].outputValue;
if (outputValue != null && "undefined" !== typeof(outputValue)) {
var datavalue = outputValue.dataValue;
if (datavalue != null && "undefined" !== typeof(datavalue)) {
datavalue = eval("(" + datavalue + ")");
var successflag = datavalue.success;
if (successflag) {
var engine_num = datavalue.engine_num;
var modelName = datavalue.model;
var owner = datavalue.owner;
var plate_num = datavalue.plate_num;
var register_date = datavalue.register_date;
var vin = datavalue.vin; $("#drvowner").val(owner);
$("#lcnno").val(plate_num);
$("#vinno").val(vin);
$("#model").val(modelName);
$("#engno").val(engine_num);
$("#registerdate").val(register_date);
layer.close(this.layerIndex);
} else {
layer.close(this.layerIndex);
alert("识别失败");
}
} else {
layer.close(this.layerIndex);
alert("识别失败");
}
} else {
layer.close(this.layerIndex);
alert("识别失败");
}
}
},
error: function(res) {
layer.close(this.layerIndex);
alert("识别失败");
}
});
}
Java代码:
@RequestMapping("/getResultbyOCR.do")
public @ResponseBody String getResultbyOCR( @RequestBody String requestData,HttpServletResponse response){
logger.info("开始识别行驶证 图片内容为 : "+requestData);
String result=vehicleInforService.getVehicleinforByOCR( requestData);
logger.info("结束识别行驶证,结果为 : "+result);
return result;
}