如果您使用cordova-plugin-image-picker,您可以设置选项outputType: 1。这将返回 base64 格式的图像。
来自plugin 存储库:
options = {
// Android only. Max images to be selected, defaults to 15. If this is set to 1, upon
// selection of a single image, the plugin will return it.
maximumImagesCount: int,
// max width and height to allow the images to be. Will keep aspect
// ratio no matter what. So if both are 800, the returned image
// will be at most 800 pixels wide and 800 pixels tall. If the width is
// 800 and height 0 the image will be 800 pixels wide if the source
// is at least that wide.
width: int,
height: int,
// quality of resized image, defaults to 100
quality: int (0-100),
// output type, defaults to FILE_URIs.
// available options are
// window.imagePicker.OutputType.FILE_URI (0) or
// window.imagePicker.OutputType.BASE64_STRING (1)
outputType: int
};
记得在返回的字符串前加上data:image/jpeg;base64,。
那么,例如:
this.imagePicker.getPictures({
maximumImagesCount: 1,
width: 800,
height: 800,
quality: 70,
outputType: 1 }) //base64 output
.then((results) => {
return 'data:image/jpeg;base64,'+results[0];
});