免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4509 | 回复: 1
打印 上一主题 下一主题

根据最大和最小宽高限制生成缩略图 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-22 17:14 |只看该作者 |倒序浏览
在开发聊天APP时,咱们发送图片通常都需要进行压缩。这段代码帮助你生成适合大小的缩略图。
1. 对于极长或者极宽的图片进行缩放居中并裁剪。
2. 对于比例合适的图片则进行相应的缩放,使它满足最大和最小宽高限制。

代码:
  1. public class BitmapUtils {

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

  13.         float x, y, sourceX, sourceY;
  14.         if (sourceHeight > sourceWidth) {
  15.             y = sourceHeight;
  16.             x = sourceWidth;
  17.         } else {
  18.             y = sourceWidth;
  19.             x = sourceHeight;
  20.         }
  21.         sourceX = x;
  22.         sourceY = y;

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

  24.         boolean needCrop = false;
  25.         int crop = 0; // 1 = cropY 2=cropX
  26.         float scale = 1;
  27.         // 区分出需要裁剪的情况
  28.         if ((y > max && x < min) || (y > max && x > min && x < max && (y - max > x - min))) {
  29.             needCrop = true;
  30.             System.out.println("需要进行裁剪");
  31.             if (y > max && x < min) { // 1plan
  32.                 System.out.println("plan 1");
  33.                 y = max;
  34.                 x = min;
  35.                 scale = min / sourceX;
  36.             } else {     // 2plan
  37.                 System.out.println("plan 2");
  38.                 y = max;
  39.                 x = sourceY;
  40.                 scale = max / sourceY;
  41.             }
  42.         } else {
  43.             System.out.println("不需要进行裁剪");
  44.             if (y < max && x > min && y < min && x < max) {// 3plan
  45.                 System.out.println("plan 3");
  46.                 y = sourceY;
  47.                 x = sourceX;
  48.             } else if ((y > max && x > min && x < max && (y - max < x - min))) { // 4plan
  49.                 System.out.println("plan 4");
  50.                 y = max;
  51.                 scale = max / sourceY;
  52.                 x = scale * sourceX;
  53.             } else if (y > max && x > max) {
  54.                 System.out.println("plan 5");
  55.                 y = max;
  56.                 scale = max / sourceY;
  57.                 x = scale * sourceX;
  58.             } else if (y < min && x < min) {
  59.                 System.out.println("plan 6");
  60.                 x = min;
  61.                 scale = min / sourceX;
  62.                 y = scale * sourceY;
  63.             } else {
  64.                 System.out.println("plan 7");
  65.             }
  66.         }

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

  69.         // 转X,Y ->targetWidth,targetHeight
  70.         float targetWidth, targetHeight;
  71.         if (sourceHeight > sourceWidth) {
  72.             targetHeight = y;
  73.             targetWidth = x;
  74.         } else {
  75.             targetHeight = x;
  76.             targetWidth = y;
  77.         }

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

  79.         Bitmap target = Bitmap.createBitmap((int) targetWidth, (int) targetHeight, getConfig(source));
  80.         Canvas canvas = new Canvas(target);
  81.         if (needCrop) {
  82.             if(sourceHeight > sourceWidth) {
  83.                 float ty = (targetHeight - sourceHeight * scale) / 2;
  84.                 System.out.println("竖向移动了:" + ty * scale);
  85.                 canvas.translate(0, ty);
  86.             } else {
  87.                 float tx = (targetWidth - sourceWidth * scale) / 2;
  88.                 System.out.println("横向移动了:" + tx * scale);
  89.                 canvas.translate(tx, 0);
  90.             }
  91.         }
  92.         canvas.scale(scale, scale);
  93.         Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
  94.         canvas.drawBitmap(source, 0, 0, paint);

  95.         return target;
  96.     }

  97.     private static Bitmap.Config getConfig(Bitmap bitmap) {
  98.         Bitmap.Config config = bitmap.getConfig();
  99.         if (config == null) {
  100.             config = Bitmap.Config.ARGB_8888;
  101.         }
  102.         return config;
  103.     }
  104. }
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-08-12 11:13 |只看该作者
这个有什么实用性吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP