, .
, , , , .
. JPG
Bitmap.createScaledBitmap(input, newWidthPx, newHeightPx, true)
, , .
, MAX_IMAGE_SIZE = 1024000, 350 2,33 . ?
. 4, Google Pixel.
, , .
A WHILE LOOP!
,
private fun scaleBitmap() {
if (originalFile.length() > MAX_IMAGE_SIZE) {
var streamLength = MAX_IMAGE_SIZE
var compressQuality = 100
val bmpStream = ByteArrayOutputStream()
while (streamLength >= MAX_IMAGE_SIZE) {
bmpStream.use {
it.flush()
it.reset()
}
compressQuality -= 8
val bitmap = BitmapFactory.decodeFile(originalFile.absolutePath, BitmapFactory.Options())
bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream)
streamLength = bmpStream.toByteArray().size
}
FileOutputStream(compressedFile).use {
it.write(bmpStream.toByteArray())
}
}
}
I think this approach will use exponential time depending on image resolution. A 9 MB image takes up to 12 seconds to compress to 1 MB. Good quality. You can configure this by decreasing the original raster resolution (which looks like a constant operation) by doing:
options.inSampleSize = 2;
What we need to do is somehow calculate the quality compression for any image. There should be math around this so that we can determine compressQuality from the original image size or width + height.
source
share