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 ...
随机推荐
- PHP控制session时效(转)
1.php session 有效期php的session有效期默认是1440秒(24分钟),如果客户端超过24分钟没有刷新,当前session会被回收,失效. 当用户关闭浏览器,会话结束,sessio ...
- 【iOS】正則表達式抓取网页数据制作小词典
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xn4545945/article/details/37684127 应用程序不一定要自己去提供数据. ...
- C++11随机数的正确打开方式
C++11随机数的正确打开方式 在C++11之前,现有的随机数函数都存在一个问题:在利用循环多次获取随机数时,如果程序运行过快或者使用了多线程等方法,srand((unsigned)time(null ...
- 将div的内容生成清晰的PDF、高清PDF
//需要引入html2canvas.js.jquery.js文件 html: <button type="button" class="btn btn-primar ...
- 关于&联系我
本文已迁移至: Github博客:https://coco5666.github.io/blog/about Gitee博客:https://coco56.gitee.io/blog/about 博客 ...
- ADF检验
单位根检验是指检验序列中是否存在单位根,因为存在单位根就是非平稳时间序列了.单位根就是指单位根过程,可以证明,序列中存在单位根过程就不平稳,会使回归分析中存在伪回归. from statsmodels ...
- Jmeter分布式测试dubbo接口1
最近工作中接到一个需求,需要对一个Dubbo接口进行压力测试,测试其性能,之前一直使用jmeter做压力测试,在踏了好多坑之后,决定把这些记录下来,顺便也希望能帮助到大家. 开始测试之前,我们需要先知 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- python snippets
1.Find memory used by an object import sys 2.Combine a list of strings into a single string strings ...
- Codeforces 903 绝对值1e19longdouble算贡献 汉明距离交换两项是否可行
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...