您好,我想知道如何将图像选择器结果转换为 Base64 ,通过 Cordova 相机获取图像时,我可以获得 Base64 格式数据,但在 cordova 图像选择器中它不起作用
我已看到以下链接并应用了其“适用于 Cordova 相机图像捕获”,但不适用于 Cordova 图像选择器
http://stackoverflow.com/questions/29456897/cordova-image-picker-convert-to-base64
不适用于 Cordova 图像选择器
$scope.Pick=function(){
var options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
$cordovaImagePicker.getPictures(options)
.then(function (results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
}
$scope.results=results;
}, function(error) {
});
};
为 Cordova 图像捕获工作
$scope.captureimage=function()
{
var options = {
quality: 100,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
$scope.cimage=imageData;
alert($scope.cimage);
}, function(err) {
});
}
最佳答案
pickimage 返回 URI,而不是图像数据。这是应该执行您想要的操作的代码:
$scope.Pick=function(){
var options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
$cordovaImagePicker.getPictures(options)
.then(function (results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
window.resolveLocalFileSystemURI(results[i],
function (fileEntry) {
// convert to Base64 string
fileEntry.file(
function(file) {
//got file
var reader = new FileReader();
reader.onloadend = function (evt) {
var imgData = evt.target.result; // this is your Base64 string
};
reader.readAsDataURL(file);
},
function (evt) {
//failed to get file
});
},
// error callback
function () { }
);
}
$scope.results=results;
}, function(error) {
});
};
关于angularjs - 如何将 Cordova 图像选择器结果转换为 Base64 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39221459/