论压缩图片核心技术,几种常见使用方式

第一种,(file----bitmap)就是加载本地图像资源时,由于图片比较大,有必要进行压缩处理

第二种,(bitmap---bitmap)就是压缩bitmap,比如我们从服务端上获取啦一个图片的bitmap,而这个bitmap可能比较大,因此也有进行压缩

 第三种,上传服务器(file---bitmap----btye),如果是上传到服务器端,使用base64或者IO流进行图片上传时,由于图片是可是是拍照来的,图片比较大

代码

第一种:

 


private Bitmap compressImageFromFile(String srcPath) {

                   BitmapFactory.OptionsnewOpts = new BitmapFactory.Options();

                   newOpts.inJustDecodeBounds= true;//只读边,不读内容

                   Bitmapbitmap = BitmapFactory.decodeFile(srcPath, newOpts);

 

                   newOpts.inJustDecodeBounds= false;

                   intw = newOpts.outWidth;

                   inth = newOpts.outHeight;

                   floathh = 800f;//

                   floatww = 480f;//

                   intbe = 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;//设置采样率

                  

                   newOpts.inPreferredConfig= Config.ARGB_8888;//该模式是默认的,可不设

                   newOpts.inPurgeable= true;// 同时设置才会有效

                   newOpts.inInputShareable= true;//。当系统内存不够时候图片自动被回收

                  

                   bitmap= BitmapFactory.decodeFile(srcPath, newOpts);



                   returnbitmap;

         }


第二种

              //压缩bitmap 可存入文件中

  private static BitmapcompressImage(Bitmap image,String finishPathh) {

  ByteArrayOutputStream baos = newByteArrayOutputStream();
               image.compress(Bitmap.CompressFormat.JPEG, 60, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
                int options = 60;
                while(baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
                       baos.reset();// 重置baos即清空baos
                       options -= 10;// 每次都减少10
                       image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中

                }
                ByteArrayInputStreamisBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到   ByteArrayInputStream中
                Bitmap bitmap =BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片

               FileOutputStream out = newFileOutputStream(finishPath);
             bitmap.compress(Bitmap.CompressFormat.PNG,60, out);

               return bitmap;

}

 第三种

     //file----bitmap   bitmap---byte[]    byte[]-----base64

public static Bitmap getSmallBitmap(String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(filePath, options);

 

        // Calculate inSampleSize

    options.inSampleSize = calculateInSampleSize(options, 480, 800);

 

        // Decode bitmap with inSampleSize set

    options.inJustDecodeBounds = false;

 

    return BitmapFactory.decodeFile(filePath, options);

    }

//  得到图片比例

   public static intcalculateInSampleSize(BitmapFactory.Options options,int reqWidth, intreqHeight) {

    final intheight = options.outHeight;

    final int width= options.outWidth;

    intinSampleSize = 1;

 

    if (height >reqHeight || width > reqWidth) {

             finalint heightRatio = Math.round((float) height/ (float) reqHeight);

             finalint widthRatio = Math.round((float) width / (float) reqWidth);

            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

    }

        returninSampleSize;

}

 

// bitmap------Io流

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);       //40代表压缩系数 100为不压缩
            byte[] b = baos.toByteArray();


// bitmap------base64

 String image=Base64.encodeToString(b, Base64.DEFAULT);   //b 为byte数组   将String类型image上传服务器  服务器可解码成图片


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值