【发布时间】:2012-10-26 23:16:47
【问题描述】:
我正在开发一个 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)
到目前为止,我已经尝试过Get image data in JavaScript? 和How can you encode a string to Base64 in JavaScript?
当我尝试以下操作时,
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:, 作为内容。
关于我做错了什么有什么想法吗?
【问题讨论】:
-
欢迎来到 SQ。第一个问题很好! +1
标签: javascript android image cordova base64