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.Buf ...
- 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" ...
随机推荐
- Java简单介绍及Java生态
核心思想:面向对象编程,继承,高兼容(代码移植性强),开源,避免重复造轮子(使用mybatis,spring,redis等技术只需要将jar包依赖添加到项目中即可,jar包内就是技术核心代码,而这些框 ...
- Java五种基本的Annotation,提高程序的可读性
从JDK5开始,Java增加了对元数据的支持,也就是Annotation(即注解也被翻译为注释). 这里的Annotation和普通的注释有一定的区别,它是代码中的特殊标记,这些标记可以在编译.类加载 ...
- Java岗 面试考点精讲(基础篇02期)
1. 两个对象的hashCode相同,则equals也一定为true,对吗? 不对,答案见下面的代码: @Override public int hashCode() { return 1; } 两个 ...
- Unity3D手机斗地主游戏开发实战(02)_叫地主功能实现
大体思路 前面我们实现了点击开始游戏按钮,系统依次给玩家发牌的逻辑和动画,并展示当前的手牌.这期我们继续实现接下来的功能--叫地主. 1.首先这两天,学习了DOTween,这是一个强大的Unity动画 ...
- 教你分分钟搞定Docker私有仓库Registry
一.什么是Docker私有仓库Registry 官方的Docker hub是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去.但是,有时候我们的服务器无法 ...
- 【Spring源码解读】bean标签中的属性(一)你可能还不够了解的 scope 属性
scope 属性说明 在spring中,在xml中定义bean时,scope属性是用来声明bean的作用域的.对于这个属性,你也许已经很熟悉了,singleton和prototype信手捏来,甚至还能 ...
- CSS回顾(常见问题解决)
一.margin的塌陷解决: BFC (block format context)块级格式化上下文格式 display:inline-block float:left / right overflow ...
- Spark操作parquet文件
package code.parquet import java.net.URI import org.apache.hadoop.conf.Configuration import org.apac ...
- redis sentinel集群的搭建
背景说明: 这里采用1主2从的redis集群,3个sentinel搭建高可用redis集群. 一,关于搭建redis-sentinel高可用之前,我们必须要了解redis主从搭建redis-senti ...
- [20190214]11g Query Result Cache RC Latches补充.txt
[20190214]11g Query Result Cache RC Latches补充.txt --//上午测试链接:http://blog.itpub.net/267265/viewspace- ...