Load contents of image from camera to a file

江枫思渺然 提交于 2019-12-19 06:40:12

问题


I am taking a picture (or selecting from library) using phonegap API using the following drictive:

MyApp.directive('Camera', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            elm.bind('click', function() {
                navigator.camera.getPicture(function (imageURI)
                {
                    scope.$apply(function() {
                        ctrl.$setViewValue(imageURI);
                    });
                }, function (err) {
                    ctrl.$setValidity('error', false);
                },
                //Options => http://docs.phonegap.com/en/2.6.0/cordova_camera_camera.md.html#Camera
                { quality: 50,
                  destinationType:Camera.DestinationType.FILE_URI                      
                })
            });
        }
    };
});

Which return me a URI that looks like, using ripple emulator on chrome, which I cann see pasting this URI.

blob:http%3A//localhost%3A8080/8e18de30-d049-4ce2-ae88-8500b444581e

My issue is loading this URI

$scope.updateUserProfile = function (user) {

       var myPicfile = $http.get(user.myPicture);

       dataService.uploadPicture . . . some code to update the picture to Parse

    }

*Note: I cannot use phonegap filetransfer together with parse.com :

When I do that I get:

I am making my request like:

uploadPicture: function uploadPicture(user,callback) { var serverUrl = 'https://api.parse.com/1/files/' + user.Nick ;

            $http({
                method: 'POST',
                url: serverUrl,
                data: user.myPicture,
                headers: {'X-Parse-Application-Id': PARSE_APP_ID,
                    'X-Parse-REST-API-Key': PARSE_REST_API_KEY,
                    'Content-Type': 'text/plain'
                }
            })

Any idea on how to get the content of the image to a file that then I can happily upload to Parse.com?

Thanks!


回答1:


I finally work it around, since my ultimate goal was to use it with phonegap, with the info in this post. . Big thanks to Raymond Camden!

function gotPic(data) {

window.resolveLocalFileSystemURI(data, function(entry) {

var reader = new FileReader();

reader.onloadend = function(evt) {
    var byteArray = new Uint8Array(evt.target.result);
    var output = new Array( byteArray.length );
    var i = 0;
    var n = output.length;
    while( i < n ) {
        output[i] = byteArray[i];
        i++;
    }                
    var parseFile = new Parse.File("mypic.jpg", output);

    parseFile.save().then(function(ob) {
            navigator.notification.alert("Got it!", null);
            console.log(JSON.stringify(ob));
        }, function(error) {
            console.log("Error");
            console.log(error);
        });

}

reader.onerror = function(evt) {
      console.log('read error');
      console.log(JSON.stringify(evt));
  }

entry.file(function(s) {
    reader.readAsArrayBuffer(s);
}, function(e) {
    console.log('ee');
});

});
}


来源:https://stackoverflow.com/questions/16298439/load-contents-of-image-from-camera-to-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!