android学习笔记 图片处理工具类

/**
 * 图像处理工具类
 * 
 * @author ly
 *
 */
public class ImageHelper {

	/*
	 * 设置图像矩阵
	 */
	public static Bitmap handleImageEffect(Bitmap bm, float hue,
			float saturation, float lum) {
		Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bmp);
		Paint paint = new Paint();

		ColorMatrix hueMatrix = new ColorMatrix();
		hueMatrix.setRotate(0, hue);
		hueMatrix.setRotate(1, hue);
		hueMatrix.setRotate(2, hue);

		ColorMatrix saturationMatrix = new ColorMatrix();
		saturationMatrix.setSaturation(saturation);

		ColorMatrix lumMatrix = new ColorMatrix();
		lumMatrix.setScale(lum, lum, lum, 1);

		ColorMatrix imageMatrix = new ColorMatrix();
		imageMatrix.postConcat(hueMatrix);
		imageMatrix.postConcat(saturationMatrix);
		imageMatrix.postConcat(lumMatrix);

		paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
		canvas.drawBitmap(bm, 0, 0, paint);
		return bmp;
	}

	/*
	 * 底片效果
	 */
	public static Bitmap handleImageNegative(Bitmap bm) {

		int width = bm.getWidth();
		int height = bm.getHeight();
		int color;
		int r, g, b, a;

		Bitmap bmp = Bitmap
				.createBitmap(width, height, Bitmap.Config.ARGB_8888);

		int[] oldPx = new int[width * height];
		int[] newPx = new int[width * height];

		/*
		 * 获取Bitmap中的像素点,保存到数组中 pixels:接收位图颜色值的数组 offset:写入到pixels[]中的第一个像素索引值
		 * stride:pixels[]中的行间距 x:从位图中读取的第一个像素的x坐标值 y:从位图中读取的第一个像素的y坐标值
		 * width:从每一行中读取的像素宽度 height:读取行数
		 */
		bm.getPixels(oldPx, 0, width, 0, 0, width, height);
		for (int i = 0; i < width * height; i++) {
			// 获取每个像素具体的ARGB值
			color = oldPx[i];
			r = Color.red(color);
			g = Color.green(color);
			b = Color.blue(color);
			a = Color.alpha(color);

			r = 255 - r;
			g = 255 - g;
			b = 255 - b;

			if (r > 255) {
				r = 255;
			} else if (r < 0) {
				r = 0;
			}
			if (g > 255) {
				g = 255;
			} else if (g < 0) {
				g = 0;
			}
			if (b > 255) {
				b = 255;
			} else if (b < 0) {
				b = 0;
			}
			// 将新的RGBAC合成像素点
			newPx[i] = Color.argb(a, r, g, b);
		}
		bmp.setPixels(newPx, 0, width, 0, 0, width, height);
		return bmp;
	}

	/*
	 * 老照片效果
	 */
	public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) {

		int width = bm.getWidth();
		int height = bm.getHeight();
		int color;
		int r, g, b, a, r1, g1, b1;

		Bitmap bmp = Bitmap
				.createBitmap(width, height, Bitmap.Config.ARGB_8888);

		int[] oldPx = new int[width * height];
		int[] newPx = new int[width * height];

		/*
		 * 获取Bitmap中的像素点,保存到数组中 pixels:接收位图颜色值的数组 offset:写入到pixels[]中的第一个像素索引值
		 * stride:pixels[]中的行间距 x:从位图中读取的第一个像素的x坐标值 y:从位图中读取的第一个像素的y坐标值
		 * width:从每一行中读取的像素宽度 height:读取行数
		 */
		bm.getPixels(oldPx, 0, width, 0, 0, width, height);
		for (int i = 0; i < width * height; i++) {
			// 获取每个像素具体的ARGB值
			color = oldPx[i];
			r = Color.red(color);
			g = Color.green(color);
			b = Color.blue(color);
			a = Color.alpha(color);

			r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
			g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
			b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);

			if (r1 > 255) {
				r1 = 255;
			} else if (r < 0) {
				r1 = 0;
			}
			if (g1 > 255) {
				g1 = 255;
			} else if (g1 < 0) {
				g1 = 0;
			}
			if (b1 > 255) {
				b1 = 255;
			} else if (b < 0) {
				b1 = 0;
			}
			// 将新的RGBAC合成像素点
			newPx[i] = Color.argb(a, r1, g1, b1);
		}
		bmp.setPixels(newPx, 0, width, 0, 0, width, height);
		return bmp;
	}

	/*
	 * 浮雕效果
	 */
	public static Bitmap handleImagePixelsRelief(Bitmap bm) {
		
		Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
				Bitmap.Config.ARGB_8888);
		int width = bm.getWidth();
		int height = bm.getHeight();
		int color = 0, colorBefore = 0;
		int a, r, g, b;
		int r1, g1, b1;

		int[] oldPx = new int[width * height];
		int[] newPx = new int[width * height];

		bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
		for (int i = 1; i < width * height; i++) {
			colorBefore = oldPx[i - 1];
			a = Color.alpha(colorBefore);
			r = Color.red(colorBefore);
			g = Color.green(colorBefore);
			b = Color.blue(colorBefore);

			color = oldPx[i];
			r1 = Color.red(color);
			g1 = Color.green(color);
			b1 = Color.blue(color);

			r = (r - r1 + 127);
			g = (g - g1 + 127);
			b = (b - b1 + 127);
			if (r > 255) {
				r = 255;
			}
			if (g > 255) {
				g = 255;
			}
			if (b > 255) {
				b = 255;
			}
			newPx[i] = Color.argb(a, r, g, b);
		}
		bmp.setPixels(newPx, 0, width, 0, 0, width, height);
		return bmp;
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值