feat: add Bitmap extension functions for image manipulation
This commit is contained in:
parent
117785c957
commit
99d76703e7
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.syaroful.agrilinkvocpro.plant_disease_detection_feature.core.extention
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import androidx.core.graphics.scale
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
|
||||||
|
fun Bitmap.toByteArray(): ByteArray {
|
||||||
|
val stream = ByteArrayOutputStream()
|
||||||
|
this.compress(Bitmap.CompressFormat.JPEG, 70, stream)
|
||||||
|
return stream.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toBitmap(): Bitmap {
|
||||||
|
return BitmapFactory.decodeByteArray(this, 0, this.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Bitmap.resize(maxSize: Int = 800): Bitmap {
|
||||||
|
val ratio = width.toFloat() / height.toFloat()
|
||||||
|
val width: Int
|
||||||
|
val height: Int
|
||||||
|
if (ratio > 1) {
|
||||||
|
width = maxSize
|
||||||
|
height = (maxSize / ratio).toInt()
|
||||||
|
} else {
|
||||||
|
height = maxSize
|
||||||
|
width = (maxSize * ratio).toInt()
|
||||||
|
}
|
||||||
|
return this.scale(width, height)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user