PhoneGap的EN code图像的base64 [英] Phonegap encode image base64
问题描述
我想带code图像为Base64并将其发送到服务器。当我检索图像所有它显示为空白。
I'm trying to encode an image to base64 and send it to a server. When I retrieve the image all it shows is blank.
在code到EN $ C $我使用了c那么它是这样的:
The code I'm using to encode it is this:
encodeImageUri = function(imageUri) {
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 = imageUri;
var dataURL = c.toDataURL("image/jpeg");
return dataURL.slice(22, dataURL.length);
}
来自:<一href=\"http://stackoverflow.com/questions/11188599/using-phonegap-how-to-get-base64-image-data-of-the-photo-chosen-from-photo-libr\">Using PhoneGap的,如何获得在iPhone 从照片库选择照片的base64图像数据
Taken from: Using PhoneGap, How to get base64 image data of the photo chosen from photo library in iPhone
推荐答案
我用下面的code将图像转换为Base64:
I use the following code to convert an image to Base64:
var Base64 = {
/**
* This implementation relies on Cordova 1.5 or above implementations.
*/
getBase64ImageFromInput : function (input, callback) {
var imageReader = new FileReader();
imageReader.onloadend = function (evt) {
if (callback)
callback(evt.target.result);
};
//Start the asynchronous data read.
imageReader.readAsDataURL(input);
},
formatImageSrcString : function (base64) {
return (base64.match(/(base64)/))? base64 : "data:image/jpeg;base64," + base64;
}
};
下面是一个使用这个对象将图像从一个文件输入转换为Base64编码的例子:
Below is an example using this object to convert an image from a file input to base64 encoding:
var fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.onchange = function () {
var input = this.files[0];
if (input) {
Base64.getBase64ImageFromInput(input, function (imageData) {
//Process the image string.
console.log(imageData);
});
} else {
alert("Please select an image.");
}
};
希望这有助于。让我知道如果你有任何问题。
Hope this helps. Let me know if you have any questions.
这篇关于PhoneGap的EN code图像的base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!