在cordova工程中调用 拍照插件: cordova-plugin-camera,图片插件: com.synconset.imagepicker,
在HTML中的任何标签上加上封装好的属性,都可以调起拍照和图片选择功能
/**
* 图片选择插件封装为:
* 属性:select-image='true'
*/
app.directive("selectImage",function () {
return {
restrict: 'A',
replace: false,
template:'',
controller:function () {
},
link:function (scope,element,attr) {
element.on('click',function () {
// 打开图片库
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50, // 相片质量是50
sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM, // 设置从图片库获取
destinationType: Camera.DestinationType.DATA_URL // 以base64返回
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
// ons.notification.alert("success");
}
function onFail(message) {
ons.notification.alert('Failed because: ' + message);
}
//加载设备
$(document).ready(function () {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.camera);
// ons.notification.alert('设备已就绪');
}
})
})
}
}
});
/**
* 拍照插件封装为:
* 属性:camera='true'
*/
app.directive("camera",function () {
return{
restrict: 'A',
replace: false,
template:'',
controller:function () {
},
link:function (scope,element,attr) {
element.on("click",function () {
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});
// ons.notification.alert('将要拍照');
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
ons.notification.alert("成功");
}
function onFail(message) {
ons.notification.alert('出错: ' + message);
}
//加载设备
$(document).ready(function () {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.camera);
// ons.notification.alert('设备已就绪');
}
})
})
}
}
});