@@ -7,6 +7,7 @@ import android.graphics.Canvas
77import android.graphics.Rect
88import android.graphics.drawable.BitmapDrawable
99import android.graphics.drawable.Drawable
10+ import android.util.Base64
1011import android.view.View
1112import android.widget.ImageView
1213import com.omega_r.libs.omegatypes.Image.Companion.applyBackground
@@ -57,6 +58,23 @@ open class Image : Serializable {
5758 @JvmStatic
5859 fun from (bitmap : Bitmap ): Image = BitmapImage (bitmap)
5960
61+ @JvmStatic
62+ fun from (imageBytes : ByteArray ): Image {
63+ return from(BitmapFactory .decodeByteArray(imageBytes, 0 , imageBytes.size) ? : return empty())
64+ }
65+
66+ @JvmStatic
67+ fun from (base64String : String , flags : Int ): Image {
68+ val position = base64String.indexOf(" ," )
69+ val data = if (position != - 1 ) {
70+ base64String.substring(position + 1 )
71+ } else {
72+ base64String
73+ }
74+ return from(Base64 .decode(data, flags))
75+ }
76+
77+
6078 internal fun applyBackground (view : View , background : Drawable ? ) {
6179 if (android.os.Build .VERSION .SDK_INT < android.os.Build .VERSION_CODES .JELLY_BEAN ) {
6280 @Suppress(" DEPRECATION" )
@@ -66,55 +84,54 @@ open class Image : Serializable {
6684 }
6785 }
6886 }
87+ }
6988
70- class ResourceImage (private val resId : Int ) : Image() {
89+ class ResourceImage (private val resId : Int ) : Image() {
7190
72- override fun applyImage (imageView : ImageView , placeholderResId : Int ) {
73- imageView.setImageResource(resId)
74- }
75-
76- override fun applyBackground (view : View , placeholderResId : Int ) {
77- view.setBackgroundResource(resId)
78- }
91+ override fun applyImage (imageView : ImageView , placeholderResId : Int ) {
92+ imageView.setImageResource(resId)
93+ }
7994
80- override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
81- return BitmapFactory .decodeResource(context.resources, resId)
82- .toInputStream(compressFormat, quality)
83- }
95+ override fun applyBackground (view : View , placeholderResId : Int ) {
96+ view.setBackgroundResource(resId)
97+ }
8498
99+ override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
100+ return BitmapFactory .decodeResource(context.resources, resId)
101+ .toInputStream(compressFormat, quality)
85102 }
86103
87- class DrawableImage ( private val drawable : Drawable ) : Image() {
104+ }
88105
89- override fun applyImage (imageView : ImageView , placeholderResId : Int ) {
90- imageView.setImageDrawable(drawable)
91- }
106+ class DrawableImage (private val drawable : Drawable ) : Image() {
92107
93- override fun applyBackground ( view : View , placeholderResId : Int ) {
94- applyBackground(view, drawable)
95- }
108+ override fun applyImage ( imageView : ImageView , placeholderResId : Int ) {
109+ imageView.setImageDrawable( drawable)
110+ }
96111
97- override fun getStream ( context : Context , compressFormat : Bitmap . CompressFormat , quality : Int ): InputStream {
98- return drawable.toBitmap().toInputStream(compressFormat, quality )
99- }
112+ override fun applyBackground ( view : View , placeholderResId : Int ) {
113+ applyBackground(view, drawable )
114+ }
100115
116+ override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
117+ return drawable.toBitmap().toInputStream(compressFormat, quality)
101118 }
102119
103- class BitmapImage ( private val bitmap : Bitmap ) : Image() {
120+ }
104121
105- override fun applyImage (imageView : ImageView , placeholderResId : Int ) {
106- imageView.setImageBitmap(bitmap)
107- }
122+ class BitmapImage (private val bitmap : Bitmap ) : Image() {
108123
109- override fun applyBackground ( view : View , placeholderResId : Int ) {
110- applyBackground(view, BitmapDrawable (view.resources, bitmap) )
111- }
124+ override fun applyImage ( imageView : ImageView , placeholderResId : Int ) {
125+ imageView.setImageBitmap( bitmap)
126+ }
112127
113- override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
114- return bitmap.toInputStream(compressFormat, quality)
115- }
128+ override fun applyBackground (view : View , placeholderResId : Int ) {
129+ applyBackground(view, BitmapDrawable (view.resources, bitmap))
116130 }
117131
132+ override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
133+ return bitmap.toInputStream(compressFormat, quality)
134+ }
118135}
119136
120137fun Bitmap.toInputStream (compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
0 commit comments