2014-03-26 78 views
2

我正在使用Phonegap爲Android,Ios和Windows手機創建特定的應用程序。有了這個,我正在使用Camera API來捕獲手機中的圖片。這些目前沒有被存儲,並將在重新打開時被刪除。因此,我想訪問我的服務器來存儲圖片(例如以.jpeg格式)。這通過JavaScript/HTML。我如何創建這個文件並存儲它?如何將拍攝的照片存儲在我的服務器上

var pictureSource; //圖片來源 var destinationType; //將返回值的

// Wait for device API libraries to load 
// 
document.addEventListener("deviceready",onDeviceReady,false); 

// device APIs are available 
// 
function onDeviceReady() { 
    pictureSource=navigator.camera.PictureSourceType; 
    destinationType=navigator.camera.DestinationType; 
} 

// Called when a photo is successfully retrieved 
// 
function onPhotoDataSuccess(imageData) { 
    // Uncomment to view the base64-encoded image data 
    // console.log(imageData); 

    // Get image handle 
    // 
    var smallImage = document.getElementById('smallImage'); 

    // Unhide image elements 
    // 
    smallImage.style.display = 'block'; 

    // Show the captured photo 
    // The in-line CSS rules are used to resize the image 
    // 
    smallImage.src = "data:image/jpeg;base64," + imageData; 
} 

// Called when a photo is successfully retrieved 
// 
function onPhotoURISuccess(imageURI) { 
    // Uncomment to view the image file URI 
    // console.log(imageURI); 

    // Get image handle 
    // 
    var largeImage = document.getElementById('largeImage'); 

    // Unhide image elements 
    // 
    largeImage.style.display = 'block'; 

    // Show the captured photo 
    // The in-line CSS rules are used to resize the image 
    // 
    largeImage.src = imageURI; 
} 

// A button will call this function 
// 
function capturePhoto() { 
    // Take picture using device camera and retrieve image as base64-encoded string 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, 
    destinationType: destinationType.DATA_URL }); 
} 

// A button will call this function 
// 
function capturePhotoEdit() { 
    // Take picture using device camera, allow edit, and retrieve image as base64-encoded string 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, 
    destinationType: destinationType.DATA_URL }); 
} 

// A button will call this function 
// 
function getPhoto(source) { 
    // Retrieve image file location from specified source 
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI, 
    sourceType: source }); 
} 

// Called if something bad happens. 
// 
function onFail(message) { 
    alert('Failed because: ' + message); 
} 

回答

1

你可以得到FILE_URI然後使用PHP作爲例如像下面把它上傳到你的服務器的格式爲:

的PhoneGap代碼:

function uploadPhoto(imageURI) { 
      var options = new FileUploadOptions(); 
      options.fileKey="file"; 
      options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
      options.mimeType="image/jpeg"; 

      var params = new Object(); 
      params.value1 = "test"; 
      params.value2 = "param"; 

      options.params = params; 
      options.chunkedMode = false; 

      var ft = new FileTransfer(); 
      ft.upload(imageURI, "http://yourdomain.com/upload.php", win, fail, options); 
     } 

     navigator.camera.getPicture(uploadPhoto, function(message) { 
     alert('get picture failed'); 
    },{ 
      quality: 50, 
      destinationType: navigator.camera.DestinationType.FILE_URI, 
      sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY 
     } 
      ); 

PHP代碼(upload.php):

<?php 
print_r($_FILES); 
$new_image_name = "namethisimage.jpg"; 
move_uploaded_file($_FILES["file"]["tmp_name"], "/srv/www/upload/".$new_image_name); 
?> 
+1

雖然這可行,但我不確定我會推薦它。文檔建議您使用相機結果文件,而不是base64字符串,因爲這些字符串可能*很大*並且會影響內存。如果您使用文件,則可以使用FileTransfer API輕鬆完成相同的POST。 –

+1

我怎樣才能把這張照片放在一個文件中,用FileTransfer API發佈呢? – patrick

+0

@patrick:我用php代碼更新了我的答案 –

相關問題