7

In my application, I am invoking the camera application and taking a picture and saving it in a particular directory (E.g. /sdcard etc)

The picture is saved as a JPEG image. How do I reduce the size of the image? Is there any available image encoder or compression available?

I came across another posting at: Android Reduce Size Of Camera Picture

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

Thanks In Advance, Perumal

Community
  • 1
  • 1
perumal316
  • 1,159
  • 6
  • 17
  • 36
  • http://stackoverflow.com/questions/4943016/android-how-to-compress-or-downsize-an-image – m4n07 Apr 17 '12 at 04:05
  • http://stackoverflow.com/questions/6464062/compress-image-in-android – m4n07 Apr 17 '12 at 04:09
  • [Take a look from this link][1] [1]: http://stackoverflow.com/questions/10081008/outofmemoryerror-in-game-with-many-small-images/10086141#10086141 Hope it will help you. – Suvam Roy Apr 17 '12 at 06:38

3 Answers3

13

I am not sure anyway you can try this one.For reducing the size of the image first you should convert the image to bitmap before saving it to the particular directory, and compress the bitmap set the quality of the image and write it to the correct path.The quality of the image can be changed and hope this will help you to reduce the size of the image.

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

API:

compress (Bitmap.CompressFormat format, int quality, OutputStream stream)
C. Ross
  • 31,137
  • 42
  • 147
  • 238
Sreedev
  • 6,563
  • 5
  • 43
  • 66
1

May be the below piece of code 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);
Manju
  • 720
  • 9
  • 23
0

Using Glide inspired by this writeup

https://developer.android.com/topic/performance/graphics/load-bitmap

Gilde API

https://bumptech.github.io/glide/

// using some options for JPEG
RequestOptions myOptions = new RequestOptions()
                        .encodeFormat(Bitmap.CompressFormat.JPEG)
                        .override(1280, 1024) // this is what you need to take care of WxH
                        .fitCenter(); // or centerCrop
// uri may be local or web url anything
Glide.with(this).asBitmap().load(uri).apply(myOptions).listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        showSnackBar("There was some error in fetching images");
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap bitmap, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        // do whatever with the bitmap object here
                        return true;
                    }
                }).submit();
Divyanshu Kumar
  • 1,272
  • 15
  • 15