图片处理常用方法总结

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;
	}
}

 

 
 
 
 

你可能感兴趣的:(Android)