日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Android Bitmap與DrawAble與byte[]與InputStream之間的轉(zhuǎn)換工具類

 dmw_zgl 2015-03-18

    [java]

    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之間的轉(zhuǎn)換工具類

    * @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[]轉(zhuǎn)換成InputStream

    public InputStream Byte2InputStream(byte[] b) {

    ByteArrayInputStream bais = new ByteArrayInputStream(b);

    return bais;

    }

    // 將InputStream轉(zhuǎn)換成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轉(zhuǎn)換成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轉(zhuǎn)換成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轉(zhuǎn)換成Bitmap

    public Bitmap InputStream2Bitmap(InputStream is) {

    return BitmapFactory.decodeStream(is);

    }

    // Drawable轉(zhuǎn)換成InputStream

    public InputStream Drawable2InputStream(Drawable d) {

    Bitmap bitmap = this.drawable2Bitmap(d);

    return this.Bitmap2InputStream(bitmap);

    }

    // InputStream轉(zhuǎn)換成Drawable

    public Drawable InputStream2Drawable(InputStream is) {

    Bitmap bitmap = this.InputStream2Bitmap(is);

    return this.bitmap2Drawable(bitmap);

    }

    // Drawable轉(zhuǎn)換成byte[]

    public byte[] Drawable2Bytes(Drawable d) {

    Bitmap bitmap = this.drawable2Bitmap(d);

    return this.Bitmap2Bytes(bitmap);

    }

    // byte[]轉(zhuǎn)換成Drawable

    public Drawable Bytes2Drawable(byte[] b) {

    Bitmap bitmap = this.Bytes2Bitmap(b);

    return this.bitmap2Drawable(bitmap);

    }

    // Bitmap轉(zhuǎn)換成byte[]

    public byte[] Bitmap2Bytes(Bitmap bm) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

    return baos.toByteArray();

    }

    // byte[]轉(zhuǎn)換成Bitmap

    public Bitmap Bytes2Bitmap(byte[] b) {

    if (b.length != 0) {

    return BitmapFactory.decodeByteArray(b, 0, b.length);

    }

    return null;

    }

    // Drawable轉(zhuǎn)換成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轉(zhuǎn)換成Drawable

    public Drawable bitmap2Drawable(Bitmap bitmap) {

    BitmapDrawable bd = new BitmapDrawable(bitmap);

    Drawable d = (Drawable) bd;

    return d;

    }

    }

聲明:該文章系網(wǎng)友上傳分享,此內(nèi)容僅代表網(wǎng)友個(gè)人經(jīng)驗(yàn)或觀點(diǎn),不代表本網(wǎng)站立場和觀點(diǎn);若未進(jìn)行原創(chuàng)聲明,則表明該文章系轉(zhuǎn)載自互聯(lián)網(wǎng);若該文章內(nèi)容涉嫌侵權(quán),請(qǐng)及時(shí)向上學(xué)吧網(wǎng)站投訴>>

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多