需要计算图像的大小(以字节为单位) - 来自phonegap相机的图像 [英] Need to calculate the size of image in bytes - for images from phonegap camera

查看:157
本文介绍了需要计算图像的大小(以字节为单位) - 来自phonegap相机的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一项功能,其中我在从本机相机捕获图像后上传图像。这里,具有关于图像的大小的限制。因此,我需要以MB为单位计算图片大小。

  Camera.getPicture(gotPicture,cameraError,{
quality :50,
allowEdit:true,
targetWidth:200,
targetHeight:200,
destinationType:Camera.DestinationType.DATA_URL
}
camera
.getPicture(
gotPicture,
cameraError,{
quality:100,
allowEdit:true,
targetWidth:200,
targetHeight:200,
sourceType:Camera.PictureSourceType.PHOTOLIBRARY,
destinationType:Camera.DestinationType.DATA_URL
});

我发现很多建议保存为文件,但在这里我收到DATA URI(因为我需要传递Base64图片进行处理)。



有人可以帮助计算从PHOTO_GALLERY点击/引用的图片的实际大小。



我在调用Phonegap API时还有一个查询,是否在API级别进行了任何大小的压缩,



提前感谢!

解决方案

您可以获得图像的大小和Base64数据,如下所示: / p>

  function capturePhoto(){navigator.camera.getPicture(onPhotoDataSuccess,onFail, (functionURI('Failed');} function getSize(fileUri)function getSize(fileURI){getSize(imageURI);} function function getSize(fileURI) {window.resolveLocalFileSystemURL(fileUri,function(fileEntry){fileEntry.getMetadata(function(metadata){alert(size is+ metadata.size); },resOnError); fileEntry.file(function(file){var reader = new FileReader(); reader.onloadend = function(evt){alert(evt.target.result);}; reader.readAsDataURL(file);},resOnError); },resOnError);} function resOnError(error){alert(error); }  $ 



您可以通过设置<$ c> $ c> sourceType:Camera.PictureSourceType.PHOTOLIBRARY 。为了压缩大小,我建议您在 getPicture 中设置较低的质量。


I am working on a functionality, where I am uplaoding an image after capturing it from native camera. Here, have a limitation regarding size of the image. So, I need to calculate the image Size in MB.

Camera.getPicture(gotPicture, cameraError, {
                                quality: 50,
                                allowEdit: true,
                                targetWidth: 200,
                                targetHeight: 200,
                                destinationType: Camera.DestinationType.DATA_URL
                            });
camera
                        .getPicture(
                            gotPicture,
                            cameraError, {
                                quality: 100,
                                allowEdit: true,
                                targetWidth: 200,
                                targetHeight: 200,
                                sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                                destinationType: Camera.DestinationType.DATA_URL
                            });

I found lot of suggestions where it is being save as a file, but here I am receiving it as DATA URI(As I need to pass Base64 Image for processing).

Can someone help in calculating the actual size of the image clicked/referenced from PHOTO_GALLERY.

I have one more query as when I am calling the Phonegap API, Is there any compression of size is being done at the API level,

Thanks in advance!!!

解决方案

You can get both size and Base64 data of the image like following:

function capturePhoto() {
   navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
   destinationType: Camera.DestinationType.FILE_URI,
   saveToPhotoAlbum: true});
}
function onPhotoDataSuccess(imageURI) {
	getSize(imageURI);
}
function onFail(message) {
   alert('Failed');
}
function getSize(fileUri) {
    window.resolveLocalFileSystemURL(
          fileUri,
          function(fileEntry){
        	  fileEntry.getMetadata(function(metadata){
        		  alert("size is"+metadata.size);
        	  }, resOnError);
        	  fileEntry.file( function(file) {
  		        var reader = new FileReader();
  		        reader.onloadend = function(evt) {
  		            alert(evt.target.result);
  		        };
  		        reader.readAsDataURL(file);
  		    }, resOnError);
          },
          resOnError);
}
function resOnError(error) {
	alert("error");
   }

You can do this for Photo gallery by setting sourceType :Camera.PictureSourceType.PHOTOLIBRARY.And for compression of size I recommend you to set lower quality in getPicture.

这篇关于需要计算图像的大小(以字节为单位) - 来自phonegap相机的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆