相机,相册调用公共类
//设备相关类
var Phone = {
createNew: function() {
var phone = {};
/*
* 获得照片
* 参数:
* callback - 回调方法,成功回调照片文件、或base64编码串
* source - 相机或相册,CAMEAR/ALBUM
* direction - 前镜头或后镜头,FRONT/BACK
* width height- 尺寸
*
*/
phone.takePhoto = function(parm) {
console.log(parm);
var callbackfn = parm.callback;
if(!callbackfn || typeof(callbackfn) !== "function") return;
parm.source = parm.source || 'CAMERA';
parm.direction = parm.direction || 'BACK' ;
parm.width = parm.width || 640;
parm.height = parm.height || 640;
// 来源:相机、相册
var sourceType;
switch(parm.source.toUpperCase()) {
case 'ALBUM':
sourceType = 2;
break;
default:
sourceType = 1;
break;
}
// 相机方向
var direction;
switch(parm.direction.toUpperCase()) {
case 'FRONT':
direction = 1;
break;
default:
direction = 0;
break;
}
// 相片大小
var width = parm.width;
var height = parm.height;
if(navigator.camera) {
navigator.camera.getPicture(
function(data) {
console.log('Camera cleanup success');
callbackfn(/*'data:image/jpeg;base64,' + */data);
},
function(e) {
console.log("Error getting picture: ", e);
callbackfn('');
},
{
quality: 95,
destinationType: 0,
sourceType : sourceType,
allowEdit : true,
encodingType: 0,
targetWidth : width,
targetHeight : height,
correctOrientation: true,
saveToPhotoAlbum: false,
cameraDirection: direction
}
);
} else {
// 没有相机
var filename = '‘;
console.log('没有相机');
callbackfn(filename);
}
};
return phone;
}
};
var phone = Phone.createNew();
下面是相机的调用
//相机
the_camera : function(){
var parm = {
callback : function(filename) {
if(filename != ''){
console.log(filename);
//这里成功以后返回的
}
},
source:'CAMERA',// 相机或相册,CAMEAR/ALBUM
direction:'FRONT',// 前镜头或后镜头,FRONT/BACK
width:640, //width
height:640 // height
};
phone.takePhoto(parm);
},
相册的调用
//相册
photo_album : function(){
var parm = {
callback : function(filename) {
if(filename != ''){
console.log(filename);
//跟相机一样,只是传的参数不一样而已
}
},
source:'ALBUM',// 相机或相册,CAMEAR/ALBUM
direction:'FRONT',// 前镜头或后镜头,FRONT/BACK
width:640, //width
height:640 // height
};
phone.takePhoto(parm);
},
如果报错
java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider
java.lang.ClassNotFoundException: Didn’t find class “android.support.v4.content.FileProvider”
的话
点击这里https://blog.csdn.net/weixin_43369058/article/details/85602248