534
技術社區[雲棲]
android 一些數據轉換方法
android 一些數據轉換方法
package com.ai9475.util;
import android.content.Context;
import android.content.res.Resources;
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;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
* 數據格式轉換工具類
*
* Created by ZHOUZ on 14-2-15.
*/
public class ZFormat
{
private ZFormat() { }
/**
* 將byte[]轉換成InputStream
*
* @param b
* @return
*/
public static InputStream byte2InputStream(byte[] b) {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
return bais;
}
/**
* 將InputStream轉換成byte[]
*
* @param is
* @return
*/
public static 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
*
* @param bm
* @return
*/
public static 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
*
* @param bm
* @param quality
* @return
*/
public static 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
*
* @param is
* @return
*/
public static Bitmap inputStream2Bitmap(InputStream is) {
return BitmapFactory.decodeStream(is);
}
// Drawable轉換成InputStream
public static InputStream drawable2InputStream(Drawable d) {
Bitmap bitmap = drawable2Bitmap(d);
return bitmap2InputStream(bitmap);
}
/**
* InputStream轉換成Drawable
*
* @param context
* @param is
* @return
*/
public static Drawable inputStream2Drawable(Context context, InputStream is) {
Bitmap bitmap = inputStream2Bitmap(is);
return bitmap2Drawable(context, bitmap);
}
/**
* Drawable轉換成byte[]
*
* @param d
* @return
*/
public static byte[] drawable2Bytes(Drawable d) {
Bitmap bitmap = drawable2Bitmap(d);
return bitmap2Bytes(bitmap);
}
/**
* byte[]轉換成Drawable
*
* @param context
* @param b
* @return
*/
public static Drawable bytes2Drawable(Context context, byte[] b) {
Bitmap bitmap = bytes2Bitmap(b);
return bitmap2Drawable(context, bitmap);
}
/**
* Bitmap轉換成byte[]
*
* @param bm
* @return
*/
public static byte[] bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* byte[]轉換成Bitmap
*
* @param b
* @return
*/
public static Bitmap bytes2Bitmap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
return null;
}
/**
* Drawable轉換成Bitmap
*
* @param drawable
* @return
*/
public static 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
*
* @param context
* @param bitmap
* @return
*/
public static Drawable bitmap2Drawable(Context context, Bitmap bitmap) {
Resources res = context.getResources();
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
Drawable d = (Drawable) bd;
return d;
}
}
最後更新:2017-04-03 12:55:02
上一篇:
Win8使用感受
下一篇:
數據分析的前途在哪? 職業發展之路?有什麼資格認證證書嗎?
SUSE(Linux操作係統)
Android listView滑動刷新代碼(分頁功能)
MyRocks簡介
高通量衛星牛在哪?飛機高鐵上高速下電影成真
asp.net中ListBox在jQuery中遇到的問題——直接傳值到不了後台
Facebook TSDB論文翻譯
WINDOWS中cmd的切換目錄cd命令失效
2011????????????????????????????????????????????????Java????????????-??????-????????????-?????????
如何判斷撥打的手機號碼及存儲的號碼是否合法?
使用AjaxPro實現ajax效果