html 代码
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
- <title></title>
- <link href="CSS/mui.min.css" rel="stylesheet"/>
- <style type="text/css">
- *{margin: 0px;padding: 0px;}
- .imgs-box{width: 300px;height: 200px;margin: 20px auto;overflow: hidden;border: 1px solid #ccc;}
- .imgs-box img{height: 100%;display: block;margin: 0px auto;border: none;}
- </style>
- </head>
- <body>
- <header class="mui-bar mui-bar-nav">
- <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
- <h1 class="mui-title"> 标题 </h1>
- </header>
- <div class="mui-content">
- <div class="imgs-box" id="headImage">
- <img src=""alt="" id="choosedpic"/>
- </div>
- <div class="imgs-text">
- <form action=""method="">
- <input type="text" name=""id="num1"value="" placeholder="自动识别身份证姓名"/>
- <input type="text" name=""id="num2"value="" placeholder="自动识别身份证号码"/>
- </form>
- </div>
- </div>
- </body>
- <script src="js/mui.min.js"></script>
- <script type="text/javascript" charset="utf-8">
- mui.init();
- // 点击头像触发
- document.getElementById("headImage").addEventListener('tap',function(){
- if(mui.os.plus){
- var a=[{title:"拍照"},{title:"从手机相册选择"}];
- plus.nativeUI.actionSheet({
- title:"修改用户头像",
- cancel:"取消",
- buttons:a
- },function(b){/*actionSheet 按钮点击回调事件 */
- switch(b.index){
- case 0:
- break;
- case 1:getImage();// 拍照
- break;
- case 2:galleryImg();// 打开相册
- break;
- default:
- break;
- }
- });
- }
- },false);
- // 拍照
- function getImage() {
- var c = plus.camera.getCamera();
- c.captureImage(function(e) {
- plus.io.resolveLocalFileSystemURL(e, function(entry) {
- var s = entry.toLocalURL();
- console.log(s)
- uploadHead(s); /* 上传图片 */
- }, function(e) {
- console.log("读取拍照文件错误:" + e.message);
- });
- plus.gallery.save( e );
- }, function(s) {
- console.log("error" + s);
- }, {
- filename: "_doc/"
- })
- }
- // 从相册添加文件
- function galleryImg(){
- plus.gallery.pick(function(path){
- uploadHead(path);
- });
- }
- // 上传头像图片
- // 身份证姓名
- var inputText = document.getElementById("num1");
- // 身份证号码
- var inputText2 = document.getElementById("num2");
- var mainImage = document.getElementById("choosedpic");
- function uploadHead(imgPath) {
- mainImage.src = imgPath;
- // mainImage.style.width = "300px";
- // mainImage.style.height = "200px";
- var image = new Image();
- image.src = imgPath;
- image.onload = function() {
- var imgData = getBase64Image(image).toString();
- /* 在这里调用上传接口 */
- mui.ajax("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=24.6de055adc677dd1b05b655448de8e979.2592000.1512612354.282335-10326617", {
- data: {
- detect_direction:true,
- id_card_side:"front",
- image: imgData
- },
- headers:{'Content-Type':'application/x-www-form-urlencoded'},
- dataType: 'json',
- type: 'post',
- timeout: 5000,
- success: function(data) {
- // plus.nativeUI.showWaiting("正在为您自动识别中...")
- console.log(JSON.stringify(data))
- // 当图片上传不正确时, 如上传非身份证, 或者无法识别身份证
- if(data.error_code==216633)
- {
- mui.alert("无法识别身份证, 请检查是否正确上传了身份证照片!")
- }
- inputText.value=data.words_result['姓名'].words;
- inputText2.value=data.words_result['公民身份号码'].words;
- mui.alert('已经为您识别完毕','温馨提示','确认');
- // plus.nativeUI.closeWaiting();
- console.log('上传成功');
- },
- error: function(xhr, type, errorThrown) {
- console.log('网络异常');
- mui.toast('网络异常, 请稍后再试!');
- }
- });
- }
- }
- // 将图片压缩转成 base64
- function getBase64Image(img) {
- var canvas = document.createElement("canvas");
- var width = img.width;
- var height = img.height;
- if (width> height) {
- if (width> 500) {
- height = Math.round(height *= 500 / width);
- width = 500;
- }
- } else {
- if (height> 500) {
- width = Math.round(width *= 500 / height);
- height = 500;
- }
- }
- canvas.width = width; /* 设置新的图片的宽度 */
- canvas.height = height; /* 设置新的图片的长度 */
- var ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0, width, height); /* 绘图 */
- var dataURL = canvas.toDataURL("image/png", 0.8);
- console.log(dataURL);
- var newdataurl=dataURL.substring(dataURL.indexOf(",")+1);
- console.log(newdataurl)
- return newdataurl;
- }
- </script>
- </html>
来源: http://www.qdfuns.com/article/42561/7fd0cc47a381f13248cbad7c14af956c.html