2014-10-02 4 views
3

При использовании getPicture мы получаем относительный URL или base64image. Но я хочу отправить объект изображения в Amazon S3. В любом случае, чтобы это сделать?Как получить объект изображения с помощью Кордовы?

Вот как я получаю base64image

// 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, { 
     destinationType: Camera.DestinationType.DATA_URL, 
     quality: 50 
    }); 
} 

[UPDATE] Теперь мне нужно, чтобы получить изображение объекта преобразовать его в массив байтов и загрузить его на Amazon S3 с помощью REST (PUT) вызова.

ответ

5

, что вы можете

камеры/источник изображения код

HTML

<!DOCTYPE html> 

<html> 
<head> 
    <meta charset="utf-8"> 
    <meta content="telephone=no" name="format-detection"> 
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
    <meta content= 
    "user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" 
    name="viewport"> 
    <script src="cordova.js" type="text/javascript"></script> 
    <script src="js/index.js" type="text/javascript"></script> 
    <title>Camera Cordova Plugin</title> 
</head> 

<body> 
    <button onclick="capturePhoto();">Capture Photo</button><br> 
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo 
    Library</button><br> 
    <img id="image" src="" style="display:none;width:100%;"> 
</body> 
</html> 

JS

function onPhotoDataSuccess(imageURI) { 
    // Uncomment to view the base64-encoded image data 
    console.log(imageURI); 
    // Get image handle 
    // 
    var cameraImage = document.getElementById('image'); 
    // Unhide image elements 
    // 
    cameraImage.style.display = 'block'; 
    // Show the captured photo 
    // The inline CSS rules are used to resize the image 
    // 
    cameraImage.src = imageURI; 
} 

function capturePhoto() { 
    // Retrieve image file location from specified source 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
     quality: 30, 
     targetWidth: 600, 
     targetHeight: 600, 
     destinationType: destinationType.FILE_URI, 
     saveToPhotoAlbum: true 
    }); 
} 

источник передачи файлов код

JS

function upload() { 
    var img = document.getElementById('image'); 
    var imageURI = img.src; 
    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); 
    options.mimeType = "image/jpeg"; 
    var params = new Object(); 
    options.params = params; 
    options.chunkedMode = false; 
    var ft = new FileTransfer(); 
    ft.upload(imageURI, "https://www.example.com/upload.php", win, fail, 
     options); 
} 

function win(r) { 
    console.log("Code = " + r.responseCode); 
    console.log("Response = " + r.response); 
    console.log("Sent = " + r.bytesSent); 
} 

function fail(error) { 
    alert("An error has occurred: Code = " + error.code); 
    console.log("upload error source " + error.source); 
    console.log("upload error target " + error.target); 
} 

PHP

<?php 
move_uploaded_file($_FILES["file"]["tmp_name"], '/path/to/file'); 

Ссылка:

http://blog.revivalx.com/2014/05/03/tutorial-camera-cordova-plugin-for-ios-and-android/ http://blog.revivalx.com/2014/07/12/upload-image-using-file-transfer-cordova-plugin-for-ios-and-android/

+1

Большое спасибо. Я попробую :-) –

+0

Я пробовал это, но я узнал, что требование состоит в том, чтобы получить объект изображения и преобразовать его в массив байтов, а затем загрузить как REST-вызов (PUT). Благодаря вашей помощи я изменил свой код на URL и сохранил изображение в галерее. Теперь мне нужно каким-то образом преобразовать его в объект и вызвать службу REST. –

+0

вы можете загрузить исходный код из блога –

0

Аналогично вы можете также загрузить потоковые изображения тэга

 function(success) { // Assuming you are sending the bytestream from the code 

     var img = document.getElementById('image'); 
     img.src = 'data:image/png;base64,' + success.bytestream; 
     } 
Смежные вопросы