如何将Cordova图像搜索器结果转换为Base64格式? [英] How to Convert Cordova Image Picker Results to Base64 format?

查看:112
本文介绍了如何将Cordova图像搜索器结果转换为Base64格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想知道如何将图像搜索器结果转换为Base64,同时通过 Cordova相机获取图像我可以获得base64格式数据,但在 cordova图像选择器中它无法正常工作

Hi I want to know how to Convert the Image Picker Results to Base64 , While Getting the image through Cordova Camera I can get base64 format data but in cordova image picker it does not working

我看过以下链接,并将其工作用于Cordova相机图像捕获,但不适用于cordova图像选择器

I have Seen the below link and applied its Working for Cordova Camera Image Capture but not working for cordova image picker

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

  }


推荐答案

pickimage返回URI不是图像数据。下面是应该做你想做的代码:

pickimage returns an URI not the image data. Here's the code that should do what you want:

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

};

这篇关于如何将Cordova图像搜索器结果转换为Base64格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆