一直在使用的一个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工具类的更多相关文章

  1. Bitmap工具类BitmapHelper

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

  2. Android BitmapUtils工具类

    Bitmap工具类 public final class BitmapUtils { public static final String TAG = "BitmapUtil"; ...

  3. Android-BitmapUtil工具类

    Bitmap工具类,获取Bitmap对象 public class BitmapUtil { private BitmapUtil(){} /** * 根据资源id获取指定大小的Bitmap对象 * ...

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

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  5. Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

  6. java工具类

    1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...

  7. 随笔分类 - Android之工具类

    Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...

  8. Android 系统工具类SystemUtils

    包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...

  9. 转:工具类之SpannableStringUtils(相信你会爱上它)

    这个工具类真是构思了良久才设计出来,采用了建造者模式,然后你们就可以用链式调用了,talk is cheap, let me show the demo. demo code 有没有心动一下哈,下面就 ...

随机推荐

  1. 通过winForm控制webForm的上传控件file的值

    文件上传是日常开发中经常遇到的,文件上传用的最多的当然是上传控件file了,一个form表单,其中有一点就是form表单的enctype属性设置为multipart/form-data,呵呵,这个在所 ...

  2. [Hapi.js] Replying to Requests

    hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...

  3. 4. 绘制光谱曲线QGraphicsView类

    一.前言 Qt的QGraphicsView类具有强大的视图功能,与其一起使用的还有QGraphicsScene类和QGraphicsItem类.大体思路就是通过构建场景类,然后向场景对象中增加各种图元 ...

  4. python- 迭代器与生成器

    1.迭代器: 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么, 因为人们很少在迭代途中往后退.另外,迭代器的一 ...

  5. LESS使用方法简介(装逼神器)

    LESS 做为 CSS 的一种形式的扩展,它并没有阉割 CSS 的功能,而是在现有的 CSS 语法上,添加了很多额外的功能,所以学习 LESS 是一件轻而易举的事情,果断学习之! 变量 很容易理解: ...

  6. 【转载】ADO.NET与ORM的比较(4):EntityFramework实现CRUD

    [转载]ADO.NET与ORM的比较(4):EntityFramework实现CRUD 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hiber ...

  7. c++设计模式之状态模式

    一. 状态模式 定义:允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类. 实现方式1: workstate 状态抽象类 workstate1 状态实现类1 workstate2 ...

  8. oracle 联表更新

    依 a 表 cate_pub_id  为依据 更新 v 表的 cate_pub_id update td_tobrel_cate_pub_attrval v set v.CATE_PUB_ID=(se ...

  9. Unity3d Web Player 的server端联网配置

    新游戏出了第一个能跑完流程的版本,不得不佩服Unity3D强大的功力,PC.MAC OS.Linux.IOS.Android.web player,前天刚发布的unity3d 4.2版本还支持WIND ...

  10. scheme代码高亮

    (defun iedit-symbol-in-defun () "Enter `iedit-mode' to rename the symbol in current function, o ...