一,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相关工具类的更多相关文章

  1. Bitmap工具类

    一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...

  2. Java 文件名操作的相关工具类

    项目中的操作文件名的工具类: import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; ...

  3. redis集成相关工具类

    package cn.yiyuan.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; i ...

  4. java List的相关工具类

    1. <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</ar ...

  5. Bitmap工具类BitmapHelper

    BitmapHelper 提供一些获取本地缩略图,获取网络图片.dp与px的相互转换等方法. import java.io.ByteArrayInputStream; import java.io.B ...

  6. 淘淘相关工具类【json,httpClient,id,FTP,exception,cookie(包括共享cookie的设置等)】

    json package com.taotao.common.utils; import java.util.List; import com.fasterxml.jackson.core.JsonP ...

  7. Apache Commons 相关工具类使用

    Apache Commons Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 ...

  8. 【Springboot】FastJson与Jackson全局序列化方式的配置和相关工具类

    springboot 版本: <parent> <groupId>org.springframework.boot</groupId> <artifactId ...

  9. Java IP白名单相关工具类

    关于设置IP白名单相关的一些方法,整理,记录了一下. package com.tools.iptool; import java.util.ArrayList; import java.util.Ha ...

随机推荐

  1. python3.6 使用newspaper库的Article包来快速抓取网页的文章或者新闻等正文

    我主要是用了两个方法来抽去正文内容,第一个方法,诸如xpath,css,正则表达式,beautifulsoup来解析新闻页面的时候,总是会遇到这样那样各种奇奇怪怪的问题,让人很头疼.第二个方法是后面标 ...

  2. 使用python的kazoo模块连接zookeeper实现最基本的增删改查

    kazoo的官方文档地址:https://kazoo.readthedocs.io/en/latest/index.html #!/usr/bin/env python # -*- coding: u ...

  3. 一个常用的通过curl发送HTTP请求的函数

    function: function curl_get($url, $params) { return curl_http($url, $params, 'GET'); } function curl ...

  4. shell脚本之删除内容相同的重复文件

    #!/bin/bash #!当前文件夹下,删除内容相同的重复文件,只保留重复文件中的一个. ls -lS --time-style=long-iso | awk 'BEGIN{ getline;get ...

  5. ifconfig - 配置网络接口

    总览 ifconfig [接口] ifconfig 接口 [aftype] options | address ... 描述 ifconfig 用于配置常驻内核的网络接口.它用于在引导成功时设定网络接 ...

  6. Vue分割音乐歌词数据函数

    parseLyric(lyric) {       var lines = lyric.split(/\n/); //使用/n换行,将数据切成一个数组       var getLtricTime = ...

  7. 【JAVA】Maven profiles控制多环境数据源日志打包(转载)

    https://blog.csdn.net/qq_17213067/article/details/81661295

  8. WPF界面控件Telerik UI for WPF发布R2 2019 SP1|实现新的属性

    Telerik UI for WPF拥有超过100个控件来创建美观.高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序.UI for WPF支持MVVM.触摸等,创建的应用程序可靠且结构良 ...

  9. java课堂动手测试

    课堂实验3 一个Java类文件中真的只能有一个公有类吗? 经过测试,当含有两个public  类时会报错,不能执行,假如删除第二个public则可以正常生成,说明一个java文件只能有一个公有类. 课 ...

  10. Java中InputStream和String之间的转换方法

    1.InputStream转化为String1.1 JDK原生提供方法一:byte[] bytes = new byte[0];bytes = new byte[inputStream.availab ...