0

I Need to compress the image size after a taken a photo. I want to decrease the size to a maximum of 400K.

So, the average image size after taken the photo is about 3.3MB. I need to compress it to 400K.

What is the best option for this ?

I have tried :

Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

. The code below allow me to reduce the size by way of width and height, but not is storage space.

Bitmap bitmap = Bitmap.createScaledBitmap(capturedImage, width, height, true);

i find this sample from https://stackoverflow.com/a/823966/556337, But he does not explain how to make a image of a maxim size of XXX.MB. There is there a way to implement my issue. ?

// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE=70;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while(o.outWidth / scale / 2 >= REQUIRED_SIZE && 
              o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
Community
  • 1
  • 1
Thiago
  • 12,778
  • 14
  • 93
  • 110

0 Answers0