0

我正在尝试使用 ngCordova 的相机插件 ( http://ngcordova.com/docs/plugins/camera/ ) 从我的 iphone 获取图片并将其发送到我的服务器。后端开发人员希望我将其编码为 base64。我的数据模型看起来像这样

[{image_name:"foo", link: //base64 string}]

这就是我的代码的样子

$scope.getPictureFromGallery = function() {  $cordovaCamera.getPicture(options).then(function(imageURI) {


     $scope.image = imageURI;


    }, function(err) {
      // error
    });

  };
4

1 回答 1

2

文档指出,您只需设置destinationType 即可获得正确的结果。

module.controller('PictureCtrl', function($scope, $cordovaCamera) {

  document.addEventListener("deviceready", function () {

    var options = {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL, // <== HERE
      sourceType: Camera.PictureSourceType.CAMERA,
      allowEdit: true,
      encodingType: Camera.EncodingType.JPEG,
      targetWidth: 100,
      targetHeight: 100,
      popoverOptions: CameraPopoverOptions,
      saveToPhotoAlbum: false
    };

    $cordovaCamera.getPicture(options).then(function(imageData) {
      var image = document.getElementById('myImage');
      image.src = "data:image/jpeg;base64," + imageData; // <== HERE is how you create the actual string to send to server
    }, function(err) {
      // error
    });

  }, false);
});

我在这里有一个完整的工作示例https://github.com/aaronksaunders/dcww/blob/master/www/js/services.js#L39

于 2015-07-07T13:41:31.057 回答