Android Bitmap Size Get computeSampleSize(float scale)

Here you can find the source of computeSampleSize(float scale)

Description

compute Sample Size

Declaration

public static int computeSampleSize(float scale) 

Method Source Code

//package com.java2s;

import android.graphics.BitmapFactory;

public class Main {
    public static final int UNCONSTRAINED = -1;

    /**/*from   w w w .j av a 2 s  .  c o m*/
     * Compute the sample size as a function of minSideLength
     * and maxNumOfPixels.
     * minSideLength is used to specify that minimal width or height of a
     * bitmap.
     * maxNumOfPixels is used to specify the maximal size in pixels that is
     * tolerable in terms of memory usage.
     * <p/>
     * The function returns a sample size based on the constraints.
     * Both size and minSideLength can be passed in as UNCONSTRAINED,
     * which indicates no care of the corresponding constraint.
     * The functions prefers returning a sample size that
     * generates a smaller bitmap, unless minSideLength = UNCONSTRAINED.
     * <p/>
     * Also, the function rounds up the sample size to a power of 2 or multiple
     * of 8 because BitmapFactory only honors sample size this way.
     * For example, BitmapFactory downsamples an image by 2 even though the
     * request is 3. So we round up the sample size to avoid OOM.
     */
    public static int computeSampleSize(int width, int height,
            int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(width, height,
                minSideLength, maxNumOfPixels);

        return initialSize <= 8 ? nextPowerOf2(initialSize)
                : (initialSize + 7) / 8 * 8;
    }

    public static int computeSampleSize(float scale) {
        assertTrue(scale > 0);
        int initialSize = Math.max(1, (int) Math.ceil(1 / scale));
        return initialSize <= 8 ? nextPowerOf2(initialSize)
                : (initialSize + 7) / 8 * 8;
    }

    private static int computeSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    private static int computeInitialSampleSize(int w, int h,
            int minSideLength, int maxNumOfPixels) {
        if (maxNumOfPixels == UNCONSTRAINED
                && minSideLength == UNCONSTRAINED)
            return 1;

        int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math
                .ceil(Math.sqrt((double) (w * h) / maxNumOfPixels));

        if (minSideLength == UNCONSTRAINED) {
            return lowerBound;
        } else {
            int sampleSize = Math.min(w / minSideLength, h / minSideLength);
            return Math.max(sampleSize, lowerBound);
        }
    }

    private static int computeInitialSampleSize(
            BitmapFactory.Options options, int minSideLength,
            int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math
                .ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == UNCONSTRAINED) ? 128
                : (int) Math.min(Math.floor(w / minSideLength),
                        Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == UNCONSTRAINED)
                && (minSideLength == UNCONSTRAINED)) {
            return 1;
        } else if (minSideLength == UNCONSTRAINED) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

    private static int nextPowerOf2(int n) {
        if (n <= 0 || n > (1 << 30))
            throw new IllegalArgumentException();
        n -= 1;
        n |= n >> 16;
        n |= n >> 8;
        n |= n >> 4;
        n |= n >> 2;
        n |= n >> 1;
        return n + 1;
    }

    private static void assertTrue(boolean cond) {
        if (!cond) {
            throw new AssertionError();
        }
    }
}

Related

  1. computeInitialSampleSize( BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  2. computeInitialSampleSize(int w, int h, int minSideLength, int maxNumOfPixels)
  3. computeInitialSampleSize( BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  4. computeInSampleSizeForMoment(Bitmap map, int newWidth, int newHeight)
  5. computeSampleSize(int width, int height, int minSideLength, int maxNumOfPixels)
  6. computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels)
  7. computeSampleSizeLarger(int w, int h, int minSideLength)
  8. computeSampleSizeLarger(float scale)
  9. getBitmapSize(Bitmap bitmap)