Android-简单总结一下图片压缩

最近项目需要用到图片压缩,所以简单总结一下。大致分为三种压缩。

  1. 图片质量压缩。
    • 意思就是降低图片的质量,针对文件处理,但本身的像素点并不会减少。
      • 本来像素点是这样的,经过算法计算,若一个像素点周围所存的值都一样的话,则清除周围的像素点所存的值。
      • 代码如下
      •         /**
                 * 1. 质量压缩
                         设置bitmap options属性,降低图片的质量,像素不会减少
                         第一个参数为需要压缩的bitmap图片对象,第二个参数为压缩后图片保存的位置
                         设置options 属性0-100,来实现压缩
                 * @param bmp
                 * @param file
                 */
                public static void compressImageToFile(Bitmap bmp,File file) {
                    // 0-100 100为不压缩
                    int options = 20;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    // 把压缩后的数据存放到baos中
                    bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
                    try {
                        FileOutputStream fos = new FileOutputStream(file);
                        fos.write(baos.toByteArray());
                        fos.flush();
                        fos.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

  2.图片尺寸压缩。

    • 这个没什么好说的,就是通过缩放图片像素来达到减少图片占用内存的大小。
    • public static void compressBitmapToFile(Bitmap bmp, File file){
                  // 尺寸压缩倍数,值越大,图片尺寸越小
                  int ratio = 8;
                  // 压缩Bitmap到对应尺寸
                  Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888);
                  Canvas canvas = new Canvas(result);
                  Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
                  canvas.drawBitmap(bmp, null, rect, null);
      
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  // 把压缩后的数据存放到baos中
                  result.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);
                  try {
                      FileOutputStream fos = new FileOutputStream(file);
                      fos.write(baos.toByteArray());
                      fos.flush();
                      fos.close();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }

  3.图片采样率压缩。

    • 设置图片的采样率,降低图片像素
    • public static void compressBitmap(String filePath, File file){
                  // 数值越高,图片像素越低
                  int inSampleSize = 8;
                  BitmapFactory.Options options = new BitmapFactory.Options();
                  options.inJustDecodeBounds = false;
      //            options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
                  //采样率
                  options.inSampleSize = inSampleSize;
                  Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
      
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  // 把压缩后的数据存放到baos中
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);
                  try {
                      if(file.exists())
                      {
                          file.delete();
                      }
                      else {
                          file.createNewFile();
                      }
                      FileOutputStream fos = new FileOutputStream(file);
                      fos.write(baos.toByteArray());
                      fos.flush();
                      fos.close();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }

4.最后推荐一个图片压缩工具

  • 鲁班压缩(Luban)  https://github.com/Curzibn/Luban
  • Luban(鲁班)就是通过在微信朋友圈发送近100张不同分辨率图片,对比原图与微信压缩后的图片逆向推算出来的压缩算法。
原文地址:https://www.cnblogs.com/819158327fan/p/9300642.html