標籤:android bitmap canvas string
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[]轉換成InputStreampublic 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轉換成InputStreampublic InputStream Bitmap2InputStream(Bitmap bm) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);InputStream is = new ByteArrayInputStream(baos.toByteArray());return is;}// 將Bitmap轉換成InputStreampublic 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轉換成Bitmappublic Bitmap InputStream2Bitmap(InputStream is) {return BitmapFactory.decodeStream(is);}// Drawable轉換成InputStreampublic InputStream Drawable2InputStream(Drawable d) {Bitmap bitmap = this.drawable2Bitmap(d);return this.Bitmap2InputStream(bitmap);}// InputStream轉換成Drawablepublic 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[]轉換成Drawablepublic 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[]轉換成Bitmappublic Bitmap Bytes2Bitmap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);}return null;}// Drawable轉換成Bitmappublic 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轉換成Drawablepublic Drawable bitmap2Drawable(Bitmap bitmap) {BitmapDrawable bd = new BitmapDrawable(bitmap);Drawable d = (Drawable) bd;return d;}}
構建自己的架構,從點滴做起
Android Bitmap、BitmapDrawable、Stream轉換整合