Phonegap: How to save an image taken and transformed into Base64 in the Bank?

Asked

Viewed 625 times

0

I take a photo with an android application and turn into Base64, I would like to know how to save this Base64 in the bank along with other information I have on the page...

This is my code that takes the photos and turns into Base64:

var pictureSource;   // picture source
var destinationType; // sets the format of returned value


document.addEventListener("deviceready",onDeviceReady,false);
document.addEventListener("deviceready",onDeviceReady2,false);


function onDeviceReady() {
  pictureSource=navigator.camera.PictureSourceType;
  destinationType=navigator.camera.DestinationType;

}

 function onDeviceReady2() {
 navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

function onPhotoDataSuccess(imageData) {

var smallImage = document.getElementById('smallImage');

smallImage.style.display = 'block';

smallImage.src = "data:img/jpeg;base64," + imageData; 

}


function capturePhoto() {

navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
quality: 60,
destinationType: destinationType.DATA_URL,
allowEdit: true,
correctOrientation: true,
sourceType: Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 3000,
saveToPhotoAlbum:1

 });
}

function onFail(message) {
alert('Failed because: ' + message);
}

This function will call through a button and it will send the data of a form, my location and the image taken(but I am sending the image wrong)

function enviar(){
  var formula = $('#formCliente').serialize();
  var lon = document.getElementById("myDivLo").innerHTML;
  var lat = document.getElementById("myDivL").innerHTML;
  var img = document.getElementById('smallImage').innerHTML;


 $.ajax({

  type:'POST',
  data:formula + ' &lat=' + lat  + "&lon=" + lon+ "&img=" + img ,

    url:'http://ip/teste/www/criar_incidente_camera.php',

 success: function(data){


 if(data == '' || data == 0){
     alert('Usuário ou senha incorreto' + data);   
     window.location = "";
 }

if(data == 1){
     window.location.href = "mapa_incidente.html";   
    }

    else{
         alert('outro' + data); 
       }


     }


  });

}
</script>

Entering values in the bank:

 $sql = 'INSERT INTO incidente (titulo, descricao, anonimo, tipo,   
 latitude, longitude, foto)';
 $sql .= 'VALUES (:titulo, :descricao, :anonimo, :tipo, :latitude, 
 :longitude, :foto)';

try {

$recebeConexao = $pdo->prepare($sql);

$recebeConexao->bindParam(':titulo', $_POST['titulo'], 
PDO::PARAM_STR);
$recebeConexao->bindParam(':descricao', $_POST['descricao'], 
PDO::PARAM_STR);
$recebeConexao->bindParam(':anonimo', $_POST['anonimo'], 
PDO::PARAM_STR);
$recebeConexao->bindParam(':tipo', $_POST['tipo'], PDO::PARAM_STR);
$recebeConexao->bindParam(':latitude', $_POST['lat'], PDO::PARAM_STR);
$recebeConexao->bindParam(':longitude', $_POST['lon'], 
PDO::PARAM_STR);
$recebeConexao->bindParam(':foto', $_POST['img'], PDO::PARAM_STR);



  $recebeConexao->execute();

  if($recebeConexao == true){

  $cadastro = 1;
 }else{
 $cadastro = 0;
}


} catch (PDOException $ex) {
echo "Erro inserção";
}



echo (json_encode($cadastro));

I’m using the BLOB Bank to store the image... Thank you!

  • @Marcosregis Sorry to ask, but I’m new to programming... In this part of the code you showed: form.append('photos[]', fileInput.files[0]); ?

  • Ignore the previous message. You need to see what is coming in http://ip/teste/www/criar_incidente_camera.php How are you retrieving the information??

  • @Marcosregis Do you want to know how my php file is? If it is I edited up and inserted the php part.

  • In this case, if you are in Base64 the field does not need to be BLOB. Were you able to save any information in the Database? Debug the $_POST variable to see what’s in it when you try to send the data.

  • All other information I’m sending is being saved in the bank, only the photo (Base64) that I can’t save...

  • must be a problem of serialization...

  • Why the negative votes on that question?

Show 2 more comments

2 answers

0

In his job onPhotoDataSuccess you save the Base64 data on src of the element smallImage:

smallImage.src = "data:img/jpeg;base64," + imageData; 

But in function enviar you’re catching the innerHTML of this element and sending via Ajax:

var img = document.getElementById('smallImage').innerHTML;

$.ajax({

  type:'POST',
  data:formula + ' &lat=' + lat  + "&lon=" + lon+ "&img=" + img ,

I suppose then that this value is going empty, before it even reaches the server. Send the value of src, and this particular problem (of not saving anything in the bank) should solve (whether there are other problems or not with the BLOB field, I can’t say):

var img = document.getElementById('smallImage').src;
  • I tried to switch to src, but it didn’t work :/

  • @Amandapistolato Ok, but at least the data is going to the server? Like, if you do console.log(img) what appears on the console of browser? Or, on the PHP side, if you print $_POST['img'] somewhere, what appears?

-1

in a simplified way, you can store the image using inside the onPhotoDataSuccess function: localStorage.setItem("imageURI", imageURI);

and then just retrieve the value before making the AJAX call:

var img = localStorage.getItem("imageURI");

$.ajax({
type:'POST',
data:formula + ' &lat=' + lat  + "&lon=" + lon+ "&img=" + img ,

Browser other questions tagged

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