首页 > 代码库 > Bitmap工具类

Bitmap工具类

一直在使用的一个Bitmap工具类

处理Bitmap和ImageView对象,实现了以下功能:

1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存.
2.getViewBitmap: 从view得到bitmap对象
3.addWatermark: Bitmap加水印
4.zoomBitmap: 放大缩小图片
5.getLoacalBitmap: 传入路径,从持久存储(SD卡或手机内存)得到Bitmap对象
6.getBitMapByUrl: 通过URL地址获取Bitmap对象

7.toGrayscale: 对Bitmap进行滤镜特效处理.实现了图片变黑白,变亮,变暗,高对比,低对比,高饱和,低饱和

下载地址:http://download.csdn.net/download/landehuxi/6661713

源码如下:

public class ImgService {
	//亮
	public  final float[] LIGHT_ARR = new float[] { 
			1, 0, 0, 0, 100, 
			0, 1, 0, 0, 100, 
			0, 0, 1, 0, 100,
			0, 0, 0, 1, 0 };
	//暗
	public  final float[] DARK_ARR = new float[] { 
			0.2f, 0, 0, 0, 50.8f, 
			0, 0.2f, 0, 0, 50.8f, 
			0, 0,0.2f, 0, 50.8f, 
			0, 0, 0, 1f, 0 };
	//高对比
	public  final float[] GDB_ARR = new float[] { 
				5, 0, 0, 0, -250, 
				0, 5, 0, 0, -250, 
				0, 0, 5, 0, -250,
				0, 0, 0, 1, 0 };
	//高对比
	public  final float[] DDB_ARR = new float[] { 
				0.2f, 0, 0, 0, 50, 
				0, 0.2f, 0, 0, 50, 
				0, 0, 0.2f, 0, 50,
				0, 0, 0, 1, 0 };
	//高饱和
	public  final float[] GBH_ARR = new float[] { 
				3f, -1.8f, -0.25f, 0, 50, 
				-0.9f, 2.1f, -0.25f, 0, 50, 
				-0.9f, -1.8f, 3.8f, 0, 50,
				0, 0, 0, 1, 0 };
	//低饱和
	public final float[] DBH_ARR = new float[] { 
			0.3f, 0.6f, 0.08f, 0, 0, 
			0.3f, 0.6f, 0.08f, 0, 0, 
			0.3f, 0.6f, 0.08f, 0, 0,
			0, 0, 0, 1, 0 };
	//COPY
	public final float[] COPY_ARR = new float[] { 
				0, 0, 0, 0, 0, 
				0, 0, 0, 0, 0, 
				0, 0, 0, 0, 0,
				0, 0, 0, 0, 0 };
	/**
	 * 为图片加滤镜特效.array参数为ImgService定义的几个滤镜矩阵.如ImgService.LIGHT_ARR
	 * @param bmpOriginal
	 * @param array
	 * @return
	 */
	public Bitmap toGrayscale(Bitmap bmpOriginal, float[] array) {
		int width, height;
		height = bmpOriginal.getHeight();
		width = bmpOriginal.getWidth();

		Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
				Bitmap.Config.RGB_565);
		Canvas c = new Canvas(bmpGrayscale);
		Paint paint = new Paint();
		ColorMatrix colorMatrix = new ColorMatrix();
		colorMatrix.set(array);
		paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
		c.drawBitmap(bmpOriginal, 0, 0, paint);
		bmpOriginal.recycle();
		bmpOriginal=null;
		return bmpGrayscale;
	}

	public boolean saveBitmap(Bitmap bitmap, String fileName, String path) {
		File file = new File(path);
		FileOutputStream fos=null;
		if (!file.exists()) {
			file.mkdir();
		}
		File imageFile = new File(file, fileName);
		try {
			imageFile.createNewFile();
			fos = new FileOutputStream(imageFile);
			bitmap.compress(CompressFormat.JPEG, 50, fos);
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				fos=null;
			}
		}
		return true;
	}

	// 从view得到bitmap
	public Bitmap getViewBitmap(View view) {
		Bitmap bitmap = null;
		try {
			int width = view.getWidth();
			int height = view.getHeight();
			if (width != 0 && height != 0) {
				bitmap = Bitmap.createBitmap(width, height,
						Bitmap.Config.ARGB_8888);
				Canvas canvas = new Canvas(bitmap);
				view.draw(canvas);
			}
		} catch (Exception e) {
			bitmap = null;
			Debug.out(e);
		}
		return bitmap;
	}

	// Bitmap加水印
	public Bitmap addWatermark(Bitmap src, Bitmap watermark) {
		if (src =http://www.mamicode.com/= null || watermark == null) {>