1

I have a JPEG from my camera that is 3264x1952. This file is about 1.7 megabytes.

I'm wondering is there anyway to compress this image to a smaller file size without changing the resolution. i.e. I want my resultant image to also be 3264x1952.

I cannot even open this JPEG as a bitmap without using the inSampleSize option to scale down the image.

Anyone know what my options are? Is there any way to reduce color bit depth/increase compression/reduce quality?

edit: I'm specifically looking for a solution for Android. Main problem being I can't open the full res JPEG without hitting a OOM error, so I don't know how to proceed.

Ideally I'd like to do:

bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);

But the application will crash when I do:

Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
blinduck
  • 428
  • 1
  • 5
  • 20

3 Answers3

1

you can use the BitmapFactory.decodeFile method that takes as second paramter a BitmapFactory.Options object and use inSampleSize to scale the image. For instance an inSampleSize = 2 will produce an output image of width/2 and height/2, . Just remember that your bitmap need always width x heigth x 4 bytes of memory

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

I think you want to use BitmapFactory.decodeStream, taking in a stream from the JPEG file. You can modify the density before the bitmap is returned.

Ken
  • 30,811
  • 34
  • 116
  • 155
0

You need to scale down your image.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.

Use appropriate Bitmap.decode method and scale down the image.

Bitmap.decode

Example :

Call the method with the required parameters.

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
 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_WIDTH=WIDTH;
     final int REQUIRED_HIGHT=HIGHT;
     //Find the correct scale value. It should be the power of 2.
     int scale=1;
     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
         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;
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256