一直在使用的一个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. qt Graphics View Framework(非重点)

    Graphics View 提供了一种接口,用于管理大量自定义的 2D 图形元素,并与之进行交互:还提供了用于将这些元素进行可视化显示的观察组件,并支持缩放和旋转. 说明;Graphics View ...

  2. [Immutable,js] Iterating Over an Immutable.js Map()

    Immutable.js provides several methods to iterate over an Immutable.Map(). These also apply to the ot ...

  3. UIView属性clipsTobounds的应用

    view添加view,并剪边(UIView属性clipsTobounds的应用) 如题,有两个view: view1,view2 view1添加view2到其中,如果view2大于view1,或者vi ...

  4. Linux 挂载命令 --mount

    1.挂载光盘命令  mount :  mount [-t vfstype] [-o options] device dir mount [-t 文件系统] [-o 特殊选项] 设备文件名 挂载点 -t ...

  5. NET基础课-- 类型基础(NET之美)

    1.类型:值类型  引用类型. 分类依据:类型在内存的分配方式.值类型在堆栈,引用类型在托管堆. 名词:栈--所有变量都会被分配在栈上,只不过值类型直接含有数据,引用类型含有一个指向托管堆对象的地址. ...

  6. fullcalendar .net版本

    实现了基本的增删改和拖拽,先记与此,抽时间继续优化和完善. 参考链接:http://www.helloweba.com/tag-fullcalendar.html 参考demo:   http://f ...

  7. java进程

    package com.process;   public class ProcessTest { public static void main(String[] args) { new Proce ...

  8. jBPM4.4与SSH2整合

    整合jBPM的目的就是能够通过注入的方式得到ProcessEngine实例,因为ProcessEngine是jbpm 的核心. 整合步骤: 1.新建web程,搭建好SSH2环境 2.导入jbpm相关的 ...

  9. HttpClient中异步方法的同步调用

    在System.Net.Http中,提供了使用Http与远程服务器通讯的httpClient,但是里面都是异步方法,有时候我们并不需要使用异步操作.这个时候可以使用如下的方式来进行同步调用. clas ...

  10. MySql移植到嵌入式Linux平台

    最近在做考勤机系统,硬件采用的cortex-A8,哈哈,其实是有点浪费的,2410就可以的.所以就要考虑到考勤数据的存储问题,本来是打算用sqlite数据库存储的,可是后来发现,这个数据库只是一个本地 ...