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 ...
随机推荐
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- HDU-5155 Harry And Magic Box
题目描述 在\(n*m\)的矩阵内每一行每一列都有钻石,问钻石分布的种类? 答案有可能很大,所以输出答案对\(1000000007\)取模. Input 对于每个测试用例,有两个整数\(n\)和\(m ...
- 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)
题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...
- mysql数据库之存储过程
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该 ...
- H5头部meta标签的作用
<!DOCTYPE html> H5标准声明,使用 HTML5 doctype,不区分大小写 <head lang=”en”> 标准的 lang 属性写法 <meta ...
- 使用CXF开发WebService程序的总结(五):基于Map数据类型处理的的客户端和服务端代码的编写
1. 首先我们按照List或数组等处理方式来处理Map,看看效果 1.1 在服务端的接口中添加以下方法 /** * 查询所有班级信息加上对应的学生列表 * * @return */ public Ma ...
- mysql5.7.26做主从复制配置
一.首先两台服务器安装好mysql数据库环境 参照linux rpm方式安装mysql5.1 https://www.cnblogs.com/sky-cheng/p/10564604.html 二.主 ...
- java面试07——设计模式
1.什么是设计模式 设计模式就是经过前人无数次的实践总结出的,设计过程可以反复使用的,可以解决特定问题的设计方法. 2.常用的设计模式有哪些 2.1单例模式(饱汉模式.饿汉模式.双重锁模式) http ...
- tensorflow中张量_常量_变量_占位符
1.tensor 在tensorflow中,数据是被封装在tensor对象中的.tensor是张量的意思,即包含从0到任意维度的张量.常数是0维度的张量,向量是1维度的张量,矩阵是二维度的张量,以及还 ...
- multi gpu inference with tfserving or keras
在tfserving上,目测只能在每个gpu上起docker https://github.com/tensorflow/serving/issues/311#issuecomment-4801 ...