<div id="image" style="height: 130px;background: #fff;padding-top: 10px;display: flex;justify-content: space-around;">
<div class="pie" onclick="captureImage(1)" id="img1">
<img src="img/photograph@3x.png" />
<p>拍照</p>
</div>
<div class="pie" onclick="captureImage(2)" id="img2">
<img src="img/photograph@3x.png" />
<p>拍照</p>
</div>
<div class="pie" onclick="captureImage(3)" id="img3">
<img src="img/photograph@3x.png" />
<p>拍照</p>
<!--<img src="img/connection_icon@3x.png"/><i>x</i>-->
</div>
</div>
//调用手机拍照功能
// 扩展API加载完毕后调用onPlusReady回调函数
document.addEventListener( "plusready", onPlusReady, false );
// 扩展API加载完毕,现在可以正常调用扩展API
function onPlusReady() {
console.log("plusready");
}
var TC = 0;
// 拍照
function captureImage(t){
TC = t;
var cmr = plus.camera.getCamera();
var res = cmr.supportedImageResolutions[0];
var fmt = cmr.supportedImageFormats[0];
// console.log("Resolution: "+res+", Format: "+fmt);
cmr.captureImage( function( path ){
var files = [];
files.push({name:"uploadkey",path:path});
compressImage(files);
},
function( error ) {
// alert( "Capture image failed: " + error.message );
},
{resolution:res,format:fmt}
);
}
// 压缩图片
function compressImage(f){
plus.nativeUI.showWaiting();
plus.zip.compressImage({
src:f[0].path,
dst:"_doc/cm.jpg",
quality:20,
overwrite:true,
width:'50%',
clip:{
top:"25%",
left:"25%",
width:"50%",
height:"50%"
}
},
function(i){
plus.nativeUI.closeWaiting();
upload(i.target,f[0].name);
},function(e){
plus.nativeUI.closeWaiting();
});
}
var server=sysurl + "home/visit/upfile";
// 上传文件
function upload(files,name){
if(files.length<=0){
plus.nativeUI.alert("没有添加上传文件!");
return;
}
console.log("开始上传:")
var wt=plus.nativeUI.showWaiting();
var task=plus.uploader.createUpload(server,
{method:"POST"},
function(t,status){ //上传完成
if(status==200){
var txt = JSON.parse(t.responseText);
if(TC == 1){ //图片显示
var im = '<img src="'+txt.url+'" /><i>x</i>';
$('#img1').addClass('active');
$('#img1').html(im);
}else if(TC == 2){
var im = '<img src="'+txt.url+'" /><i>x</i>';
$('#img2').addClass('active');
$('#img2').html(im);
}else if(TC == 3){
var im = '<img src="'+txt.url+'" /><i>x</i>';
$('#img3').addClass('active');
$('#img3').html(im);
}else{
var im = '<img src="'+txt.url+'" /><i>x</i>';
$('#img1').addClass('active');
$('#img1').html(im);
}
wt.close();
}else{
console.log("上传失败:"+status);
wt.close();
}
}
);
task.addFile(files,{key:name});
task.start();
}
// 拍照添加文件
function appendByCamera() {
plus.camera.getCamera().captureImage(function(p) {
appendFile(p);
});
}