[Android算法] bitmap 将图片压缩到指定的大小

第一部分:

不多说直接上代码,代码中在做仔细解释:

private void imageZoom() {
                //图片允许最大空间   单位:KB
                double maxSize =400.00;
                //将bitmap放至数组中,意在bitmap的大小(与实际读取的原文件要大)  
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] b = baos.toByteArray();
                //将字节换成KB
                double mid = b.length/1024;
                //判断bitmap占用空间是否大于允许最大空间  如果大于则压缩 小于则不压缩
                if (mid > maxSize) {
                        //获取bitmap大小 是允许最大大小的多少倍
                        double i = mid / maxSize;
                        //开始压缩  此处用到平方根 将宽带和高度压缩掉对应的平方根倍 (1.保持刻度和高度和原bitmap比率一致,压缩后也达到了最大大小占用空间的大小)
                        bitMap = zoomImage(bitMap, bitMap.getWidth() / Math.sqrt(i),
                                        bitMap.getHeight() / Math.sqrt(i));
                }
        }



/***
         * 图片的缩放方法
         * 
         * @param bgimage
         *            :源图片资源
         * @param newWidth
         *            :缩放后宽度
         * @param newHeight
         *            :缩放后高度
         * @return
         */
        public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
                        double newHeight) {
                // 获取这个图片的宽和高
                float width = bgimage.getWidth();
                float height = bgimage.getHeight();
                // 创建操作图片用的matrix对象
                Matrix matrix = new Matrix();
                // 计算宽高缩放率
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                // 缩放图片动作
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                                (int) height, matrix, true);
                return bitmap;
        }

压缩后的图片比预期要大10%  原因可能是bitmap图的宽高不一致,导致压缩误差!如有更好方法也请联系:690358889

希望能帮助各位


转自:http://www.eoeandroid.com/thread-174003-1-1.html

第二部分:

Android的图片压缩类ThumbnailUtils

从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework包下的android.media.ThumbnailUtils位置,可以帮助我们从mediaprovider中获取系统中的视频或图片文件的缩略图,该类提供了三种静态方法可以直接调用获取。

 

 

1、extractThumbnail (source, width, height)

 

 

Java代码   收藏代码
  1. /** 
  2.  *  
  3.  * 创建一个指定大小的缩略图 
  4.  * @param source 源文件(Bitmap类型) 
  5.  * @param width  压缩成的宽度 
  6.  * @param height 压缩成的高度 
  7.  */  
  8. ThumbnailUtils.extractThumbnail(source, width, height);  
 

2、extractThumbnail(source, width, height, options):

 

 

Java代码   收藏代码
  1. /** 
  2.  * 创建一个指定大小居中的缩略图 
  3.  *  
  4.  * @param source 源文件(Bitmap类型) 
  5.  * @param width  输出缩略图的宽度 
  6.  * @param height 输出缩略图的高度 
  7.  * @param options 如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件 
  8.  * (除非缩略图等于@param source) 
  9.  *  
  10.  */  
  11.  ThumbnailUtils.extractThumbnail(source, width, height, options);  
 

 

3、createVideoThumbnail(filePath, kind):

 

 

Java代码   收藏代码
  1. /** 
  2.  * 创建一张视频的缩略图 
  3.  * 如果视频已损坏或者格式不支持可能返回null 
  4.  *  
  5.  * @param filePath 视频文件路径  如:/sdcard/android.3gp 
  6.  * @param kind kind可以为MINI_KIND或MICRO_KIND 
  7.  *  
  8.  */  
  9. ThumbnailUtils.createVideoThumbnail(filePath, kind);  
 

PS: 此类不向下兼容

转自: http://jakend.iteye.com/blog/1153111


第三部分:

Android:指定分辨率和清晰度的图片压缩方法源码


[java]  view plain copy
  1. public void transImage(String fromFile, String toFile, int width, int height, int quality)  
  2.     {  
  3.         try  
  4.         {  
  5.             Bitmap bitmap = BitmapFactory.decodeFile(fromFile);  
  6.             int bitmapWidth = bitmap.getWidth();  
  7.             int bitmapHeight = bitmap.getHeight();  
  8.             // 缩放图片的尺寸  
  9.             float scaleWidth = (float) width / bitmapWidth;  
  10.             float scaleHeight = (float) height / bitmapHeight;  
  11.             Matrix matrix = new Matrix();  
  12.             matrix.postScale(scaleWidth, scaleHeight);  
  13.             // 产生缩放后的Bitmap对象  
  14.             Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 00, bitmapWidth, bitmapHeight, matrix, false);  
  15.             // save file  
  16.             File myCaptureFile = new File(toFile);  
  17.             FileOutputStream out = new FileOutputStream(myCaptureFile);  
  18.             if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){  
  19.                 out.flush();  
  20.                 out.close();  
  21.             }  
  22.             if(!bitmap.isRecycled()){  
  23.                 bitmap.recycle();//记得释放资源,否则会内存溢出  
  24.             }  
  25.             if(!resizeBitmap.isRecycled()){  
  26.                 resizeBitmap.recycle();  
  27.             }  
  28.   
  29.         }  
  30.         catch (FileNotFoundException e)  
  31.         {  
  32.             e.printStackTrace();  
  33.         }  
  34.         catch (IOException ex)  
  35.         {  
  36.             ex.printStackTrace();  
  37.         }  
  38.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值