我正在开发一个PhoneGap应用程序,使用
我获取图片的方式是:
在返回之前打印
你觉得可能是哪里做错了吗?
navigator.getPicture
方法来获取图像。我获取图片的方式是:
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI });
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
就像phonegap文档中的例子一样。
我想使用imageURI,然后将其转换为图像数据以便稍后上传。(我不想使用phonegap的FileTransfer)
到目前为止,我尝试了如何在JavaScript中获取图像数据?和如何在JavaScript中对字符串进行Base64编码?
当我尝试以下操作时:
function onSuccess(imageURI) {
getBase64Image(imageURI);
}
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to
// guess the original format, but be aware the using "image/jpg"
// will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
console.log(dataURL); //here is where I get 'data:,'
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
在返回之前打印
dataURL
。我得到的内容只有data:,
。你觉得可能是哪里做错了吗?