功能描述:
实现步骤:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
html {
font-size: 62.5%;
/*10 ÷ 16 × 100% = 62.5%*/
}
#btn,
#input_file {
font-size: 4.0rem;
/*2.4*10px=24px*/
}
#input_file {
position: absolute;
/*相对于最近的祖先元素定位,如果absolute的元素,没有被positioned的祖先元素,那么他是相对于文档的body元素来定位的;一个“positioned”元素是指 position 值不是 static 的元素*/
left: 8px;
opacity: 0;
z-index: 10;
}
#btn {
position: absolute;
z-index: 0;
color: black;
}
</style>
<script type="text/javascript">
//步骤2
function setImagePreview() {
var docObj = document.getElementById("input_file");
var imgObjPreview = document.getElementById("myimg");
var reader = new FileReader();
reader.readAsDataURL(docObj.files[0]);
reader.onload = function (e) {
var dataobj = this.result;
imgObjPreview.src = dataobj;
imgObjPreview.onload=function(){
var cropStr =compress(imgObjPreview,800,800,1.0);
console.log("cropStr:" + cropStr);//压缩后的base64 带前缀的
var dot = cropStr.indexOf(",");
var newBase64Data = cropStr.substring(dot + 1, cropStr.length);
//console.log("newBase64Data:"+newBase64Data);
localStorage.setItem("base64data", newBase64Data);
//步骤3
jump();
}
}
}
/*
* 图片压缩
* img 原始图片
* width 压缩后的宽度
* height 压缩后的高度
* ratio 压缩比率
*/
function compress(img, width, height, ratio) {
var canvas, ctx, img64;
canvas = document.getElementById('canv');
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);//把图片绘制到画布上
img64 = canvas.toDataURL("image/jpeg", ratio);
return img64;
}
function jump() {
//window.setTimeout("window.location.href='cardRecognition.html'", 5000);
var file = document.getElementById("input_file");
// for IE, Opera, Safari, Chrome
if (file.outerHTML) {
file.outerHTML = file.outerHTML;
} else { // FF(包括3.5)
file.value = "";
}
window.location.href = "cardRecognition.html";
}
</script>
</head>
<body>
<div><!--步骤1-->
<input type="button" value="名片识别" id="btn" disabled="disabled" />
<input type="file" accept="image/*" multiple="multiple" id="input_file" onchange="setImagePreview()" />
<input type="hidden" id="img" name="img" value="" />
</div>
<div>
<canvas id="canv" width="400" height="400" style="border:1px solid red;"></canvas>
<img id="myimg" src="" alt="预览" width="400" height="400" />
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>名片识别</title>
<link rel="stylesheet" type="text/css" href="../css/loader.min.css">
<script src="../js/jquery-3.2.1.min.js"></script>
<style type="text/css">
</style>
<script type="text/javascript">
$(function () {
var key = "Fna5PKtWYEGE3uCkyZvoMy";
var secret = "1cc17342718546f9a535a15eb243c87a";
var typeId = 20;
var format = "json";
var base64Data = localStorage.getItem("base64data");
//var test=localStorage.getItem("test");
localStorage.clear();
//console.log(base64Data);
//alert(base64Data);
//alert(test);
requestData();
///////////调取翔云orc接口//////////
function requestData() {
var oData = { "img": base64Data, "key": key, "secret": secret, "typeId": typeId, "format": format };
$.ajax({
url: "https://www.netocr.com/api/recogliu.do",
//url:"http://101.200.79.184/api/recogliu.do",
type: 'POST',
dataType: "json",//注意,此处设置为text,可以在ie中解析返回的xml
data: oData,
success: function (returndata) {
if (returndata != null) {
var jsonresult = JSON.stringify(returndata);
var mdata = $.parseJSON(jsonresult);
console.log(mdata);
var len = mdata.cardsinfo[0].items.length;
for (var i = 0; i <= len - 1; i++) {
item = mdata.cardsinfo[0].items[i];
$("#tb").append("<tr></tr><td style=\"border: 1px solid #000;width: 100px\">" + item.desc + "</td>" +
"<td style=\"border: 1px solid #000;width: 300px\">" + item.content + "</td></tr>");
}
//移除loading
$('body').addClass('loaded');
$('#loader-wrapper .load_title').remove();
}
},
error: function (returndata) {
alert("请求失败");
}
});
}
});
</script>
</head>
<body>
<div id="loader-wrapper">
<!--loading-->
<div id="loader"></div>
<div class="loader-section section-left"></div>
<div class="loader-section section-right"></div>
<div class="load_title">正在识别名片
<br> </div>
</div>
<div id="dv" align="center">
<table id="tb" style="text-align: center;">
</table>
</div>
</body>
</html>
开发时遇到的问题
/*摄像头方向控制*/
/*
* 图片压缩
* img 原始图片
* width 压缩后的宽度
* height 压缩后的高度
* ratio 压缩比率
*/
function compress(img, width, height, ratio) {
var canvas, ctx, img64;
canvas = document.getElementById('canv');
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height); //把压缩后的图片绘制到画布上
img64 = canvas.toDataURL("image/jpeg", ratio);
return img64;
}