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);
}
雖然這可行,但我不確定我會推薦它。文檔建議您使用相機結果文件,而不是base64字符串,因爲這些字符串可能*很大*並且會影響內存。如果您使用文件,則可以使用FileTransfer API輕鬆完成相同的POST。 –
我怎樣才能把這張照片放在一個文件中,用FileTransfer API發佈呢? – patrick
@patrick:我用php代碼更新了我的答案 –