- 浏览: 382820 次
最新评论
-
qq_19471875:
为了感谢楼主 我特意注册了一个账号!,谢谢!很实用!真棒
ViewPager刷新单个页面的方法 -
asdf658:
...
Eclipse安装server插件 -
JasonMichael:
多谢。搞定~
Eclipse安装server插件 -
passerby_whu:
应该是官网写错了。应该是144x144.
Android不太能够分辨率launcher icon的适配 -
zhengyong7232:
Create or replace function test ...
postgresql产生随机数和随机日期的存储过程
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成缩略图。
两种方法都实装在了我的项目中,结果却发现在质量压缩的模块中,本来1.9M的图片压缩后反而变成3M多了,很是奇怪,再做了进一步调查终于知道原因了。下面这个博客说的比较清晰:
android图片压缩总结
总结来看,图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;
最后把自己总结的工具类贴出来:
两种方法都实装在了我的项目中,结果却发现在质量压缩的模块中,本来1.9M的图片压缩后反而变成3M多了,很是奇怪,再做了进一步调查终于知道原因了。下面这个博客说的比较清晰:
android图片压缩总结
总结来看,图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;
最后把自己总结的工具类贴出来:
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; /** * Image compress factory class * * @author * */ public class ImageFactory { /** * Get bitmap from specified image path * * @param imgPath * @return */ public Bitmap getBitmap(String imgPath) { // Get bitmap through image path BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = false; newOpts.inPurgeable = true; newOpts.inInputShareable = true; // Do not compress newOpts.inSampleSize = 1; newOpts.inPreferredConfig = Config.RGB_565; return BitmapFactory.decodeFile(imgPath, newOpts); } /** * Store bitmap into specified image path * * @param bitmap * @param outPath * @throws FileNotFoundException */ public void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException { FileOutputStream os = new FileOutputStream(outPath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); } /** * Compress image by pixel, this will modify image width/height. * Used to get thumbnail * * @param imgPath image path * @param pixelW target pixel of width * @param pixelH target pixel of height * @return */ public Bitmap ratio(String imgPath, float pixelW, float pixelH) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true,即只读边不读内容 newOpts.inJustDecodeBounds = true; newOpts.inPreferredConfig = Config.RGB_565; // Get bitmap info, but notice that bitmap is null now Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 想要缩放的目标尺寸 float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了 float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了 // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 // 开始压缩图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(imgPath, newOpts); // 压缩好比例大小后再进行质量压缩 // return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除 return bitmap; } /** * Compress image by size, this will modify image width/height. * Used to get thumbnail * * @param image * @param pixelW target pixel of width * @param pixelH target pixel of height * @return */ public Bitmap ratio(Bitmap image, float pixelW, float pixelH) { ByteArrayOutputStream os = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, os); if( os.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 os.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; newOpts.inPreferredConfig = Config.RGB_565; Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了 float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了 //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 is = new ByteArrayInputStream(os.toByteArray()); bitmap = BitmapFactory.decodeStream(is, null, newOpts); //压缩好比例大小后再进行质量压缩 // return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除 return bitmap; } /** * Compress by quality, and generate image to the path specified * * @param image * @param outPath * @param maxSize target will be compressed to be smaller than this size.(kb) * @throws IOException */ public void compressAndGenImage(Bitmap image, String outPath, int maxSize) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); // scale int options = 100; // Store the bitmap into output stream(no compress) image.compress(Bitmap.CompressFormat.JPEG, options, os); // Compress by loop while ( os.toByteArray().length / 1024 > maxSize) { // Clean up os os.reset(); // interval 10 options -= 10; image.compress(Bitmap.CompressFormat.JPEG, options, os); } // Generate compressed image file FileOutputStream fos = new FileOutputStream(outPath); fos.write(os.toByteArray()); fos.flush(); fos.close(); } /** * Compress by quality, and generate image to the path specified * * @param imgPath * @param outPath * @param maxSize target will be compressed to be smaller than this size.(kb) * @param needsDelete Whether delete original file after compress * @throws IOException */ public void compressAndGenImage(String imgPath, String outPath, int maxSize, boolean needsDelete) throws IOException { compressAndGenImage(getBitmap(imgPath), outPath, maxSize); // Delete original file if (needsDelete) { File file = new File (imgPath); if (file.exists()) { file.delete(); } } } /** * Ratio and generate thumb to the path specified * * @param image * @param outPath * @param pixelW target pixel of width * @param pixelH target pixel of height * @throws FileNotFoundException */ public void ratioAndGenThumb(Bitmap image, String outPath, float pixelW, float pixelH) throws FileNotFoundException { Bitmap bitmap = ratio(image, pixelW, pixelH); storeImage( bitmap, outPath); } /** * Ratio and generate thumb to the path specified * * @param image * @param outPath * @param pixelW target pixel of width * @param pixelH target pixel of height * @param needsDelete Whether delete original file after compress * @throws FileNotFoundException */ public void ratioAndGenThumb(String imgPath, String outPath, float pixelW, float pixelH, boolean needsDelete) throws FileNotFoundException { Bitmap bitmap = ratio(imgPath, pixelW, pixelH); storeImage( bitmap, outPath); // Delete original file if (needsDelete) { File file = new File (imgPath); if (file.exists()) { file.delete(); } } } }
发表评论
-
Cocos2dx开发解决undefined reference to 'atof'和x86平台下报internal compiler error的错误
2016-11-21 17:10 2783最近在为游戏做java sdk的cocos2dx引擎层代码时遇 ... -
Android项目集成Jenkins(JUnit test & Coverage)
2016-08-26 14:12 4362为了实现持续集成,提 ... -
Android短信监听功能(解决onChange触发两次的问题)
2016-06-16 18:51 6849前言 项目要做短信验证码自动填充的功能,基本上两种方法:Con ... -
Android实现可自动关闭的定时器
2015-12-03 18:54 1401之前一篇文章里有用到过一个封装好的定时器工具类,现在又做了一些 ... -
Android不太能够分辨率launcher icon的适配
2015-04-24 11:01 1908网上讲android适配不同分辨率的文章很多,但是很少有说明不 ... -
Android 根据屏幕尺寸适配控件尺寸(按比例缩放)
2015-04-03 18:28 4633在做facebook登录时,正好看到其SDK中一段代码,可以根 ... -
Android获取状态栏高度
2015-01-28 12:04 1192获取状态栏高度有两种方法: 1.如果是在Activity中: ... -
Apktool打包和解包
2014-12-02 17:49 0本文的学习内容参考自[Android实例] 【eoeAndro ... -
Android 控件自动“移入、暂停、移出”效果的实现
2014-09-05 09:54 2387一个常见的效果:控件自动移入屏幕,停留几秒,再移出屏幕。项目中 ... -
Google Map 如何捕获onTouchEvent
2014-09-02 17:42 1713当我的项目中需要捕获google map的touch事件时,才 ... -
Android日期时间选择器实现以及自定义大小
2014-08-27 20:01 62556本文主要讲两个内容:1.如何将DatePicker和TimeP ... -
ViewPager刷新单个页面的方法
2014-08-22 11:09 39083使用ViewPager做滑动切换图片的效果时,如果图片是从 ... -
Android使用MediaPlayer开发时抛IllegalStateException
2014-08-18 16:45 59792在我开发的语音播放程序中,首次播放语音没问题,第二次播放时 ... -
Android 含有图片和文字的Button的实现
2014-07-17 20:15 8853要实现一个同时包含图片和文字的按钮,粗糙一点的做法当然是直 ... -
LinearLayout半透明效果
2014-07-16 18:12 17430透明效果有很多中实现方式,可以代码实现,也可以直接在布局文件中 ... -
Google Map无法显示:Error contacting Google servers. XXX authentication issue
2014-06-30 20:32 2080在开发google map时遇到的问题: 06-26 14 ... -
【转载】Android异步处理
2013-06-20 12:12 883关于Android异步处理的一整个系列的博文,共有4篇,博主写 ... -
android 写log到文件
2013-06-14 17:31 14210网上找的一个很强大的实现方法,原网页的链接找不到了,没法转载, ... -
android service被系统回收的解决方法
2013-06-14 11:20 7214自己的app的service总是容 ... -
Android 中的 Service 全面总结
2013-06-13 16:42 889关于Android Service的知识,可以参考以下博文,内 ...
相关推荐
android上传图片压缩图片大小
首先通过尺寸压缩,压缩到手机常用的一个分辨率(1280*960 微信好像是压缩到这个分辨率),然后我们要把图片压缩到100KB以内,通过质量压缩来计算options需要设置为多少,最后调用JNI压缩,这边我测试了下,压缩出来的...
使用质量压缩、尺寸压缩、采样率压缩以及转换为RGB565等四种方式压缩图片
Luban(鲁班)——Android图片压缩工具,仿微信朋友圈压缩策略。项目描述目前做app开发总绕不开图片这个元素。但是随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题。单纯对图片进行裁切,压缩已经有很多...
Android压缩图片,但是不改变图片的质量。
Android图片压缩结合多种压缩方式,常用的有尺寸压缩、质量压缩以及通过JNI调用libjpeg库来进行压缩,三种方式结合使用实现指定图片内存大小,清晰度达到最优。
android 照片获取 压缩存储代码 经过实际项目测试相当稳定
Android图片压缩解决方案,解决图片加载过程中ANR现象
压缩图片到 200h*200w 高自定义性
主要通过尺寸压缩和质量压缩,以达到清晰度最优,该项目参考了 https://github.com/zetbaitsu/Compressor 的部分代码,且在基础上修正了部分 bug 效果图 ⊙开源不易,希望给个 star 或者 fork 奖励 ⊙拥抱开源:...
android jpeg图片压缩
但是裁切成多少,压缩成多少却很难控制好,裁切过头图片太小,质量压缩过头则显示效果太差。 于是自然想到App巨头“微信”会是怎么处理,Luban(鲁班)就是通过在微信朋友圈发送近100张不同分辨率图片,对比原图与...
上传图片时按照服务器限制对图片进行压缩,工具类进行了宽度、高度、图片大小进行循环判断压缩。供大家参考如有问题欢迎指出,积分缺乏忘老板们体谅
Android资源图片压缩工具 可以减小apk包的体积 批文件已经写好 将图片拖动到Drag PNG here to reduce palette to 256,这个是我感觉比较好的 批文件大家可以按需求写
android图片压缩的处理. 本代码只能用于学习使用; 不得以任何商业手段进行营利销售; 谢谢大家合作; 感谢同学们支持.
Luban Android图片压缩工具,仿微信朋友圈压缩策略。来源:https://github.com/Curzibn/Luban。在其基础上解决6.0以上权限问题。
这是用eclipse开发的用于图片压缩的Demo,对这个资源介绍的博客为:http://blog.csdn.net/maolin0815/article/details/51477200
将压缩前与压缩后的图片效果预览出来,并可在手机文件管理中比对两者实际大小。
Android图片压缩 尽量不失真100k左右.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。