3

The problem is... suppose I have very good quality camera.It return high resolution picture.But I want to upload that image to server. So I want to reduce the picture quality if image size is very big.Any help? Thanks is advance.

Arnab Kundu
  • 1,287
  • 2
  • 12
  • 17
  • Possible duplicate of [How to save a JPEG image on Android with a custom quality level](http://stackoverflow.com/questions/4579647/how-to-save-a-jpeg-image-on-android-with-a-custom-quality-level) – Ian Grainger Feb 23 '17 at 14:44

4 Answers4

1

Yes, you can use Bitmap.compress() to do it, all you need to do is specify compress format, quality and output format. Refer following link "https://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)".

Let me know if it helps you !!

Saarang Tiwari
  • 291
  • 2
  • 12
  • this is good. But I think it will take some time app will be slower.Is there ant way to change camera setting pragmatically which will give me small size(in Kb) image output? – Arnab Kundu Feb 23 '17 at 14:50
  • Try out the following if you find the saving of bitmap to be slow: Try to use JPEG format and use PNG only when it is absolutely necessary. Use bitmap.compress`(Bitmap.CompressFormat.JPEG, 100, out);` If you are creating the bitmap and you don't care about the transparency, then use `Bitmap.Config.RGB_565` instead of `Bitmap.Config.ARGB_8888`. Use `Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);` – Saarang Tiwari Feb 23 '17 at 14:53
1

If writing Android code I suggest you set the image quality you need and store/transcode as a JPEG

This is an answer to the opposite question - which should give you what you need?: How to compress Bitmap as JPEG with least quality loss on Android?

Community
  • 1
  • 1
Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
  • 1
    Thanks But I want android code. Not App. Actually I have written code for camera and upload image but I want to reduce image size(in kb) before upload. – Arnab Kundu Feb 23 '17 at 14:43
  • 1
    @ArnabKundu I've changed my answer - and suggested this might be a duplicate of another question ... How do you store a JPEG with x% quality in Android. HTH – Ian Grainger Feb 23 '17 at 14:46
  • Thanks for your help. I'll check that. – Arnab Kundu Feb 23 '17 at 14:54
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/15316344) – Maksim Simkin Feb 24 '17 at 07:04
  • @MaksimSimkin Sure - but I think it's actually a duplicate of either that question or the one I marked it as a dupe of... I think it'd be better to accept this as a duplicate. – Ian Grainger Feb 24 '17 at 09:38
1

I think it is the issue with the Image Format of Your Uploadable Image usually *.raw has the largest size. You should try to upload the jpg/png images. When saving the images just use Filename.jpg as the file name and the image will get saved .ready for upload .You may delete the file once it is uploaded. the changes won't get reflected to your gallery till the the Media Store probe { service } get updated. You can use this https://github.com/googlesamples/android-Camera2Basic as a reference to build your app

Aloy A Sen
  • 764
  • 5
  • 9
1

try this: To get Specific size image in KB

/**
 * reduces the size of the image
 * param image bitmap received from camera
 * @param maxSize
 * @return
 */
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float)width / (float) height;
    if (bitmapRatio > 0) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

use it like:

Bitmap scaledImage = getResizedBitmap(photo, 200); //here 200 is maxsize

From Wikipedia

For highest quality images (Q=100), about 8.25 bits per color pixel is required

So, for Q=100 on an 200x200 image, that would result in (200 * 200) px * 8.25 bits/px = 330000 bits = ~ 41 kB which is surely less than 64KB

you can try for other dimension too.. just change the maxSize parameter

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62