package rasel.lunar.launcher.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import java.io.ByteArrayOutputStream

object ImageUtils {

    fun bitmapToBase64String(resource : Bitmap) : String? {
        val baos = ByteArrayOutputStream()
        resource.compress(Bitmap.CompressFormat.PNG, 100, baos)
        val iconArray = baos.toByteArray()

        return if (iconArray != null && iconArray.size > 0) {
            Base64.encodeToString(iconArray, Base64.DEFAULT)
        } else {
            null
        }
    }

    fun stringToBitmap(src : String) : Bitmap? {
        val iconArray = Base64.decode(src?.toByteArray(), Base64.DEFAULT)
        return if (iconArray != null && iconArray.size > 0) {
            BitmapFactory.decodeByteArray(iconArray, 0, iconArray.size)
        } else {
            null
        }
    }

}