【问题标题】:How to Convert Cordova Image Picker Results to Base64 format?如何将 Cordova 图像选择器结果转换为 Base64 格式?
【发布时间】:2016-08-30 18:22:02
【问题描述】:

您好,我想知道如何将图像选择器结果转换为 Base64,通过 Cordova 相机获取图像时,我可以获得 base64 格式的数据,但在 cordova 图像选择器中它不起作用

我已经看到下面的链接并应用了它的 Working for Cordova Camera Image Capture 但不适用于 Cordova 图像选择器

http://stackoverflow.com/questions/29456897/cordova-image-picker-convert-to-base64

不适用于科尔多瓦图像选择器

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

  }

【问题讨论】:

    标签: angularjs cordova ionic-framework cordova-plugins


    【解决方案1】:

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

    【讨论】:

    • @coding-dude.com 当我用ionic serveionic cordova run browser 运行它时它直接返回base64,当我在android ionic cordova run android 上运行它时它返回img URI,如何解决那个?
    猜你喜欢
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2020-09-03
    • 2018-03-22
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多