现在的位置: 首页 > 综合 > 正文

android将图片转化为指定的宽和高

2013年10月08日 ⁄ 综合 ⁄ 共 671字 ⁄ 字号 评论关闭

    需求:开发中服务器端要求把获取到的图片压缩处理,转化为指定的宽和高,例如:需要上传宽100,高200的图片

    在android2.2提供了一个API可以直接实现

Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(bmp, 100, 200);

但是因为项目需求需要兼容2.2一下的版本,这时需要另外一种方式,使用Bitmap.createBitmap方法生成指定宽高的图片

	/**
	 *  处理图片 
	 * @param bm 所要转换的bitmap
	 * @param newWidth新的宽
	 * @param newHeight新的高  
	 * @return 指定宽高的bitmap
	 */
	 public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){  
	    // 获得图片的宽高  
	    int width = bm.getWidth();  
	    int height = bm.getHeight();  
	    // 计算缩放比例  
	    float scaleWidth = ((float) newWidth) / width;  
	    float scaleHeight = ((float) newHeight) / height;  
	    // 取得想要缩放的matrix参数  
	    Matrix matrix = new Matrix();  
	    matrix.postScale(scaleWidth, scaleHeight);  
	    // 得到新的图片  
	    Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);  
	    return newbm;  
	} 


抱歉!评论已关闭.