zoukankan      html  css  js  c++  java
  • 图片压缩的方法

    参考下面的

    http://www.oschina.net/code/snippet_726985_22365

    http://gundumw100.iteye.com/blog/849729(好)

    /**
         * compress bitmap to less than long size,可以再研究
         * @param bitmap
         * @param long  size
         * @return compressed bitmap
         */
        public static Bitmap compressBitmap(Bitmap bitmap, long size ){
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG,100 , baos);//100表示不压缩,压缩后放在baos中
            Log.i("before compress", String.valueOf( baos.toByteArray().length/1024));
            
            if (bitmap==null|| baos.toByteArray().length<=size){
                Log.i("before", "bitmap null or < size");
                return bitmap;  //已经很小了
            }    
            
            int compressRatio=100; //压缩
            while (baos.toByteArray().length>size) {//如果压缩后大于100k,再压缩
                baos.reset();
                compressRatio=compressRatio-3;
                if(compressRatio<=10){
                    Log.i("compress", "break in while");
                    break;
                }        
                bitmap.compress(Bitmap.CompressFormat.JPEG,compressRatio , baos);
                Log.i("compress", "in while "+String.valueOf(baos.toByteArray().length/1024));
            }
            Log.i("after, baos ",String.valueOf(baos.toByteArray().length/1024));
            ByteArrayInputStream bois = new ByteArrayInputStream(baos.toByteArray());
            Bitmap newBitmap= BitmapFactory.decodeStream(bois,null,null);
            bitmap.recycle();
            return newBitmap;
        }
        
        public static long getSizeofBitmap(Bitmap bitmap) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 100表示不压缩质量
            long size = baos.toByteArray().length ;// 读出图片的kb大小
            return size;
        }
  • 相关阅读:
    浅读《构建之法》
    def 和 lamdba的区别
    Numpy和Pandas的区别
    Django:每点击一次就增加行可输入的表格
    F函数和Q函数的作用
    super()的用法
    liunx操作系统
    celery(超详细)
    celery
    FastDFS环境搭建
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4050900.html
Copyright © 2011-2022 走看看