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工具类的更多相关文章
- Bitmap工具类BitmapHelper
BitmapHelper 提供一些获取本地缩略图,获取网络图片.dp与px的相互转换等方法. import java.io.ByteArrayInputStream; import java.io.B ...
- Android BitmapUtils工具类
Bitmap工具类 public final class BitmapUtils { public static final String TAG = "BitmapUtil"; ...
- Android-BitmapUtil工具类
Bitmap工具类,获取Bitmap对象 public class BitmapUtil { private BitmapUtil(){} /** * 根据资源id获取指定大小的Bitmap对象 * ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- java工具类
1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...
- 随笔分类 - Android之工具类
Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...
- Android 系统工具类SystemUtils
包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...
- 转:工具类之SpannableStringUtils(相信你会爱上它)
这个工具类真是构思了良久才设计出来,采用了建造者模式,然后你们就可以用链式调用了,talk is cheap, let me show the demo. demo code 有没有心动一下哈,下面就 ...
随机推荐
- Staple: Complementary Learners For Real-time Tracking Tracking
转载注明出处: http://www.cnblogs.com/sysuzyq/p/6169414.html By 少侠阿朱 讨论班上的PPT 1.同学大家好.今天给大家讲一篇单目标跟踪的论文,方法比较 ...
- 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
- Android 环境配置:git开启多颜色模式
git config --global color.status autogit config --global color.diff autogit config --global color.br ...
- 把一个select查询结果插入到一个表(可选指定字段和值实例)
把一个select查询结果插入到一个表(可选指定字段和值实例) insert into bak (cc,yf) select cc,9 from ket insert into bak (cc,yf ...
- 前端编辑神器Brackets
介绍 Brackets 是Adobe发布的一款免费.开源且跨平台的 HTML/CSS/JavaScript 前端 WEB 集成开发环境.使用Node.js构建!官网:http://brackets.i ...
- java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...
- [j2ee][IDEA properties中文乱码解决]
http://my.oschina.net/pengzai/blog/133908 在project settings - File Encoding,在标红的选项上打上勾,确定即可
- Makefile 工程管理
Makefile 工程管理 Makefile 规则 --变量 在Makefile中,用户除了可以自己定义变量外,还可以使用存在系统已经定义好的默认变量 $^:代表所有的依赖文件 $@:代表目标 $&l ...
- HTML基础知识笔记(一)
HTML定义 HTML指的是超文本标记语言 HTML不是编程语言,而是标记语言 标记语言是一套标记标签 HTML是用标记标签来描述网页 HTML标签1 <html></html& ...
- No1_4.数组的基本操作_Java学习笔记
import java.util.Arrays; public class HelloArrayOp { public static void main(String[] args) { // TOD ...