Skip to content

Commit

Permalink
#17 in progress - refactor in PhotoHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Jun 9, 2024
1 parent 55227fd commit 57b22eb
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 64 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)

TODO:
- remove dead code
- prepare custom version artifact for the play shop
- prepare custom version artifact for the play shop
- add doDommemorativePhoto to searching screen
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.test.platform.app.InstrumentationRegistry
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress

//TODO t: try to change to https://robolectric.org/androidx_test/
abstract class ReportAbstractTest {
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
val model: FacebookViewModel = FacebookViewModel(SavedStateHandle(mapOf()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.shared.PhotoHelper
import pl.marianjureczko.poszukiwacz.shared.PhotoScalingHelper

class ElementHolder(
private val activity: FragmentActivity,
Expand Down Expand Up @@ -68,7 +69,7 @@ class ElementHolder(

private fun renderPhoto(image: ImageView, photoFile: String) {
val photo = BitmapFactory.decodeFile(photoFile)
val resizedPhoto = PhotoHelper.scalePhotoKeepRatio(photo, 250f, 300f)
val resizedPhoto = PhotoScalingHelper.scalePhotoKeepRatio(photo, 250f, 300f)
image.setImageBitmap(resizedPhoto)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Typeface
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.shared.PhotoHelper
import pl.marianjureczko.poszukiwacz.shared.PhotoScalingHelper
import java.io.File
import java.io.FileInputStream
import java.lang.System.currentTimeMillis
Expand Down Expand Up @@ -70,7 +70,7 @@ class ReportCommemorativePhotos(
.chunked(3)
.forEach { row ->
val images = row.map { photoElement ->
PhotoHelper.scalePhotoKeepRatio(BitmapFactory.decodeStream(FileInputStream(File(photoElement.photo!!))), IMAGE_PLACEHOLDER_HEIGHT, IMAGE_PLACEHOLDER_WIDTH)
PhotoScalingHelper.scalePhotoKeepRatio(BitmapFactory.decodeStream(FileInputStream(File(photoElement.photo!!))), IMAGE_PLACEHOLDER_HEIGHT, IMAGE_PLACEHOLDER_WIDTH)
}
when (row.size) {
3 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import pl.marianjureczko.poszukiwacz.activity.facebook.ElementDescription
import pl.marianjureczko.poszukiwacz.activity.facebook.FacebookReportModel
import pl.marianjureczko.poszukiwacz.activity.facebook.ReportGenerator
import pl.marianjureczko.poszukiwacz.shared.GoToGuide
import pl.marianjureczko.poszukiwacz.shared.PhotoHelper
import pl.marianjureczko.poszukiwacz.shared.PhotoScalingHelper
import pl.marianjureczko.poszukiwacz.shared.RotatePhoto
import pl.marianjureczko.poszukiwacz.ui.components.AdvertBanner
import pl.marianjureczko.poszukiwacz.ui.components.LargeButton
Expand Down Expand Up @@ -198,7 +198,7 @@ fun FacebookElement(it: ElementDescription, viewModel: FacebookViewModel, onRota

private fun renderPhoto(photoFile: String): Bitmap {
val photo = BitmapFactory.decodeFile(photoFile)
return PhotoHelper.scalePhotoKeepRatio(photo, 250f, 300f)
return PhotoScalingHelper.scalePhotoKeepRatio(photo, 250f, 300f)
}

object FacebookShareHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import kotlinx.coroutines.withContext
import pl.marianjureczko.poszukiwacz.activity.treasureseditor.TreasuresEditorActivity
import java.io.File
import java.io.FileOutputStream
import kotlin.math.max
import kotlin.math.min

class PhotoHelper(
val context: Context,
Expand All @@ -25,39 +23,6 @@ class PhotoHelper(
private val TAG = javaClass.simpleName
const val TMP_PICTURE_FILE = "/tmp_commemorative_photo.jpg"

//TODO t: merge with other functions
/**
* Scales the photo, but keeps the aspect ratio
* @param maxHeight - the maximum height after scaling
* @param maxWidth - the maximum width after scaling
*/
fun scalePhotoKeepRatio(photo: Bitmap, maxHeight: Float, maxWidth: Float): Bitmap {
val widthFactor = maxWidth / photo.width
val heightFactor = maxHeight / photo.height
val factor = min(widthFactor, heightFactor)
val matrix = Matrix()
matrix.postScale(factor, factor)
return Bitmap.createBitmap(photo, 0, 0, photo.width, photo.height, matrix, false)
}

fun calculateScalingFactor(width: Int, height: Int): Float {
val greater = max(width, height).toFloat()
val wanted = 800F
return if (greater < 800) {
1F;
} else {
wanted / greater;
}
}

//TODO t: private?
fun createScalingMatrix(width: Int, height: Int): Matrix {
val scalingFactor = calculateScalingFactor(width, height)
val matrix = Matrix()
matrix.postScale(scalingFactor, scalingFactor)
return matrix
}

/**
* @param photoFile absolute path to graphic file
*/
Expand Down Expand Up @@ -107,7 +72,7 @@ class PhotoHelper(
val bm: Bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
val width: Int = bm.width
val height: Int = bm.height
val newSizeMatrix = createScalingMatrix(width, height)
val newSizeMatrix = PhotoScalingHelper.createScalingMatrix(width, height)
val resized = Bitmap.createBitmap(bm, 0, 0, width, height, newSizeMatrix, false)

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pl.marianjureczko.poszukiwacz.shared

import android.graphics.Bitmap
import android.graphics.Matrix
import kotlin.math.max
import kotlin.math.min

object PhotoScalingHelper {

//TODO t: add suspend
//TODO t: try to test with https://robolectric.org/androidx_test/
/**
* Scales the photo, but keeps the aspect ratio
* @param maxHeight - the maximum height after scaling
* @param maxWidth - the maximum width after scaling
*/
fun scalePhotoKeepRatio(photo: Bitmap, maxHeight: Float, maxWidth: Float): Bitmap {
val widthFactor = maxWidth / photo.width
val heightFactor = maxHeight / photo.height
val factor = min(widthFactor, heightFactor)
val matrix = Matrix()
matrix.postScale(factor, factor)
return Bitmap.createBitmap(photo, 0, 0, photo.width, photo.height, matrix, false)
}

/**
* Keeps aspect ratio, but scales down so that the greater dimension goes down to 800 px
* @param matrix for unit tests
*/
fun createScalingMatrix(imageWidth: Int, imageHeight: Int, matrix: Matrix = Matrix()): Matrix {
val scalingFactor = scalingFactorToGoDownTo800px(imageWidth, imageHeight)
matrix.postScale(scalingFactor, scalingFactor)
return matrix
}

private fun scalingFactorToGoDownTo800px(width: Int, height: Int): Float {
val greater = max(width, height).toFloat()
val wanted = 800F
return if (greater < 800) {
1F;
} else {
wanted / greater;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ import java.io.File

class PhotoHelperTest {

@Test
fun `SHOULD scale height to ~ 800px WHEN height is greater than weight`() {
//given

//when
val actual = PhotoHelper.calculateScalingFactor(900, 1000)

//then
assertThat(actual).isEqualTo(0.8f)
}

@Test
fun `SHOULD scale weight to ~ 800px WHEN weight is greater than height`() {
//given

//when
val actual = PhotoHelper.calculateScalingFactor(950, 1000)

//then
assertThat(actual).isEqualTo(0.8f)
}

@Test
fun `SHOULD move commemorative file`() {
//given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package pl.marianjureczko.poszukiwacz.shared

import android.graphics.Matrix
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.then
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.junit.jupiter.MockitoExtension

@ExtendWith(MockitoExtension::class)
class PhotoScalingHelperTest {

@Mock
private lateinit var matrix: Matrix

@Test
fun `SHOULD scale width to ~ 800px WHEN width is greater than height`() {
//when
val actual = PhotoScalingHelper.createScalingMatrix(950, 1000, matrix)

//then
assertThat(actual).isSameAs(matrix)
val expected = 0.8f
then(matrix).should(times(1)).postScale(expected, expected)
}

@Test
fun `SHOULD scale height to ~ 800px WHEN height is greater than width`() {
//when
val actual = PhotoScalingHelper.createScalingMatrix(1000, 900, matrix)

//then
assertThat(actual).isSameAs(matrix)
val expected = 0.8f
then(matrix).should(times(1)).postScale(expected, expected)
}

@Test
fun `SHOULD scale to ~ 800px WHEN image is large`() {
//when
val actual = PhotoScalingHelper.createScalingMatrix(4000, 4000, matrix)

//then
assertThat(actual).isSameAs(matrix)
val expected = 0.2f
then(matrix).should(times(1)).postScale(expected, expected)
}
}

0 comments on commit 57b22eb

Please sign in to comment.