Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

本文介绍了一个用于Android平台的图片格式转换工具类,包括Bitmap、Drawable、byte[]和InputStream之间的相互转换方法。

java] view plain copy print ?
  1. package com.soai.imdemo;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.InputStream;  
  6.   
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Canvas;  
  10. import android.graphics.PixelFormat;  
  11. import android.graphics.drawable.BitmapDrawable;  
  12. import android.graphics.drawable.Drawable;  
  13.   
  14. /** 
  15.  * Bitmap与DrawAble与byte[]与InputStream之间的转换工具类 
  16.  * @author Administrator 
  17.  * 
  18.  */  
  19. public class FormatTools {  
  20.     private static FormatTools tools = new FormatTools();  
  21.   
  22.     public static FormatTools getInstance() {  
  23.         if (tools == null) {  
  24.             tools = new FormatTools();  
  25.             return tools;  
  26.         }  
  27.         return tools;  
  28.     }  
  29.   
  30.     // 将byte[]转换成InputStream  
  31.     public InputStream Byte2InputStream(byte[] b) {  
  32.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  33.         return bais;  
  34.     }  
  35.   
  36.     // 将InputStream转换成byte[]  
  37.     public byte[] InputStream2Bytes(InputStream is) {  
  38.         String str = "";  
  39.         byte[] readByte = new byte[1024];  
  40.         int readCount = -1;  
  41.         try {  
  42.             while ((readCount = is.read(readByte, 01024)) != -1) {  
  43.                 str += new String(readByte).trim();  
  44.             }  
  45.             return str.getBytes();  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.         return null;  
  50.     }  
  51.   
  52.     // 将Bitmap转换成InputStream  
  53.     public InputStream Bitmap2InputStream(Bitmap bm) {  
  54.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  55.         bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  56.         InputStream is = new ByteArrayInputStream(baos.toByteArray());  
  57.         return is;  
  58.     }  
  59.   
  60.     // 将Bitmap转换成InputStream  
  61.     public InputStream Bitmap2InputStream(Bitmap bm, int quality) {  
  62.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  63.         bm.compress(Bitmap.CompressFormat.PNG, quality, baos);  
  64.         InputStream is = new ByteArrayInputStream(baos.toByteArray());  
  65.         return is;  
  66.     }  
  67.   
  68.     // 将InputStream转换成Bitmap  
  69.     public Bitmap InputStream2Bitmap(InputStream is) {  
  70.         return BitmapFactory.decodeStream(is);  
  71.     }  
  72.   
  73.     // Drawable转换成InputStream  
  74.     public InputStream Drawable2InputStream(Drawable d) {  
  75.         Bitmap bitmap = this.drawable2Bitmap(d);  
  76.         return this.Bitmap2InputStream(bitmap);  
  77.     }  
  78.   
  79.     // InputStream转换成Drawable  
  80.     public Drawable InputStream2Drawable(InputStream is) {  
  81.         Bitmap bitmap = this.InputStream2Bitmap(is);  
  82.         return this.bitmap2Drawable(bitmap);  
  83.     }  
  84.   
  85.     // Drawable转换成byte[]  
  86.     public byte[] Drawable2Bytes(Drawable d) {  
  87.         Bitmap bitmap = this.drawable2Bitmap(d);  
  88.         return this.Bitmap2Bytes(bitmap);  
  89.     }  
  90.   
  91.     // byte[]转换成Drawable  
  92.     public Drawable Bytes2Drawable(byte[] b) {  
  93.         Bitmap bitmap = this.Bytes2Bitmap(b);  
  94.         return this.bitmap2Drawable(bitmap);  
  95.     }  
  96.   
  97.     // Bitmap转换成byte[]  
  98.     public byte[] Bitmap2Bytes(Bitmap bm) {  
  99.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  100.         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  101.         return baos.toByteArray();  
  102.     }  
  103.   
  104.     // byte[]转换成Bitmap  
  105.     public Bitmap Bytes2Bitmap(byte[] b) {  
  106.         if (b.length != 0) {  
  107.             return BitmapFactory.decodeByteArray(b, 0, b.length);  
  108.         }  
  109.         return null;  
  110.     }  
  111.   
  112.     // Drawable转换成Bitmap  
  113.     public Bitmap drawable2Bitmap(Drawable drawable) {  
  114.         Bitmap bitmap = Bitmap  
  115.                 .createBitmap(  
  116.                         drawable.getIntrinsicWidth(),  
  117.                         drawable.getIntrinsicHeight(),  
  118.                         drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  119.                                 : Bitmap.Config.RGB_565);  
  120.         Canvas canvas = new Canvas(bitmap);  
  121.         drawable.setBounds(00, drawable.getIntrinsicWidth(),  
  122.                 drawable.getIntrinsicHeight());  
  123.         drawable.draw(canvas);  
  124.         return bitmap;  
  125.     }  
  126.   
  127.     // Bitmap转换成Drawable  
  128.     public Drawable bitmap2Drawable(Bitmap bitmap) {  
  129.         BitmapDrawable bd = new BitmapDrawable(bitmap);  
  130.         Drawable d = (Drawable) bd;  
  131.         return d;  
  132.     }  
  133. }  
package com.soai.imdemo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
 * @author Administrator
 *
 */
public class FormatTools {
	private static FormatTools tools = new FormatTools();

	public static FormatTools getInstance() {
		if (tools == null) {
			tools = new FormatTools();
			return tools;
		}
		return tools;
	}

	// 将byte[]转换成InputStream
	public InputStream Byte2InputStream(byte[] b) {
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		return bais;
	}

	// 将InputStream转换成byte[]
	public byte[] InputStream2Bytes(InputStream is) {
		String str = "";
		byte[] readByte = new byte[1024];
		int readCount = -1;
		try {
			while ((readCount = is.read(readByte, 0, 1024)) != -1) {
				str += new String(readByte).trim();
			}
			return str.getBytes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 将Bitmap转换成InputStream
	public InputStream Bitmap2InputStream(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	// 将Bitmap转换成InputStream
	public InputStream Bitmap2InputStream(Bitmap bm, int quality) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	// 将InputStream转换成Bitmap
	public Bitmap InputStream2Bitmap(InputStream is) {
		return BitmapFactory.decodeStream(is);
	}

	// Drawable转换成InputStream
	public InputStream Drawable2InputStream(Drawable d) {
		Bitmap bitmap = this.drawable2Bitmap(d);
		return this.Bitmap2InputStream(bitmap);
	}

	// InputStream转换成Drawable
	public Drawable InputStream2Drawable(InputStream is) {
		Bitmap bitmap = this.InputStream2Bitmap(is);
		return this.bitmap2Drawable(bitmap);
	}

	// Drawable转换成byte[]
	public byte[] Drawable2Bytes(Drawable d) {
		Bitmap bitmap = this.drawable2Bitmap(d);
		return this.Bitmap2Bytes(bitmap);
	}

	// byte[]转换成Drawable
	public Drawable Bytes2Drawable(byte[] b) {
		Bitmap bitmap = this.Bytes2Bitmap(b);
		return this.bitmap2Drawable(bitmap);
	}

	// Bitmap转换成byte[]
	public byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

	// byte[]转换成Bitmap
	public Bitmap Bytes2Bitmap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		}
		return null;
	}

	// Drawable转换成Bitmap
	public Bitmap drawable2Bitmap(Drawable drawable) {
		Bitmap bitmap = Bitmap
				.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}

	// Bitmap转换成Drawable
	public Drawable bitmap2Drawable(Bitmap bitmap) {
		BitmapDrawable bd = new BitmapDrawable(bitmap);
		Drawable d = (Drawable) bd;
		return d;
	}
}


转载自:http://my.oschina.net/547217475/blog/93485

**项目名称:** 基于Vue.jsSpring Cloud架构的博客系统设计开发——微服务分布式应用实践 **项目概述:** 本项目为计算机科学技术专业本科毕业设计成果,旨在设计并实现一个采用前后端分离架构的现代化博客平台。系统前端基于Vue.js框架构建,提供响应式用户界面;后端采用Spring Cloud微服务架构,通过服务拆分、注册发现、配置中心及网关路由等技术,构建高可用、易扩展的分布式应用体系。项目重点探讨微服务模式下的系统设计、服务治理、数据一致性及部署运维等关键问题,体现了分布式系统在Web应用中的实践价值。 **技术架构:** 1. **前端技术栈:** Vue.js 2.x、Vue Router、Vuex、Element UI、Axios 2. **后端技术栈:** Spring Boot 2.x、Spring Cloud (Eureka/Nacos、Feign/OpenFeign、Ribbon、Hystrix、Zuul/Gateway、Config) 3. **数据存储:** MySQL 8.0(主数据存储)、Redis(缓存会话管理) 4. **服务通信:** RESTful API、消息队列(可选RabbitMQ/Kafka) 5. **部署运维:** Docker容器化、Jenkins持续集成、Nginx负载均衡 **核心功能模块:** - 用户管理:注册登录、权限控制、个人中心 - 文章管理:富文本编辑、分类标签、发布审核、评论互动 - 内容展示:首页推荐、分类检索、全文搜索、热门排行 - 系统管理:后台仪表盘、用户内容监控、日志审计 - 微服务治理:服务健康检测、动态配置更新、熔断降级策略 **设计特点:** 1. **架构解耦:** 前后端完全分离,通过API网关统一接入,支持独立开发部署。 2. **服务拆分:** 按业务域划分为用户服务、文章服务、评论服务、文件服务等独立微服务。 3. **高可用设计:** 采用服务注册发现机制,配合负载均衡熔断器,提升系统容错能力。 4. **可扩展性:** 模块化设计支持横向扩展,配置中心实现运行时动态调整。 **项目成果:** 完成了一个具备完整博客功能、具备微服务典型特征的分布式系统原型,通过容器化部署验证了多服务协同运行的可行性,为云原生应用开发提供了实践参考。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值