gpt4 book ai didi

javascript - 如何在cordova-camera中获取相机图像的大小

转载 作者:行者123 更新时间:2023-11-30 06:52:49 24 4
gpt4 key购买 nike

所以,我正在使用这个 cordova 相机插件来使用相机拍照。由于我的应用程序有将近 10 个 div 可以触发相机,我想根据我发送的二进制字符串的大小配置服务器。我尝试了以下代码,它打印了二进制字符串而不是文件大小

 navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});

function onSuccess(imageData) {
//var finalimage = "data:image/jpeg;base64," + imageData;
window.resolveLocalFileSystemURI(imageData, function(fileEntry) {
fileEntry.file(function(fileObj) {
console.log("Size = " + fileObj.size);
});
});

此代码为我提供了很好的二进制字符串作为输出而不是文件大小。现在我还有另一个问题,在上面的评论中,我将图像转换为 base 64,那么 最终图像和 imageData 之间的文件大小是否会有任何变化

最佳答案

使用函数

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

// 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;


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
});
}

并在函数“允许编辑”中使用

您将获得完整的编辑和裁剪选项。!!

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

// 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
//
var x = 0;

function onPhotoDataSuccess(imageURI) {
x++;
// Uncomment to view the base64-encoded image data
console.log(imageURI);
alert(imageURI);
// Get image handle
//
var y = 'smallImage' + x;
var smallImage = document.getElementById(y);

// 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," + imageURI;
}

// Called when a photo is successfully retrieved
//
var z = 0;

function onPhotoURISuccess(imageURI) {
z++;
// Uncomment to view the image file URI
console.log(imageURI);
alert(imageURI);


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

// 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,
allowEdit: true,
destinationType: destinationType.FILE_URI,
sourceType: source
});
}

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

关于javascript - 如何在cordova-camera中获取相机图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31961830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com