图片处理常用方法总结

1. 高斯模式处理

查考笔记"高斯模式"部分,有专门总结

2.裁剪图片(按屏幕比例)

下面这个方法是将传入的bitmap(当初写这个方法的时候是取得系统壁纸)根据屏幕尺寸来裁剪
public Bitmap setAndcropWallpaper(Context context, Bitmap wallpaper) {
	Bitmap targetBitmap = null;
	if (wallpaper != null) {
		try {
			int wallpaperWidth = wallpaper.getWidth();
			int wallpaperHeight = wallpaper.getHeight();
			DisplayMetrics dm = new DisplayMetrics();
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			wm.getDefaultDisplay().getMetrics(dm);
			int screenWidth = dm.widthPixels;// 1080
			int screenHeight = dm.heightPixels;// 1920
			float defaultScreen = (float) screenHeight / (float) screenWidth;
			Log.d(TAG, "defaultScreen=" + defaultScreen);
			if (defaultScreen < 1) {
				defaultScreen = (float) screenWidth / (float) screenHeight;
			}
			float bitmapSize = (float) wallpaperHeight / (float) wallpaperWidth;
			int disWidth = 0;
			int disHeight = 0;
			int captureBitmapWidth = wallpaperWidth;
			int captureBitmapHeight = wallpaperHeight;
			Log.d(TAG, "defaultScreen=" + defaultScreen + " bitmapSize=" + bitmapSize);
			if (defaultScreen > bitmapSize) { // bitmapHeight is min , capture bitmapWidth
				captureBitmapWidth = (int) ((screenWidth * wallpaperHeight) / screenHeight);
				disWidth = wallpaperWidth - captureBitmapWidth;
			} else if (defaultScreen < bitmapSize) {
				captureBitmapHeight = (int) ((wallpaperWidth * screenHeight) / screenWidth);
				disHeight = wallpaperHeight - captureBitmapHeight;
			}
			if (DEBUG) {
				Log.d(TAG, "setAndcropWallpaper ----- disWidth=" + disWidth + ",disHeight=" + disHeight
						+ ",captureBitmapWidth=" + captureBitmapWidth + ",captureBitmapHeight="
						+ captureBitmapHeight + " screenWidth=" + screenWidth + " screenHeight=" + screenHeight);
			}
			if (disWidth < 0 || disHeight < 0) {
				return null;
			}
			targetBitmap = Bitmap.createBitmap(wallpaper, disWidth / 2, disHeight / 2, captureBitmapWidth,
					captureBitmapHeight);
			float scaleWidth = (float) ((float) captureBitmapWidth / screenWidth);
			float scaleHeight = (float) ((float) captureBitmapHeight / screenHeight);
			Log.d(TAG, "scaleWidth=" + scaleWidth + " scaleHeight=" + scaleHeight);
			if ((targetBitmap != null) && (scaleWidth != 1 || scaleHeight != 1)) {
				Matrix matrix = new Matrix();
				/**
				 * 处理之后的图片小于屏幕尺寸 处理原始的图片比例和屏幕比例是一致,但尺寸比屏幕大
				 */
				if (scaleWidth != 0.0f && scaleHeight != 0.0f && (scaleWidth != 1.0f || scaleHeight != 1.0f)) {//
					scaleWidth = 1.0f / scaleWidth;
					scaleHeight = 1.0f / scaleHeight;
					Log.d(TAG, "---scaleWidth=" + scaleWidth + " scaleHeight=" + scaleHeight);

				}
				matrix.postScale(scaleWidth, scaleHeight);
				int width = targetBitmap.getWidth();
				int height = targetBitmap.getHeight();
				Log.d(TAG, "---width=" + width + " height=" + height);
				targetBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, screenWidth > width ? width : screenWidth,
						screenHeight > height ? height : screenHeight, matrix, true);
			}
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
	}
	return targetBitmap;
}

3.把图片裁剪成圆形

public static Bitmap cropBitmap(Bitmap bitmap) {//从中间截取一个正方形
	int w = bitmap.getWidth(); // 得到图片的宽,高
	int h = bitmap.getHeight();
	int cropWidth = w >= h ? h : w;// 裁切后所取的正方形区域边长

	return Bitmap.createBitmap(bitmap, (bitmap.getWidth() - cropWidth) / 2,
			(bitmap.getHeight() - cropWidth) / 2, cropWidth, cropWidth);
}

public static Bitmap getCircleBitmap(Bitmap bitmap) {//把图片裁剪成圆形
	if (bitmap == null) {
		return null;
	}
	bitmap = cropBitmap(bitmap);//裁剪成正方形
	try {
		Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(circleBitmap);
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight());
		final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight()));
		float roundPx = 0.0f;
		roundPx = bitmap.getWidth();
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(Color.WHITE);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		final Rect src = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight());
		canvas.drawBitmap(bitmap, src, rect, paint);
		return circleBitmap;
	} catch (Exception e) {
		return bitmap;
	}
}

 

 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值