Home > front end >  Bitmap.compress doesn't decrease image size
Bitmap.compress doesn't decrease image size

Time:10-30

I'm building an app that uses Room database and store images in it. I store the image as ByteArray and convert it with typeconverter to a Bitmap like this :

    @TypeConverter
    fun fromBitmap(bmp: Bitmap): ByteArray{
        val outputStream = ByteArrayOutputStream()
        bmp.compress(Bitmap.CompressFormat.JPEG, 50, outputStream)
        return outputStream.toByteArray()
    }
    @TypeConverter
    fun toBitmap(bytes: ByteArray): Bitmap{
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
    }

but I noticed that when I change the quality the size of the image does not change. What did I do wrong? and how can I decrease the size of the bitmap?

CodePudding user response:

It did compress it. The in memory Bitmap object takes 4 bytes per pixel. It wrote it to the file system in a format that's copressed- a JPG of 50% quality. If you look at the size of the file, it will be smaller than 4*width*height bytes small.

That doesn't mean that if you read it in and wrote it back to disk it will be smaller than the original file. It probably won't be, unless you lower the quality setting in the new file compared to the old. And its recommended not to do that anyway, as doing so will reencode the data with a lossy algorithm, which will degrade the image quality.

CodePudding user response:

A bitmap is pretty much just a stream of data for width * height pixels, where each pixel is represented by a fixed number of bytes enter image description here

  • Related