Skip to content

Commit 73a1c14

Browse files
committed
[BitmapUtils@cropAndResize()] Fix: Case result size not equal to targetSize
1 parent 511f0fe commit 73a1c14

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

photomanipulator/src/androidTest/java/com/guhungry/photomanipulator/BitmapUtilsAndroidTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ internal class BitmapUtilsAndroidTest {
7676
assertThat(background!!.getPixel(75 + 96, 145 + 70), equalTo(overlay!!.getPixel(96, 70)))
7777
}
7878

79+
/**
80+
* Fix Issue For
81+
* https://github.com/react-native-community/react-native-image-editor/issues/27
82+
*/
83+
@Test
84+
fun cropAndResize_when_bug_scaledown_should_have_correct_size() {
85+
FileUtils.openBitmapInputStream(TestHelper.context(), TestHelper.drawableUri(R.drawable.issue27)).use {
86+
output = BitmapUtils.cropAndResize(it, CGRect(0, 0, 2160, 3840), CGSize(16, 16), BitmapFactory.Options())
87+
88+
assertThat(output!!.width, equalTo(16))
89+
assertThat(output!!.height, equalTo(16))
90+
}
91+
}
92+
7993
@Test
8094
fun readImageDimensions_should_return_correct_dimension() {
8195
assertReadImageDimensions(R.drawable.background, 800, 530)
Loading

photomanipulator/src/main/java/com/guhungry/photomanipulator/BitmapUtils.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.guhungry.photomanipulator.factory.AndroidFactory
55
import com.guhungry.photomanipulator.factory.AndroidConcreteFactory
66
import java.io.IOException
77
import java.io.InputStream
8-
import kotlin.math.floor
98

109
object BitmapUtils {
1110
@JvmStatic
@@ -75,7 +74,7 @@ object BitmapUtils {
7574
// This uses scaling mode COVER
7675
// Where would the crop rect end up within the scaled bitmap?
7776
val crop = findCropPosition(cropSize, targetSize, outOptions.inSampleSize)
78-
val scaleMatrix = findCropScale(cropSize, targetSize, outOptions.inSampleSize)
77+
val scaleMatrix = findCropScale(crop, targetSize)
7978

8079
return Bitmap.createBitmap(bitmap, crop.origin.x, crop.origin.y, crop.size.width, crop.size.height, scaleMatrix, true)
8180
}
@@ -105,18 +104,17 @@ object BitmapUtils {
105104

106105
return CGRect(applyScale(newX, sampleSize), applyScale(newY, sampleSize), applyScale(newWidth, sampleSize), applyScale(newHeight, sampleSize))
107106
}
108-
private fun applyScale(value: Float, sampleSize: Int) = floor(value / sampleSize).toInt()
107+
private fun applyScale(value: Float, sampleSize: Int) = kotlin.math.floor(value / sampleSize).toInt()
109108

110-
private fun findCropScale(rect: CGRect, targetSize: CGSize, sampleSize: Int): Matrix {
109+
private fun findCropScale(rect: CGRect, targetSize: CGSize): Matrix {
111110
val cropRectRatio = rect.size.ratio()
112111
val targetRatio = targetSize.ratio()
113112

114-
val scale = if (cropRectRatio > targetRatio) { // e.g. source is landscape, target is portrait
113+
val cropScale = if (cropRectRatio > targetRatio) { // e.g. source is landscape, target is portrait
115114
targetSize.height / rect.size.height.toFloat()
116115
} else { // e.g. source is landscape, target is portrait
117116
targetSize.width / rect.size.width.toFloat()
118117
}
119-
val cropScale = scale * sampleSize
120118
return Matrix().apply { setScale(cropScale, cropScale) }
121119
}
122120

0 commit comments

Comments
 (0)