【问题标题】:How to convert this image to json in angularjs?如何在angularjs中将此图像转换为json?
【发布时间】:2015-12-16 11:14:14
【问题描述】:

我想在网络服务中发帖后用相机拍照

那么如何将此图像转换为Json格式

我的控制器改变了:

facebookExample.controller('MainCtrl', function($scope,$cordovaOauth, $localStorage, $location,$ionicPopup,$state,$http, Camera) { $scope.fileArray = []; $scope.imgContent = {}; $scope.imageStrings = [];

  $scope.getPhoto = function(files) {
    Camera.getPicture().then(function(imageURI) {
      console.log(imageURI);
      $scope.lastPhoto = imageURI;
      angular.forEach(files, function(flowFile, i) {
      var uri = imageURI;
      $scope.imageStrings[i] = uri;
      $scope.imgContent = {
                fileName: flowFile.name,
                fileContent: uri
            };
         fileReader.readAsDataURL(flowFile.file);
         $scope.fileArray.push($scope.imgContent);

       });

    }, function(err) {
      console.err(err);
    }, {
      quality: 75,
      targetWidth: 320,
      targetHeight: 320,
      saveToPhotoAlbum: false
    });

    console.log("JSON.stringify($scope.fileArray)"); 
    console.log(JSON.stringify($scope.fileArray));
  };

【问题讨论】:

    标签: angularjs json cordova ionic-framework


    【解决方案1】:

    你可以试试这个代码。

    $scope.fileArray = [];
    $scope.imgContent = {};
    $scope.imageStrings = [];
    
    $scope.processFiles = function(files) {
        angular.forEach(files, function(flowFile, i) {
            var fileReader = new FileReader();
            fileReader.onload = function(event) {
                var uri = event.target.result;
                $scope.imageStrings[i] = uri;
                $scope.imgContent = {
                    fileName: flowFile.name,
                    fileContent: uri
                };
            };
            fileReader.readAsDataURL(flowFile.file);
            $scope.fileArray.push($scope.imgContent);
        });
        console.log(JSON.stringify($scope.fileArray));
    };
    

    或者这里是jsfiddle

    【讨论】:

    • 总是显示这个结果 : !JavaScript LOG: [ ]
    【解决方案2】:

    我尝试过这种方式,并在我的一个项目中工作过。看看吧。 也许它也适合你!

       $cordovaCamera.getPicture(options).then(function(imagePath) 
        {
                    var promise = $service(imagePath);
                    promise.success(function(data) 
                    {
                        //after service
                    });
      }, function(error) 
         {
                    //An error occured
         });
       }
    

    这是一个示例服务

      $service=function(photo)
        {
            $http.defaults.headers.common["Accept"] = "application/json";
            $http.defaults.headers.post["Content-Type"] = "application/json";
    
          var data=JSON.stringify({photo:photo});
          return $http.post("your webservice url",data);
        };
    

    编辑1:忘记给你相机选项。给你!

    var options = {
                      quality: 100,
                      destinationType: Camera.DestinationType.DATA_URL,
                      sourceType: Camera.PictureSourceType.CAMERA,
                      encodingType: Camera.EncodingType.JPEG,
                      cameraDirection: Camera.Direction.FRONT,
                      correctOrientation: true,
                      saveToPhotoAlbum: true
                    };
    

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2021-09-24
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多