您的位置:首页 > 移动开发 > Android开发

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

2017-01-23 20:15 525 查看
第一部分:

不多说直接上代码,代码中在做仔细解释:
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代码  



/** 

 *  

 * 创建一个指定大小的缩略图 

 * @param source 源文件(Bitmap类型) 

 * @param width  压缩成的宽度 

 * @param height 压缩成的高度 

 */  

ThumbnailUtils.extractThumbnail(source, width, height);  

 

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

 

 

Java代码  



/** 

 * 创建一个指定大小居中的缩略图 

 *  

 * @param source 源文件(Bitmap类型) 

 * @param width  输出缩略图的宽度 

 * @param height 输出缩略图的高度 

 * @param options 如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件 

 * (除非缩略图等于@param source) 

 *  

 */  

 ThumbnailUtils.extractThumbnail(source, width, height, options);  

 

 

3、createVideoThumbnail(filePath, kind):

 

 

Java代码  



/** 

 * 创建一张视频的缩略图 

 * 如果视频已损坏或者格式不支持可能返回null 

 *  

 * @param filePath 视频文件路径  如:/sdcard/android.3gp 

 * @param kind kind可以为MINI_KIND或MICRO_KIND 

 *  

 */  

ThumbnailUtils.createVideoThumbnail(filePath, kind);  

 

PS: 此类不向下兼容
转自:http://jakend.iteye.com/blog/1153111

第三部分:


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

[java] view
plain copy

public void transImage(String fromFile, String toFile, int width, int height, int quality)  

    {  

        try  

        {  

            Bitmap bitmap = BitmapFactory.decodeFile(fromFile);  

            int bitmapWidth = bitmap.getWidth();  

            int bitmapHeight = bitmap.getHeight();  

            // 缩放图片的尺寸  

            float scaleWidth = (float) width / bitmapWidth;  

            float scaleHeight = (float) height / bitmapHeight;  

            Matrix matrix = new Matrix();  

            matrix.postScale(scaleWidth, scaleHeight);  

            // 产生缩放后的Bitmap对象  

            Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);  

            // save file  

            File myCaptureFile = new File(toFile);  

            FileOutputStream out = new FileOutputStream(myCaptureFile);  

            if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){  

                out.flush();  

                out.close();  

            }  

            if(!bitmap.isRecycled()){  

                bitmap.recycle();//记得释放资源,否则会内存溢出  

            }  

            if(!resizeBitmap.isRecycled()){  

                resizeBitmap.recycle();  

            }  

  

        }  

        catch (FileNotFoundException e)  

        {  

            e.printStackTrace();  

        }  

        catch (IOException ex)  

        {  

            ex.printStackTrace();  

        }  

    }  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: