Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress image/byteArray from CameraX

Tags:

android

I'm using the CameraX plugin which gives me a android.media.Image when I take a picture. Now I want to send the image over a webcall as a byteArray. I found a function that will create the byteArray for me:

private suspend fun createBytesFromImage(input: Image): ByteArray {
    return withContext(ioDispatcher) {
        val buffer = input.planes[0].buffer
        buffer.rewind()
        val bytes = ByteArray(buffer.capacity())
        buffer.get(bytes)
        return@withContext bytes
    }
}

The image is pretty large and I want to compress/downscale it so a save some bandwidth. How do I do this with a android.media.Image or byteArray?

(I know I can make a bitmap from the byteArray, compress/downscale the bitmap and make a new byteArray out of it but this seems wrong. I'm looking for another way, if that's possible)

like image 250
Martijn Avatar asked Sep 02 '25 10:09

Martijn


1 Answers

Use instead .setTargetResolution(Size(1080,1920)) on ImageCapture.Builder

To calculate file size according to resolution you can use this tool

like image 155
nanohack Avatar answered Sep 05 '25 00:09

nanohack