-
-
Notifications
You must be signed in to change notification settings - Fork 965
/
ResolutionConstraint.kt
37 lines (32 loc) · 1.14 KB
/
ResolutionConstraint.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package id.zelory.compressor.constraint
import android.graphics.BitmapFactory
import id.zelory.compressor.calculateInSampleSize
import id.zelory.compressor.decodeSampledBitmapFromFile
import id.zelory.compressor.determineImageRotation
import id.zelory.compressor.overWrite
import java.io.File
/**
* Created on : January 24, 2020
* Author : zetbaitsu
* Name : Zetra
* GitHub : https://github.com/zetbaitsu
*/
class ResolutionConstraint(private val width: Int, private val height: Int) : Constraint {
override fun isSatisfied(imageFile: File): Boolean {
return BitmapFactory.Options().run {
inJustDecodeBounds = true
BitmapFactory.decodeFile(imageFile.absolutePath, this)
calculateInSampleSize(this, width, height) <= 1
}
}
override fun satisfy(imageFile: File): File {
return decodeSampledBitmapFromFile(imageFile, width, height).run {
determineImageRotation(imageFile, this).run {
overWrite(imageFile, this)
}
}
}
}
fun Compression.resolution(width: Int, height: Int) {
constraint(ResolutionConstraint(width, height))
}