如何使用Cordova获取图像对象?
使用getPicture时,我们会得到一个相对URL或base64image。但是我想向AmazonS3发送一个图像对象。不管怎样,你想这么做吗 下面是我如何获得Base64图像的如何使用Cordova获取图像对象?,cordova,image-capture,Cordova,Image Capture,使用getPicture时,我们会得到一个相对URL或base64image。但是我想向AmazonS3发送一个图像对象。不管怎样,你想这么做吗 下面是我如何获得Base64图像的 // A button will call this function // function capturePhoto() { // Take picture using device camera and retrieve image as base64-encoded string naviga
// 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
});
}
[更新]现在我需要获取图像作为对象,将其转换为字节数组,并使用REST(PUT)调用将其上载到Amazon S3。当然可以
照相机/图像源代码
html
文件传输源代码
js
php
同样,您也可以将ByTestStream加载到图像标记
function(success) { // Assuming you are sending the bytestream from the code
var img = document.getElementById('image');
img.src = 'data:image/png;base64,' + success.bytestream;
}
谢谢。我将尝试:-)我尝试了这个方法,但我发现需要获取图像对象并将其转换为字节数组,然后作为REST调用(PUT)上传。多亏了你的帮助,我把代码改成了URL,并把图片储存在图库中。现在我需要以某种方式将其转换为对象并调用REST服务。我已经阅读了代码,在获得图像url后,我正在创建一个图像对象并设置src。现在我需要将其转换为字节数组,因为upload-REST调用希望图像位于字节数组中。这就是我现在所处的困境。有关于字节数组转换的想法吗?非常感谢您的帮助。您可以使用base64上传到您的数据库中
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
});
}
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
move_uploaded_file($_FILES["file"]["tmp_name"], '/path/to/file');
function(success) { // Assuming you are sending the bytestream from the code
var img = document.getElementById('image');
img.src = 'data:image/png;base64,' + success.bytestream;
}