yxl1108 发表于 2015-07-22 17:14

根据最大和最小宽高限制生成缩略图

在开发聊天APP时,咱们发送图片通常都需要进行压缩。这段代码帮助你生成适合大小的缩略图。
1. 对于极长或者极宽的图片进行缩放居中并裁剪。
2. 对于比例合适的图片则进行相应的缩放,使它满足最大和最小宽高限制。

代码:public class BitmapUtils {

    /**
   * 将原图缩放至合适的比例,比例异常将进行裁剪
   * @param source 原图
   * @param max 图片最大值
   * @param min 图片最小值
   * @return 缩放后的图片
   */
    public static Bitmap getScaleBitmap(Bitmap source, float max, float min) {
      System.out.println("--------------------------------------");
      float sourceHeight = source.getHeight();
      float sourceWidth = source.getWidth();

      float x, y, sourceX, sourceY;
      if (sourceHeight > sourceWidth) {
            y = sourceHeight;
            x = sourceWidth;
      } else {
            y = sourceWidth;
            x = sourceHeight;
      }
      sourceX = x;
      sourceY = y;

      System.out.println("y:" + y + " x:" + x + " sourceHeight:" + sourceHeight + " sourceWidth:" + sourceWidth);

      boolean needCrop = false;
      int crop = 0; // 1 = cropY 2=cropX
      float scale = 1;
      // 区分出需要裁剪的情况
      if ((y > max && x < min) || (y > max && x > min && x < max && (y - max > x - min))) {
            needCrop = true;
            System.out.println("需要进行裁剪");
            if (y > max && x < min) { // 1plan
                System.out.println("plan 1");
                y = max;
                x = min;
                scale = min / sourceX;
            } else {   // 2plan
                System.out.println("plan 2");
                y = max;
                x = sourceY;
                scale = max / sourceY;
            }
      } else {
            System.out.println("不需要进行裁剪");
            if (y < max && x > min && y < min && x < max) {// 3plan
                System.out.println("plan 3");
                y = sourceY;
                x = sourceX;
            } else if ((y > max && x > min && x < max && (y - max < x - min))) { // 4plan
                System.out.println("plan 4");
                y = max;
                scale = max / sourceY;
                x = scale * sourceX;
            } else if (y > max && x > max) {
                System.out.println("plan 5");
                y = max;
                scale = max / sourceY;
                x = scale * sourceX;
            } else if (y < min && x < min) {
                System.out.println("plan 6");
                x = min;
                scale = min / sourceX;
                y = scale * sourceY;
            } else {
                System.out.println("plan 7");
            }
      }

      System.out.println("y:" + y + " x:" + x + " 是否裁剪:" + needCrop + " scale:" + scale);
      System.out.println("--------------------------------------");

      // 转X,Y ->targetWidth,targetHeight
      float targetWidth, targetHeight;
      if (sourceHeight > sourceWidth) {
            targetHeight = y;
            targetWidth = x;
      } else {
            targetHeight = x;
            targetWidth = y;
      }

      System.out.println("targetHeight:" + targetHeight + " targetWidth:" + targetWidth);

      Bitmap target = Bitmap.createBitmap((int) targetWidth, (int) targetHeight, getConfig(source));
      Canvas canvas = new Canvas(target);
      if (needCrop) {
            if(sourceHeight > sourceWidth) {
                float ty = (targetHeight - sourceHeight * scale) / 2;
                System.out.println("竖向移动了:" + ty * scale);
                canvas.translate(0, ty);
            } else {
                float tx = (targetWidth - sourceWidth * scale) / 2;
                System.out.println("横向移动了:" + tx * scale);
                canvas.translate(tx, 0);
            }
      }
      canvas.scale(scale, scale);
      Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
      canvas.drawBitmap(source, 0, 0, paint);

      return target;
    }

    private static Bitmap.Config getConfig(Bitmap bitmap) {
      Bitmap.Config config = bitmap.getConfig();
      if (config == null) {
            config = Bitmap.Config.ARGB_8888;
      }
      return config;
    }
}

renxiao2003 发表于 2015-08-12 11:13

这个有什么实用性吗?
页: [1]
查看完整版本: 根据最大和最小宽高限制生成缩略图