Android Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系

Android Image涉及相关转换(Bitmap、InputStream、Drawable、byte[]、Base64)。

复制代码
public class Utils {


    //将Bitmap转换成InputStream 压缩率 100表示不压缩、10表示压缩90%
    public InputStream bitmapInputStream(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 inputStreamBitmap(InputStream is) {
        return BitmapFactory.decodeStream(is);

    }

    // Drawable转换成InputStream
    public InputStream drawableInputStream(Drawable d) {
        Bitmap bitmap = drawableBitmap(d);
        return bitmapInputStream(bitmap, 100);
    }

    // InputStream转换成Drawable
    public Drawable inputStreamDrawable(InputStream is) {
        Bitmap bitmap = inputStreamBitmap(is);
        return bitmapDrawable(bitmap);
    }

    // Drawable转换成byte[]
    public byte[] drawableBytes(Drawable d) {
        Bitmap bitmap = drawableBitmap(d);
        return bitmapBytes(bitmap);
    }

    // byte[]转换成Drawable
    public Drawable bytesDrawable(byte[] b) {
        Bitmap bitmap = bytesBitmap(b);
        return bitmapDrawable(bitmap);

    }

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

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

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

    // 将InputStream转换成byte[]
    public byte[] inputStreamBytes(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;
    }

    // Drawable转换成Bitmap
    public Bitmap drawableBitmap(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 bitmapDrawable(Bitmap bitmap) {
        BitmapDrawable bd = new BitmapDrawable(bitmap);
        Drawable d = (Drawable) bd;
        return d;
    }

    //将Bitmap转换成Base64
    public String getImageStr(Bitmap bit) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 100, bos);//参数100表示不压缩104byte[] bytes=bos.toByteArray();
        return Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
    }

    //将Base64转换成bitmap
    public Bitmap getImage(String str) {
        byte[] bytes;
        bytes = Base64.decode(str, 0);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

}
相关推荐
listhi5201 小时前
Vue.js 3的组合式API
android·vue.js·flutter
用户69371750013842 小时前
🚀 Jetpack MVI 实战全解析:一次彻底搞懂 MVI 架构,让状态管理像点奶茶一样丝滑!
android·android jetpack
2501_915918413 小时前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone
峥嵘life3 小时前
Android EDLA 打开5G热点失败分析解决2
android·5g
消失的旧时光-19435 小时前
webkitx(Android WebView 最佳实践库)--> 上
android·webview
安卓兼职framework应用工程师5 小时前
android 15.0 app应用安装黑名单
android·pms·install·rom·安装黑名单
泷羽Sec-静安5 小时前
Less-7 GET-Dump into outfile-String
android·前端·网络·sql·安全·web安全
花花鱼6 小时前
html5与android之间相互调用
android
aqi007 小时前
FFmpeg开发笔记(八十八)基于Compose的国产电视直播开源框架MyTV
android·ffmpeg·音视频·直播·流媒体
●VON8 小时前
双非大学生自学鸿蒙5.0零基础入门到项目实战 -《基础篇》
android·华为·harmonyos·鸿蒙