如何在 cordova 中将图像从图库转换为 base64
How can I convert image from gallery to base64 in cordova
我想将图像转换为 Base64。
用例:用户从图库中选择图片,我想将其发送到服务器以 base64 格式上传。我使用了 cordova 相机,它成功地从图库中选择了图像。但问题是 - 无法将其转换为 base64。
我使用了 navigator.camera.getPicture
,成功后我得到了 content://media/external/images/media/18604
,我试图将其转换为:
var c=document.createElement('canvas');
var ctx=c.getContext("2d");
var img=new Image();
img.onload = function(){
c.width=this.width;
c.height=this.height;
//ctx.drawImage(img, 0,0);
};
img.src = imageData;
var dataURL = c.toDataURL("image/jpeg");
js代码:
document.addEventListener("deviceready", function() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY });
function onPhotoURISuccess(imageData) {
console.log("onPhotoURISuccess..")
//var image = document.getElementById('myImage');
var image = imageData;
//this is printing - content://media/external/images/media/18604
console.log("Image..." + image);
}
function onFail(e){
console.log("onFail..Err.." + e);
}
}, false);
好像不行,如果我拍本地jpeg就可以了!
你试过 Camera.destinationType.DATA_URL
了吗?
documentation 清楚地解释了如何调用 camera.getPicture
以获得 base64 输出(此代码来自文档):
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
注意:请注意,base64 操作非常占用内存,可能会冻结应用程序,尤其是在廉价手机中。
我想将图像转换为 Base64。
用例:用户从图库中选择图片,我想将其发送到服务器以 base64 格式上传。我使用了 cordova 相机,它成功地从图库中选择了图像。但问题是 - 无法将其转换为 base64。
我使用了 navigator.camera.getPicture
,成功后我得到了 content://media/external/images/media/18604
,我试图将其转换为:
var c=document.createElement('canvas');
var ctx=c.getContext("2d");
var img=new Image();
img.onload = function(){
c.width=this.width;
c.height=this.height;
//ctx.drawImage(img, 0,0);
};
img.src = imageData;
var dataURL = c.toDataURL("image/jpeg");
js代码:
document.addEventListener("deviceready", function() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY });
function onPhotoURISuccess(imageData) {
console.log("onPhotoURISuccess..")
//var image = document.getElementById('myImage');
var image = imageData;
//this is printing - content://media/external/images/media/18604
console.log("Image..." + image);
}
function onFail(e){
console.log("onFail..Err.." + e);
}
}, false);
好像不行,如果我拍本地jpeg就可以了!
你试过 Camera.destinationType.DATA_URL
了吗?
documentation 清楚地解释了如何调用 camera.getPicture
以获得 base64 输出(此代码来自文档):
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
注意:请注意,base64 操作非常占用内存,可能会冻结应用程序,尤其是在廉价手机中。