Gaurav Arora
Gaurav Arora

Reputation: 8362

android camera image compression

I am making a camera application. And I have to save the images after camera click on to server and as the size of the image captured is very large always ( in Mb's) . So it is always dificult for me to save the large sized image on the server. Is there any to compress the image before saving it.??

And I have to only use android native camera

Thanks

Upvotes: 1

Views: 4830

Answers (3)

Laviniux
Laviniux

Reputation: 364

Another approach would be to directly make smaller pictures. The advantage would be that you use less memory, but you might need a big picture in another part of your app.

This can be done as follows:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
...
   Camera.Parameters mParameters = mCamera.getParameters();
   List<Size> sizes = mParameters.getSupportedPictureSizes();
   Size optimalSize = getOptimalSize(sizes, width, height);
   if (optimalSize != null && !mParameters.getPictureSize().equals(optimalSize))
        mParameters.setPictureSize(optimalSize.width, optimalSize.height);
...
}  

To choose the optimal size, you can use any criteria you want. I tried to make it as close as possible to the screen size:

 private Size getOptimalSize(List<Size> sizes, int w, int h){

    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;
    if (sizes == null)
        return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Size size: sizes)
    {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff)
        {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null)
    {
        minDiff = Double.MAX_VALUE;
        for (Size size: sizes)
        {
            if (Math.abs(size.height - targetHeight) < minDiff)
            {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }

    return optimalSize;

}

Upvotes: 1

Naveen Kumar
Naveen Kumar

Reputation: 3818

try this

            Bitmap bmp = (Bitmap) data.getExtras().get("data");

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

Upvotes: 0

pixelscreen
pixelscreen

Reputation: 1975

You need to resize the bitmap before actually uploading it to the server. This code returns a resized bitmap. Reduce the bitmap to required width and required height - it will result in a much smaller image file.

public static Bitmap getBitmapImages(final String imagePath, final int requiredWidth, final int requiredHeight)
{
    System.out.println(" --- image_path in getBitmapForCameraImages --- "+imagePath+" - reqWidth & reqHeight "+requiredWidth+" "+requiredHeight);
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = true;
    options.inJustDecodeBounds = true;

    // First decode with inJustDecodeBounds=true to check dimensions
    bitmap = BitmapFactory.decodeFile(imagePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);

    options.inJustDecodeBounds = false;

    // Decode bitmap with inSampleSize set
    bitmap = BitmapFactory.decodeFile(imagePath, options);

    return bitmap;
}

Upvotes: 2

Related Questions