Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
revanthkumarJ committed Jan 24, 2025
1 parent bd4359c commit 2d9d3fb
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 22 deletions.
10 changes: 6 additions & 4 deletions core/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
plugins {
alias(libs.plugins.mifos.kmp.library)
// alias(libs.plugins.mifos.android.library.compose)
alias(libs.plugins.jetbrainsCompose)
alias(libs.plugins.compose.compiler)
}
Expand All @@ -30,8 +29,7 @@ kotlin{
}
commonMain.dependencies {
api(projects.core.designsystem)
api(projects.core.model)
api(projects.core.common)
api(libs.kotlinx.datetime)
implementation(libs.jb.composeViewmodel)
implementation(libs.jb.lifecycleViewmodel)
implementation(libs.jb.lifecycleViewmodelSavedState)
Expand All @@ -43,7 +41,11 @@ kotlin{
implementation(libs.jb.composeNavigation)
implementation(libs.filekit.compose)
implementation(libs.filekit.core)
implementation(libs.accompanist.systemuicontroller)
}
desktopMain.dependencies {
implementation(libs.kotlin.stdlib.jdk8)
implementation(libs.imageio.core)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ expect object ImageUtil {
maxWidth: Float = DEFAULT_MAX_WIDTH,
maxHeight: Float = DEFAULT_MAX_HEIGHT,
): ByteArray
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,88 @@
*/
package org.mifos.mobile.core.ui.utils

import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO

actual object ImageUtil {
actual val DEFAULT_MAX_WIDTH: Float
get() = TODO("Not yet implemented")
actual val DEFAULT_MAX_HEIGHT: Float
get() = TODO("Not yet implemented")
actual val DEFAULT_MAX_WIDTH: Float = 816f
actual val DEFAULT_MAX_HEIGHT: Float = 612f

actual fun compressImage(
decodedBytes: ByteArray,
maxWidth: Float,
maxHeight: Float,
): ByteArray {
TODO("Not yet implemented")

val inputStream = ByteArrayInputStream(decodedBytes)
val originalImage: BufferedImage = try {
ImageIO.read(inputStream)
} catch (e: Exception) {
e.printStackTrace()
return ByteArray(0)
}

val (actualWidth, actualHeight) = calculateActualDimensions(
originalImage.width.toFloat(),
originalImage.height.toFloat(),
maxWidth,
maxHeight
)

val scaledImage = createScaledImage(originalImage, actualWidth, actualHeight)

val byteArrayOutputStream = ByteArrayOutputStream()
try {
ImageIO.write(scaledImage, "JPEG", byteArrayOutputStream)
} catch (e: Exception) {
e.printStackTrace()
}

return byteArrayOutputStream.toByteArray()
}

private fun calculateActualDimensions(
actualWidth: Float,
actualHeight: Float,
maxWidth: Float,
maxHeight: Float
): Pair<Int, Int> {
val imgRatio = actualWidth / actualHeight
val maxRatio = maxWidth / maxHeight

var finalWidth = actualWidth
var finalHeight = actualHeight

if (actualHeight > maxHeight || actualWidth > maxWidth) {
when {
imgRatio < maxRatio -> {
finalHeight = maxHeight
finalWidth = maxHeight * imgRatio
}
imgRatio > maxRatio -> {
finalWidth = maxWidth
finalHeight = maxWidth / imgRatio
}
else -> {
finalWidth = maxWidth
finalHeight = maxHeight
}
}
}

return Pair(finalWidth.toInt(), finalHeight.toInt())
}

private fun createScaledImage(image: BufferedImage, targetWidth: Int, targetHeight: Int): BufferedImage {
val scaledImage = BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB)
val g2d: Graphics2D = scaledImage.createGraphics()
g2d.drawImage(image, 0, 0, targetWidth, targetHeight, null)
g2d.dispose()

return scaledImage
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ actual object ImageUtil {
): ByteArray {
TODO("Not yet implemented")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,86 @@
*/
package org.mifos.mobile.core.ui.utils

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.skia.Bitmap
import org.jetbrains.skia.Canvas
import org.jetbrains.skia.Image
import org.jetbrains.skia.Matrix33
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect
import java.io.ByteArrayOutputStream

actual object ImageUtil {
actual val DEFAULT_MAX_WIDTH: Float
get() = TODO("Not yet implemented")
actual val DEFAULT_MAX_HEIGHT: Float
get() = TODO("Not yet implemented")
actual val DEFAULT_MAX_WIDTH: Float = 816f
actual val DEFAULT_MAX_HEIGHT: Float = 612f

actual fun compressImage(
actual suspend fun compressImage(
decodedBytes: ByteArray,
maxWidth: Float,
maxHeight: Float,
): ByteArray {
TODO("Not yet implemented")
): ByteArray = withContext(Dispatchers.Default) {
val bitmap = Bitmap.makeFromImage(Image.makeFromEncoded(decodedBytes))

val (actualWidth, actualHeight) = calculateActualDimensions(bitmap.width, bitmap.height, maxWidth, maxHeight)
val scaledBitmap = createScaledBitmap(bitmap, actualWidth, actualHeight)

val byteArrayOutputStream = ByteArrayOutputStream()
scaledBitmap.encodeToData()?.bytes?.let { byteArrayOutputStream.write(it) }
byteArrayOutputStream.toByteArray()
}

private fun calculateActualDimensions(
width: Int,
height: Int,
maxWidth: Float,
maxHeight: Float,
): Pair<Int, Int> {
val imgRatio = width.toFloat() / height
val maxRatio = maxWidth / maxHeight

return if (height > maxHeight || width > maxWidth) {
when {
imgRatio < maxRatio -> {
val newHeight = maxHeight.toInt()
val newWidth = (maxHeight * imgRatio).toInt()
Pair(newWidth, newHeight)
}

imgRatio > maxRatio -> {
val newWidth = maxWidth.toInt()
val newHeight = (maxWidth / imgRatio).toInt()
Pair(newWidth, newHeight)
}

else -> {
Pair(maxWidth.toInt(), maxHeight.toInt())
}
}
} else {
Pair(width, height)
}
}

private fun createScaledBitmap(
bitmap: Bitmap,
targetWidth: Int,
targetHeight: Int,
): Bitmap {
val scaledBitmap = Bitmap().apply {
allocPixels(targetWidth, targetHeight)
}

val canvas = Canvas(scaledBitmap)
val scaleX = targetWidth.toFloat() / bitmap.width
val scaleY = targetHeight.toFloat() / bitmap.height
val scaleMatrix = Matrix33.makeScale(scaleX, scaleY)

canvas.save()
canvas.concat(scaleMatrix)
canvas.drawBitmapRect(bitmap, Rect.makeWH(bitmap.width.toFloat(), bitmap.height.toFloat()), Paint())
canvas.restore()

return scaledBitmap
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.mifos.mobile.core.ui.utils

// TODO: Not Implemented
actual object ImageUtil {
actual val DEFAULT_MAX_WIDTH: Float
get() = TODO("Not yet implemented")
Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ androidxUiAutomator = "2.3.0"
annotation = "1.9.1"
appcompatVersion = "1.7.0"
cameraCoreVersion = "1.3.4"
imageioCoreVersion = "1.1"
mlkit="17.3.0"
guavaVersion = "33.3.1-android"
cameraxVersion = "1.4.1"
Expand Down Expand Up @@ -124,7 +125,6 @@ uiGraphicsAndroidVersion = "1.7.6"
[libraries]
accompanist-pager = { group = "com.google.accompanist", name = "accompanist-pager", version.ref = "accompanistVersion" }
accompanist-permissions = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanistVersion" }
accompanist-systemuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "accompanistSystemuicontrollerVersion" }
android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" }
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
android-tools-common = { group = "com.android.tools", name = "common", version.ref = "androidTools" }
Expand All @@ -136,6 +136,9 @@ androidx-camera-camera2 = { group = "androidx.camera", name = "camera-camera2",
androidx-camera-core = { group = "androidx.camera", name = "camera-core", version.ref = "cameraxVersion" }
androidx-camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", version.ref = "cameraxVersion" }
androidx-camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "cameraxVersion" }
imageio-core = { module = "javax.imageio:imageio-core", version.ref = "imageioCoreVersion" }
jetbrains-kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8" }
kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8" }
mlkit-barcode-scanning = { module = "com.google.mlkit:barcode-scanning", version.ref = "mlkit" }
guava = { module = "com.google.guava:guava", version.ref = "guavaVersion" }
androidx-compose-animation = { group = "androidx.compose.animation", name = "animation" }
Expand Down Expand Up @@ -202,7 +205,6 @@ hilt-ext-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.r
hilt-ext-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hiltExt" }
jetbrains-kotlin-jdk7 = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk7", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junitVersion" }
kotlinx-datetime-v040 = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinxDatetimeVersion" }
ktlint-gradlePlugin = { group = "org.jlleitschuh.gradle", name = "ktlint-gradle", version.ref = "ktlint" }
libphonenumber-android = { group = "io.michaelrocks", name = "libphonenumber-android", version.ref = "libphonenumberAndroidVersion" }
lint-api = { group = "com.android.tools.lint", name = "lint-api", version.ref = "androidTools" }
Expand Down Expand Up @@ -272,9 +274,7 @@ kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-cor
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" }
kotlinx-serialization-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-core", version.ref = "kotlinxSerializationJson" }
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }

ksp-gradlePlugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }

ktor-client-android = { group = "io.ktor", name = "ktor-client-android", version.ref = "ktorVersion" }
ktor-client-auth = { group = "io.ktor", name = "ktor-client-auth", version.ref = "ktorVersion" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktorVersion" }
Expand Down

0 comments on commit 2d9d3fb

Please sign in to comment.