一直在使用的一个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. 使用dojo的tree

    dojo的Tree非常是灵活,可是官方站点上的样例却非常少,并且也比較分散,兴许将持续完好本样例. 总的来说,要使用tree,要接触到三个类:"dojo/store/JsonRest&quo ...

  2. Android系统默认Home应用程序(Launcher)的启动过程源代码分析

    在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还需要有一个 Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home ...

  3. Flexbox属性可视化指南

    Flexbox 布局(国内很多人称为弹性布局)正式的全称为 CSS Flexible Box布局模块,它是CSS3新增的一种布局模式.它可以很方便地用来改善动态或未知大小的元素的对齐,方向和顺序等等. ...

  4. 仿桌面通知pnotify插件

    在做网站的时候,alert弹出框是非常常见的情形.但是,有些情况下,弹框对用户来说是并不友好的.调研了几个其他的提示插件了,发现pnotify比较好用,可配置性也高. 使用示例: <!DOCTY ...

  5. Android学习笔记--远程服务的使用

    1.AIDL和Binder Android系统四大组件Activity, Content Provider, Broadcast和Service均可以进行跨进程数据传输. Activity可以隐式调用 ...

  6. css-网页整体css布局

    <!DOCTYPE html><!--有限宽度带居中布局:<style>*{margin:0;padding:0;list-style:none;} .zong{back ...

  7. Javascript的性能瓶颈

    Javascript是单线程的,它的性能瓶颈在于频繁的DOM操作, 因为每次操作都会使浏览器重新绘制一次. 其实纯JS的执行的速度是很快的,可以把元素都攒到一块,一次性放到页面中. 或者,定义一个延时 ...

  8. Java线程状态:BLOCKED与WAITING的区别

    Doc说明: /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked ...

  9. CISC + RISC = Y86

    最近在读深入理解计算机系统,打算把读时的心得放上来 Y86有着CISC和RISC的属性Y86可以看成是CISC(IA32),但用RISC的原理简化了 CISC和RISC的竞争引发了许多争论CISC和R ...

  10. OpenCV——写手势识别碰到的各种错误

    最近写opencv的手势识别时,真的碰到了好多好多程序运行时的错误. 比如: 位置冲突: findcontours函数的使用错误: 各种符号的加载错误: 这种符号加载的错误,一般用网上各种方法就可以解 ...