一直在使用的一个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. 动态规划之最长公共子序列LCS(Longest Common Subsequence)

    一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...

  2. 行人检測之HOG特征(Histograms of Oriented Gradients)

    之前的文章行人计数.计次提到HOG特征这个概念,这两天看了一下原版的论文,了解了一下HOG特征的原理,并依据自己的理解将这种方法的流程写了下来,假设有不正确的地方欢迎指正. HOG(Histogram ...

  3. Filter简单介绍

    一.简单介绍 Filter也称为过滤器,WEB开发者通过Filter技术.对webserver管理的全部web资源:比如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截.从而实 ...

  4. pthread_t结构的定义

    linux下是这样定义的: 在linux的实现中pthread_t被定义为 "unsigned long int",參考这里 Windows下这样定义: /* * Generic ...

  5. git错误:fatal: Not a git repository (or any of the parent directories): .git

    git错误:fatal: Not a git repository (or any of the parent directories): .git 我用git add file添加文件时出现这样错误 ...

  6. FlexSlider是一个非常出色的jQuery滑动切换插件

    FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...

  7. Intellij Idea安装主题

    IDEA中jar包形式的主题比较常见.(顺便给大家推荐一个主题站:http://www.ideacolorthemes.org/themes/) 从主菜单中依次选择[File]>[Import ...

  8. SQL按汉语拼音首字母排序

    以常用到的省的数据表(province)为例,其中name字段为省的名称,SQL语句如下: ))) as py ,a.name from province a left outer join ( se ...

  9. OutputCache各参数的说明

    OutputCache各参数的说明 Duration 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location Location当被设置为 ...

  10. SQL Server 存储过程之基础知识(转)

    什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...