How to grab image from camera in Base64 (using phonegap) and send to server via JSON with Angularjs?

Asked

Viewed 466 times

1

I’m getting the hang of imgEnviar, who receives the base64 coming from the camera, and inserting into the JSON and sending to the server with $http with Angularjs. I want to know if it’s really possible or if there’s a better way to do it, because I need to send the camera image along with the form data.

Camera part with Cordova plugin

var imgEnviar;
function capturarImagem(){
   navigator.camera.getPicture(onSuccess, onFail,
                    {
                        destinationType : Camera.DestinationType.DATA_URL,
                        sourceType : Camera.PictureSourceType.CAMERA
                    }
                );
            }

            function onSuccess(imageURL) {
                var image = document.getElementById('htmlImagem');
                image.src = "data:image/jpeg;base64," + imageURL;
                //var imgEnviar = JSON.stringify(imageURL); 
                imgEnviar = image.src;
            }

            function onFail(message) {
                alert('Erro: ' + message);
            }
pate do agularJS


app.controller('formularioChamado', function($scope, $http) {
 $(document).ready(function() {
  $('select').material_select();
});

 $scope.enviarForm = function(chamado){


  $http({
    url: 'https://modulosamu.herokuapp.com/chamado/store',
    method: 'POST',
    data: {

      descricao: $scope.chamado.descricao,
      img: imgEnviar,
      latitude: latitude,
      longitude: longitude,


    },
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'

    }
  }).
  success(function (data) {
    $scope.success = true;
    alert(data);
    $scope.user = {};
  }).
  error(function (data) {
    $scope.error = true;

  }); 

}

});
  • Use a service I did in Angularjs https://github.com/marangonijunior/bananaJs/blob/master/bananaJs.js Very quiet to access some plugins. *You need to have the plugins installed ok. Success!

1 answer

0

You can do it, but you’ll have to set up some things more like:

navigator.camera.getPicture(onSuccess, onFail, { 
        quality         : 25, 
        allowEdit       : false,
        //destinationType : Camera.DestinationType.FILE_URI, 
        destinationType : Camera.DestinationType.DATA_URL, 
        targetWidth     : 200,
        targetHeight    : 200,
        sourceType      : Camera.PictureSourceType.CAMERA, // Camera.PictureSourceType.PHOTOLIBRARY
        encodingType    : Camera.EncodingType.JPEG
    });

Quality the smaller the better to send, width/height same idea, then just send as string and write to the bank.

  • Thanks, but the way I did above already works, my problem was the server that was not being able to save the data in Base64 due to the file size. Because of this we put the database field in mediumBlob to increase the size of the field in the database, before the field was only blob.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.