Android Bitmap Size Get inSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)

Here you can find the source of inSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)

Description

in Sample Size

Declaration

public static final int inSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) 

Method Source Code

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.util.Base64;
import android.view.View;

public class Main{
    public static final int inSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height
                        / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }/*from w w w .j ava 2  s.co m*/

            final float totalPixels = width * height;
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
}

Related

  1. computeInitialSampleSize( BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  2. computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  3. computeSampleSize(BitmapFactory.Options options, int target)
  4. getBitmapSize(String filePath)
  5. getImageWH(String path)
  6. obtainBitmapSize(String pathName)
  7. computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  8. computeInitialSampleSize( BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  9. computeInitialSampleSize(int w, int h, int minSideLength, int maxNumOfPixels)