package com.hh.arome.utlis;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

public final class BitmapUtils {


    public static String bitmapToString(Bitmap bitmap) {
        //将Bitmap转换成字符串
        try {
            String string = null;
            ByteArrayOutputStream bStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
            byte[] bytes = bStream.toByteArray();
            string = Base64.encodeToString(bytes, Base64.DEFAULT);
            return string;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Bitmap stringToBitmap(String string) {
        // 将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    /**
     * Base64转Bitmap
     *
     * @return Bitmap 图片
     */
    public static Bitmap base642Bitmap(String base64String) {
        if (null == base64String) throw new NullPointerException();
        byte[] decode = Base64.decode(base64String.split(",")[1], Base64.DEFAULT);
        Bitmap mBitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        return mBitmap;
    }


}
