Android中Bitmap对象和字节流之间的相互转换(转)
android 将图片内容解析成字节数组;将字节数组转换为ImageView可调用的Bitmap对象;图片缩放;把字节数组保存为一个文件;把Bitmap转Byte
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix; public class ImageDispose { /**
* @param 将图片内容解析成字节数组
* @param inStream
* @return byte[]
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
} /**
* @param 将字节数组转换为ImageView可调用的Bitmap对象
* @param bytes
* @param opts
* @return Bitmap
*/
public static Bitmap getPicFromBytes(byte[] bytes,
BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
} /**
* @param 图片缩放
* @param bitmap 对象
* @param w 要缩放的宽度
* @param h 要缩放的高度
* @return newBmp 新 Bitmap对象
*/
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
return newBmp;
} /**
* 把Bitmap转Byte
*/
public static byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} /**
* 把字节数组保存为一个文件
*/
public static File getFileFromBytes(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return file;
}
}
代码很实用所以转载过来,又因为转载的也是别人转载的文章,所以出处不知道,请见谅!
Android中Bitmap对象和字节流之间的相互转换(转)的更多相关文章
- Android中Bitmap对象和字节流之间的相互转换
android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte import java.io.B ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- JS 中 JSON 对象与字符串之间的相互转换
在开发的过程中,如果对于少量参数的前后台传递,可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,这样后台 接受的时候Request多个很麻烦 ...
- Json数组操作小记 及 JSON对象和字符串之间的相互转换
[{"productid":"1","sortindex":"2"},{"productid":&q ...
- Android中传递对象的三种方法
Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者I ...
- 【转】Android中dip(dp)与px之间单位转换
Android中dip(dp)与px之间单位转换 dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因 ...
- 在android中实现webview与javascript之间的交互(转)
参见“在android中实现webview与javascript之间的交互”
- android中Bitmap的放大和缩小的方法
android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34 CSDN博客原文 http://blog.csdn.net/ada168855/article/det ...
- JSON对象与字符串之间的相互转换
<html> <head> <meta name="viewport" content="width=device-width" ...
随机推荐
- Python之常用模块之小结
复习os模块常用的一些操作 import os # 1.切换路径============= d = os.getcwd() #获取当前的工作路径 os.chdir('D:\\')#目录的切换 prin ...
- html头部和底部固定时,中间的内容随屏幕分别率铺满页面
html页面头部和底部有东西时,怎么让内容填充到中间的页面,且去适应不同的电脑分辨率,看代码 <!DOCTYPE html> <html> <head> <m ...
- python出现SyntaxError: Non-ASCII character '\xe6' in file \的错误
出现这个问题的主要原因是因为python2的编码是ASCII码,文件中有中文的话就得使用utf8编码,只需要在文件的头部加上以下其中一种标注: 一.在文件头部添加如下注释码: # coding=< ...
- 类型xxx 无法反序列化。缺乏对应的数据成员。
WebApi——json返回多了 k_BackingField 产生原因: model类添加了 [System.Serializable] 返回json的时候会出现.
- json.load(f)方法使用*.json备忘
在python中使用JSON,导入出现错误了,类型错误等. #!/usr/bin/python import json f = open('data.json', encoding='utf-8') ...
- 17. ClustrixDB 日志管理
ClustrixDB记录关于重要和有问题的查询的详细信息.这些日志有助于确定以下事项: 慢速查询 资源争用 SQL错误 读取意外数量行的查询 模式变化 全局变量的修改 集群的改变 默认情况下,查询日志 ...
- 牛客网 TaoTao要吃鸡 ( 0/1背包变形 )
题意 : 题目链接 分析 : 如果没有 BUG (即 h == 0 的时候)就是一个普通的 0 / 1 背包 需要讨论一下 h != 0 的情况 此时有就相当于有物品是有特权的 而且背包装有特权的物 ...
- EF另一个 SqlParameterCollection 中已包含 SqlParameter。
代码: SqlParameter[] commandParameters = new SqlParameter[]{ new SqlParameter("@CultID",filt ...
- vue中的methods,conputed,watcher
todo 1,computed(计算属性) 属性的结果会被缓存, 除非依赖的响应式属性变化才会重新计算, 主要 当做属性来使用;(虽然是个方法,但是当做属性使用)(场景:引用一些经过操作的数据) co ...
- spring session 加载的时候一些配置问题
启动springboot时候的错误信息: An attempt was made to call the method org.springframework.boot.autoconfigure.s ...