How to reduce JPEG image size in Android

In my application, I call the camera application and take a picture and save it in a specific directory (e.g. / sdcard, etc.)

The image is saved as a JPEG image. How to reduce image size? Is there an image encoder or compression available?

I came across another publication: Android reduces camera image size

But this is image scaling. I am looking for something that can compress or encode. Is it possible?

Thanks in advance, Perumal

+6
source share
2 answers

I'm not sure that you can try this anyway. To reduce the size of the image, you first need to convert the image to a bitmap before saving it in a specific directory, and compress the bitmap, set the image quality and write it to the correct path. Image quality can be changed and we hope that this helps you reduce image size.

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);` 

API :

 compress (Bitmap.CompressFormat format, int quality, OutputStream stream) 
+13
source

You may find that the code snippet below is useful for you.

  opt = new BitmapFactory.Options(); opt.inTempStorage = new byte[16 * 1024]; opt.inSampleSize = 4; opt.outWidth = 640; opt.outHeight = 480; Bitmap imageBitmap = BitmapFactory .decodeStream(in, new Rect(), opt); Bitmap map = Bitmap.createScaledBitmap(imageBitmap, 100, 100, true); BitmapDrawable bmd = new BitmapDrawable(map); ByteArrayOutputStream bao = new ByteArrayOutputStream(); map.compress(Bitmap.CompressFormat.PNG, 90, bao); ba = bao.toByteArray(); imagedata=Base64.encodeBytes(ba); 
+1
source

Source: https://habr.com/ru/post/913385/


All Articles