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 == null || watermark == null) {
			return src;
		}

		int sWid = src.getWidth();
		int sHei = src.getHeight();
		int wWid = watermark.getWidth();
		int wHei = watermark.getHeight();
		if (sWid == 0 || sHei == 0) {
			return null;
		}

		if (sWid < wWid || sHei < wHei) {
			return src;
		}

		Bitmap bitmap = Bitmap.createBitmap(sWid, sHei, Config.ARGB_8888);//Config可修改,改变内存占用
		try {
			Canvas cv = new Canvas(bitmap);
			cv.drawBitmap(src, 0, 0, null);
			cv.drawBitmap(watermark, sWid - wWid - 5, sHei - wHei - 5, null);
			cv.save(Canvas.ALL_SAVE_FLAG);
			cv.restore();
		} catch (Exception e) {
			bitmap = null;
			e.getStackTrace();
		}finally{
			src.recycle();
			src=null;
			watermark.recycle();
			watermark=null;
		}
		return bitmap;
	}
	/**
	 * 放大缩小图片
	 *
	 * @Title: zoomBitmap
	 * @param @param bitmap
	 * @param @param w
	 * @param @param h
	 * @return Bitmap
	 * @throws
	 */
	public  Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidht = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidht, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		bitmap.recycle();
		bitmap=null;
		return newbmp;
	}
	/**
	 * @Title: getLoacalBitmap
	 * @Description: 加载本地图片
	 * @param @param url 本地路径
	 * @param @return
	 * @return Bitmap
	 * @throws
	 */
	public  Bitmap getLoacalBitmap(String url) {
		if (url != null) {
			FileInputStream fis=null;
			try {
				fis = new FileInputStream(url);
				return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return null;
			}finally{
				StreamService.close(fis);
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					fis=null;
				}
			}
		} else {
			return null;
		}
	}

	/**
	 * 通过URL地址获取Bitmap对象
	 *
	 * @Title: getBitMapByUrl
	 * @param @param url
	 * @param @return
	 * @param @throws Exception
	 * @return Bitmap
	 * @throws
	 */
	public  Bitmap getBitMapByUrl(final String url) {
		URL fileUrl = null;
		InputStream is=null;
		Bitmap bitmap = null;
		try {
			fileUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			is=null;
		}
		return bitmap;
	}

}

Bitmap工具类,布布扣,bubuko.com

时间: 2024-10-14 21:09:05

Bitmap工具类的相关文章

Bitmap工具类BitmapHelper

BitmapHelper 提供一些获取本地缩略图,获取网络图片.dp与px的相互转换等方法. import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; impo

Android-BitmapUtil工具类

Bitmap工具类,获取Bitmap对象 public class BitmapUtil { private BitmapUtil(){} /** * 根据资源id获取指定大小的Bitmap对象 * @param context 应用程序上下文 * @param id 资源id * @param height 高度 * @param width 宽度 * @return */ public static Bitmap getBitmapFromResource(Context context,

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.grap

Android对Bitmap处理的工具类

处理的方法: 1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 /** * 图片工具类 * * @author rendongwei * */ public class PhotoUtil { /** * 将图片变为圆角 * * @param bitmap * 原Bitmap图片 * @param pixels * 图片圆角的弧度(单位:像素(p

Android开发常用工具类

来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括  HttpUtils.DownloadManagerPro.Safe.ijiami.ShellUtils.PackageUtils. PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils. ParcelUtils.Rand

摘录android工具类

1 import android.content.Context; 2 import android.content.pm.PackageInfo; 3 import android.content.pm.PackageManager; 4 import android.content.pm.PackageManager.NameNotFoundException; 5 6 //跟App相关的辅助类 7 public class AppUtils 8 { 9 10 private AppUtil

二维码生成工具类java版

注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Shape; import java.awt.geom.RoundRectangle2D; import java.a

自定义圆形工具类

1.自定义圆形工具类 CircleImageViewUtil.java 关键点:可以自持缩放 package com.example.circleimageviewdemo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Col

【安卓笔记】硬盘缓存工具类的编写

DiskLruCache(https://github.com/JakeWharton/DiskLruCache)想必大家都很熟悉.(不熟悉的请看这里),它是jakewharton大神写的一个开源库,提供了硬盘缓存的方案. 但是该库的API比较简单,有时候并不能满足我们使用.比如说如果你想把缓存中的数据以Bitmap的形式返回,API并没有提供这样的方法,我们必须通过DiskLruCache#get方法返回来的Snapshot获得输入流,然后将流转化为Bitmap.另外,构建DiskLruCac