如何将图像转换为base64字符串?

时间:2015-08-11 03:18:05

标签: angularjs cordova ionic-framework cordova-plugins

你好我试图在离子使用相机或从库或文件系统中选择文件进行简单的应用程序并发送到/或上传到服务器。 我找到了一个捕获一个图像并发送 base64 字符串的插件 这是插件 http://ngcordova.com/docs/plugins/camera/ 使用这个我能够得到base64字符串

$cordovaCamera.getPicture(options).then(function(imageData) {
      var image = document.getElementById('myImage');
      image.src = "data:image/jpeg;base64," + imageData;
    }, function(err) {
      // error
    });

我用来发送到服务器的base64字符串

但我的问题是,当我使用这个插件图片gallary插件

http://ngcordova.com/docs/plugins/imagePicker/ 它只向我发送图片网址(在内存中)。从图像选择器中选择图像后,我们可以获得 base64 字符串。

$cordovaImagePicker.getPictures(options)
    .then(function (results) {
      for (var i = 0; i < results.length; i++) {
        console.log('Image URI: ' + results[i]);
      }
    }, function(error) {
      // error getting photos
    });

换句话说,当我使用相机时,我得到了base64字符串,如上所示。但是当我使用图像选择器插件时,我得到图像url.can我得到base64字符串.so我能够在服务器上发送或上传。 如何将图片网址转换为base64字符串?

2 个答案:

答案 0 :(得分:2)

要将图片转换为base64,您可以在

中使用HTML5 canvans

How to convert image into base64 string using javascript

请参考以上问题

使用此代码

/**
 * Convert an image 
 * to a base64 url
 * @param  {String}   url         
 * @param  {Function} callback    
 * @param  {String}   [outputFormat=image/png]           
 */
function convertImgToBase64URL(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS'),
        ctx = canvas.getContext('2d'), dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
    img.src = url;
}

答案 1 :(得分:2)

试试这100%正常工作的代码。首先,您必须下载ngCordova.min.js文件并将其包含在index.html文件中。请遵守此代码。

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope,$cordovaCamera) {
	$scope.imgUrl;
	$scope.dataImg;
	$scope.tackPicture = 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
    };
	 $cordovaCamera.getPicture(options).then(function(imageData) {
      //var image = document.getElementById('myImage');
	  $scope.dataImg = imageData; // <--- this is your Base64 string 
      $scope.imgUrl = "data:image/jpeg;base64," + imageData;
    }, function(err) {
      // error
    });
	}
})

.controller('ChatsCtrl', function($scope, Chats) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  };
})

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  $scope.chat = Chats.get($stateParams.chatId);
})

.controller('AccountCtrl', function($scope) {
  $scope.settings = {
    enableFriends: true
  };
});
<ion-view view-title="Dashboard">
  <ion-content class="padding">
   <button class="button icon-left ion-ios-camera button-block button-positive" ng-click="tackPicture()">
   		OPEN CAMERA
   </button>
     <div class="item item-avatar">
    <img ng-src="{{ imgUrl }}">
   
    <p>{{ dataImg }}</p>
  </div>
  </ion-content>
</ion-view>

相关问题