bitmap相关工具类
一,bitmap工具
封装了以下方法:
1,获取activity屏幕截图,保存为图片文件
2,从文件中获取截图,返回bitmap对象
package com.ctbri.weather.utils; import java.io.File;
import java.io.FileOutputStream; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;
import android.os.Environment;
import android.view.View; public class BitmapUtil {
/**
* 从文件中加载图片的缩略图
*
* @param path
* 文件路径
* @return
*/
public static Bitmap loadThumbnail(String path) { BitmapFactory.Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
int windowwidth = ;
int windowheigth = ; int scalex = width / windowwidth;
int scaley = height / windowheigth; if (scalex > scaley && scaley > ) { // 姘村钩鏂瑰悜鐨勭缉鏀炬瘮渚嬫瘮杈冨ぇ
opts.inSampleSize = scalex;
}
if (scaley > scalex && scalex > ) { // 绔栫洿鏂瑰悜鐨勭缉鏀炬瘮渚嬫瘮杈冨ぇ
opts.inSampleSize = scaley;
}
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, opts); return bitmap; }
/**
* 获取acitivy屏幕截图工具
* @param activity需要截图的ctivity
* @param v 该acitivy上的一个view控件
* @return 返回截图的存储路径
*/
public static String getscreen(Activity activity,View v) {
String fname = "/mnt/sdcard/"; File file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"eweather");
if (!file.exists()) {
file.mkdirs();
}
fname = new File(file.getPath() + File.separator
+ "shareimg" + ".jpg").getAbsolutePath();
View view = v.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache(); if (bitmap != null) {
try {
FileOutputStream out = new FileOutputStream(fname);
//鑾峰彇鐘舵�鏍忛珮搴�
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
//鑾峰彇灞忓箷闀垮拰楂�
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
//鍘绘帀鏍囬鏍�
bitmap = Bitmap.createBitmap(bitmap, , statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache(); bitmap.compress(Bitmap.CompressFormat.PNG, , out);
bitmap.recycle();
bitmap=null; } catch (Exception e) {
e.printStackTrace();
} } else {
System.out.println("bitmapis NULL!");
}
return fname; } }
二、实现bitmap,drawable,byte【】之间的转换
package com.ctbri.weather.utils; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable; public class ByteUtil { public static byte[] read(String path) {
ByteArrayOutputStream outputStream = null;
try {
File imageFile = new File(path);
FileInputStream inputStream = new FileInputStream(imageFile);
outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outputStream.toByteArray();
} public static byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} public synchronized static byte[] drawableToByte(Drawable drawable) { if (drawable != null) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.JPEG ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
bitmap.recycle();
bitmap = null;
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
int size = bitmap.getWidth() * bitmap.getHeight() * 4;
ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
byte[] imagedata = baos.toByteArray();
return imagedata;
}
return null;
}
}
关于这一点,自己的另外一篇博客有更详细的讨论,包括view,bitmap,drawable,byte[]之间的相互转换
http://www.cnblogs.com/bobodeboke/archive/2013/05/09/3069415.html
bitmap相关工具类的更多相关文章
- Bitmap工具类
一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...
- Java 文件名操作的相关工具类
项目中的操作文件名的工具类: import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; ...
- redis集成相关工具类
package cn.yiyuan.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; i ...
- java List的相关工具类
1. <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</ar ...
- Bitmap工具类BitmapHelper
BitmapHelper 提供一些获取本地缩略图,获取网络图片.dp与px的相互转换等方法. import java.io.ByteArrayInputStream; import java.io.B ...
- 淘淘相关工具类【json,httpClient,id,FTP,exception,cookie(包括共享cookie的设置等)】
json package com.taotao.common.utils; import java.util.List; import com.fasterxml.jackson.core.JsonP ...
- Apache Commons 相关工具类使用
Apache Commons Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 ...
- 【Springboot】FastJson与Jackson全局序列化方式的配置和相关工具类
springboot 版本: <parent> <groupId>org.springframework.boot</groupId> <artifactId ...
- Java IP白名单相关工具类
关于设置IP白名单相关的一些方法,整理,记录了一下. package com.tools.iptool; import java.util.ArrayList; import java.util.Ha ...
随机推荐
- python 链接mysql 连接池
# python 链接mysqlimport mysql.connector.poolingconfig = { "host":"localhost", &qu ...
- GitHub编辑README.md
一.标题 等级表示法(六级): #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 一级标题/大标题(文本下面加上等于号): 大标题 === 二级标题 ...
- 3424:Candies(差分约束,Dijkstra)(配对堆优化
题面链接 题解 令x-y<=z表示x最大比y大z. 若b-a<=k1, c-b<=k2, c-a<=k3,那么c-a最大为多少呢?显然应该等于min(k1+k2, k3).可以 ...
- vim学习(二)之模式
vim模式 基本上 vi/vim 共分为三种模式,分别是命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode). 命令模式: 用户刚刚启 ...
- 实现 RSA 算法之 C 语言实现(第二章)(老物)
第二章 如何实现应用RSA算法 趁着白天在自家店里的闲暇时间来写写第二章了,假设记住了第一章的各种定理之后,我们又该如何实现RSA密码的加密解密呢?也懒得废话了,直接进入正题吧. 先回顾几个知识点: ...
- linux的安全--Selinux,tcp_wrappers,iptables使用
一.linux安全 安全主要是端口与服务的对应配置 1.1 linux安全主要通过下面三个进行加固 Selinux----主要是对内核的访问权限加以控制 tcp_wrappers---一定程度上限制某 ...
- SpringBoot01——Framework Introduced and Helloworld
1.介绍 SpringBoot主要解决的是在微服务的架构下简化配置(有快速配置).前后端分离.快速开发 优点: l 提供了快速启动入门 l 开箱即用.提供默认配置 l 内嵌容器化web项目 l 没有冗 ...
- VS2013 删除"附加依赖项"中“继承的值”
经过好几次尝试,都无法在VS2013中直接删除“继承的值”,于是另辟蹊径,找到了一种解决方法. 相对而言,在 VS2010 中干这件事会容易一点,或者说,成功率更高一点,于是,我的思路就是再装一个 V ...
- git每次更新都需要输入账号密码,如何解决?
//如果https请求pull每次都需要输入账号密码,则 git config --global credential.helper store 那么,在下一次更新,需要输入账号密码,然后git就 ...
- 下载bat脚本
@rem 注释:从ftp服务器每小时下载北向性能文件的脚本 @rem 用vb脚本取昨天 for /f %%a in ('cscript //nologo yester.vbs') do set yes ...