如何将Cordova图像搜索器结果转换为Base64格式?

时间:2016-08-30 07:34:43

标签: angularjs cordova ionic-framework cordova-plugins

您好我想知道如何将图像搜索器结果转换为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 Image Capture工作

$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) {
    });

  }

1 个答案:

答案 0 :(得分:2)

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) {
    });

};