html代码:
<div class ="form-com door" > <label for ="" > 门头照:</label > <a href ="javascript:void(0);" onclick ="getImage(1)" > <img id ="img1" class ="img" src ="" alt ="" > </a > </div > <div class ="form-com licence" > <label for ="" > 营业执照:</label > <a href ="javascript:void(0);" onclick ="getImage(2)" > <img id ="img2" class ="img" src ="" alt ="" > </a > </div > <div class ="form-com shop-panorama" > <label for ="" > 店铺全景:</label > <a href ="javascript:void(0);" onclick ="getImage(3)" > <img id ="img3" class ="img" src ="" alt ="" > </a > </div >
js代码:
document.addEventListener( "plusready" , onPlusReady, false );function onPlusReady () { console.log("plusready" );}function getImage (obj) { var cmr = plus.camera.getCamera();cmr.captureImage(function (p) { plus.io.resolveLocalFileSystemURL(p, function (entry) { img=document.getElementById("img" +obj);img.src=entry.toLocalURL();img.style.opacity = 1 ;}, function (e) { outLine('读取拍照文件错误:' +e.message);});}, function (e) { }, {filename:'_doc/camera/' ,index:1 });
}function dealImage (path, obj, callback) {var img = new Image();img.src = path;img.onload = function () {var that = this ;var w = that.width,h = that.height,scale = w / h;w = obj.width || w;h = obj.height || (w / scale);var quality = 0.5 ; var canvas = document.createElement('canvas' );var ctx = canvas.getContext('2d' );var anw = document.createAttribute("width" );anw.nodeValue = w;var anh = document.createAttribute("height" );anh.nodeValue = h;canvas.setAttributeNode(anw);canvas.setAttributeNode(anh); ctx.drawImage(that, 0 , 0 , w, h);if (obj.quality && obj.quality <= 1 && obj.quality > 0 ){quality = obj.quality;}var base64 = canvas.toDataURL('image/jpeg' , quality );callback(base64);}}var time = null ;var view = '' ;var view1 = '' ;var view2 = '' ;$(document).on('click' ,'.submit button' ,function () { dealImage($('#img1' ).attr('src' ),$('#img1' )[0 ],function (base641) { view = base641;});dealImage($('#img2' ).attr('src' ),$('#img2' )[0 ],function (base642) { view1 = base642;});dealImage($('#img3' ).attr('src' ),$('#img3' )[0 ],function (base643) { view2 = base643; });time = setTimeout(function () { httppost(view,view1,view2);},2000 );});
到此处调用手机拍照,压缩并上传到后台完成,view,view1,view2分别是basse64