Phonegap减少图像大小 [英] Phonegap reduce image size

查看:111
本文介绍了Phonegap减少图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个phonegap应用程序,它有多个输入字段,包括textboxes / textareas / signature / cad sketch / camera image。这些数据通过ajax帖子上传到服务器。

I have a phonegap application that has multiple input fields including textboxes/textareas/signatures/cad sketches/camera images. This data is uploaded to the server via an ajax post.

问题:

上传数据时,文件非常大,并且由于图像而需要花费大量时间上传。当没有添加照片时,大小约为200kb - 400kb,但是图像文件已经达到60Mb,并且在使用具有较差互联网连接的Android设备(信号不良的区域)方面,这是无法使用的。

When uploading the data the file is unreasonably large and takes a great deal of time to upload due to the images. When no photos are added the size is around 200kb - 400kb but with images the file has reached 60Mb and in terms of using an android device with a poor internet connection (Area with bad signal) this is unusable.

我尝试了什么:

我在相机选项中使用了图像压缩,如下面的摘录所示然而,我所面临的挑战是使用具有不同相机规格的不同设备,因此如果设备具有质量差的相机而不是具有高质量相机的设备,则将发生相应的图像压缩,因此图像质量将是可怕的。

I have used image compression in the camera options as seen in the below extract of code, however the challenge I am facing is that different devices with different camera specifications are utilized so if the device has a poor quality camera vs a device with a good quality camera image compression will take place regardless so the image quality will be terrible.

 var cameraOptions = {
                    destinationType: Camera.DestinationType.DATA_URL,
                    quality: 40,
                    encodingType: 0,
                };

参考phonegap相机API和选项:

Reference to the phonegap camera API and options:

http://docs.phonegap.com/en /2.0.0/cordova_camera_camera.md.html#Camera

通过阅读以下帖子,我推断出jpeg是最轻的格式,因此使用endodingType为0

From reading the below post I deduced that jpeg is the lightest format so used the endodingType of 0

PNG与GIF对比JPEG与SVG - 何时最佳使用?

我们想要实现的目标:

将图像大小限制为每张图像最大500kb,如果它大于压缩它的大小。

Limiting the image size to maximum of 500kb per image and if its larger then than that compress it.

推荐答案

我尝试了不同的相机选项,以下我从iPhone 5获得了4.5MB的文件大小:

I tried with different camera options and with the following I got file sizes of 4.5MB from an iPhone 5:

navigator.camera.getPicture(onSuccess, onFail, {
    destinationType : Camera.DestinationType.DATA_URL,
    sourceType : Camera.PictureSourceType.CAMERA,
    quality : 50,
    encodingType : Camera.EncodingType.JPEG
});

仅限添加行

correctOrientation : true

文件大小减少到~500kB。

File size reduced to ~500kB.

我没有任何解释为什么它应该以这种方式工作,但我建议你尝试它,如果质量属性似乎没有做任何事情。

I don't have any explanation to why it should work this way, but I suggest you try it if the quality property doesn't seem to do anything.

这篇关于Phonegap减少图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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