限制上传的图像尺寸相机插件cordova ionic [英] Restrict uploaded image size Camera plugin cordova ionic

查看:120
本文介绍了限制上传的图像尺寸相机插件cordova ionic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用我的离子应用程序中的Camera插件获取了图像.我想在用户选择的图像大小上限制用户,比如说200kb.我已将相机作为文件"插件添加.

I have fetched the image using Camera plugin in my ionic app. I want to restrict the user on the size of the image which user is choosing, lets say 200kb. I have added Camera as File plugin.

我使用了以下代码:

/*Function to get image from gallery*/
$scope.getImageFromGallery = function(){
    var options = {
        quality: 100,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        targetWidth: 450,
        targetHeight: 450,
        encodingType: Camera.EncodingType.JPEG,
    };

    navigator.camera.getPicture(gallerySuccess, galleryError, options);

    function gallerySuccess(imageURI){
        getSize(imageURI);          
    }


    function getSize(fileUri) {
        window.resolveLocalFileSystemURL(fileUri, function(fileEntry){

            fileEntry.getMetadata(function(metadata){
                console.log("size is "+metadata.size);
            }, resOnError);

            fileEntry.file(function(file) {

                var reader = new FileReader();
                reader.onloadend = function(evt) {

                    var imgData = evt.target.result;
                    var res = imgData.split(",");
                    $rootScope.imagebase64 = res[1];
                    var image = document.getElementById('preview-image1');
                    image.src = evt.target.result;
                    $('#preview-image').css('display','block');
                    $('#preview-image').css('background-image', "url("+res[1]+")").show();
                };

                reader.readAsDataURL(file);
            }, resOnError);
        },
        resOnError);
    }

    function resOnError(error) {
        console.log("error "+JSON.stringify(error));
    }

    function galleryError(error) {
    }
}

但是在这里,当我选择2.5MB的图像时,它显示的大小为178908 当我选择大小为8.9MB的图像时,显示的大小为88412.

But here, when I'm choosing image of 2.5MB, it is showing size 178908 and when I'm choosing image of size 8.9MB, it is showing size 88412.

据我所知,文件大小以字节为单位,但是我得到的值不正确.

As per my knowledge, the file size is in bytes, but the values which I'm getting are not correct.

推荐答案

尝试此解决方案,对于您的150kb,只需将其更改为200kb

try this solution this is for 150kb for yours just change it to 200kb

  window.resolveLocalFileSystemURI(newPath, function (fileEntry) {
                        fileEntry.file(function (fileObj) {
                            if (fileObj.size <= 153600) {
                                var reader = new FileReader();
                                reader.onload = function () {
                                    var dataURL = reader.result;
                                    //your stuff.....
                                };
                                reader.readAsDataURL(fileObj);
                            }
                            else {
                                //  alert("Please upload image less then 150KB");
                                alertPopup = $ionicPopup.alert({
                                    title: 'Upload image failed!',
                                    template: 'Please upload image less then 150KB!'
                                });
                            }

                            console.log("Size = " + fileObj.size);
                        });
                    });

这篇关于限制上传的图像尺寸相机插件cordova ionic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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