Calculates the size of the Bitmap image - Android Graphics

Android examples for Graphics:Bitmap Size

Description

Calculates the size of the Bitmap image

Demo Code


//package com.java2s;

import android.graphics.BitmapFactory;

public class Main {
    /**/*from w  ww  . j a  va  2s. com*/
     * Calculates the size of the image
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        if (options == null || reqWidth == 0 || reqHeight == 0)
            throw new IllegalArgumentException();

        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width
                    / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio
                    : widthRatio;
        }
        return inSampleSize;
    }
}

Related Tutorials