Generates sampling factor for BitmapFactory.Options.inSampleSize according to the passed Scale Type. - Android Graphics

Android examples for Graphics:Bitmap Option

Description

Generates sampling factor for BitmapFactory.Options.inSampleSize according to the passed Scale Type.

Demo Code

/*/*w w w.ja va2s  . co m*/
 * The MIT License Copyright (c) 2014 Krayushkin Konstantin (jangokvk@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.PointF;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main{
    /**
     * Maximum texture size, witch Android could handle. That is, maximum {@link android.graphics.Bitmap}
     * size, witch won't through {@link java.lang.OutOfMemoryError}.
     * <br /><br />
     * <p/>
     * Actually maximum texture size for now is 2048x2048, but here was chosen 2000x2000
     * just in case.
     */
    public static final int MAX_TEXTURE_SIZE = 2000;
    /**
     * Generates sampling factor for BitmapFactory.Options.inSampleSize according
     * to the passed ScaleType.
     */
    protected static BitmapFactory.Options genBFOptions(
            ScaleType scaleType, int destW, int destH, int srcW, int srcH) {
        final BitmapFactory.Options ops = new BitmapFactory.Options();
        ops.inSampleSize = Math.max(1, (int) Math.ceil(1 / scaleType
                .resolveScale(destW, destH, srcW, srcH).x));
        if (ops.inSampleSize == 1)
            return ops;

        // 2^pow = ops.inSampleSize
        double pow = Math.log(ops.inSampleSize) / Math.log(2);
        // incPow == true, if ops.inSampleSize should be increased; by default it
        // is decreased - look "Note" at the end of inSampleSize description
        boolean incPow;

        if (scaleType != ScaleType.PROPORTIONAL_CROP)
            // incPow = true, if ops.inSampleSize is NOT a power of 2 (that is, pow is not integer)
            incPow = (pow - Math.ceil(pow) + 1) > 0.00001;
        else {
            // square of the resulting image, if we scale it with increased ops.inSampleSize
            int largeSquare = (int) (srcW * srcH / Math.pow(
                    (int) Math.ceil(pow), 2));
            // square of the resulting image, if we scale it with ops.inSampleSize as is
            int smallSquare = (int) (srcW * srcH / Math.pow(
                    (int) (Math.ceil(pow) - 1), 2));
            // incPow = true, if resulting size after scaling with increased ops.inSampleSize is CLOSER to
            // the target size, than resulting size after scaling without increasing ops.inSampleSize;
            // and it is still bigger, than the target size (scaleType == PROPORTIONAL_CROP)
            incPow = Math.abs(destH * destW - largeSquare) < Math.abs(destH
                    * destW - smallSquare)
                    && destH * destW <= largeSquare;
        }

        // finally, increasing ops.inSampleSize if needed
        if (incPow)
            ops.inSampleSize = (int) Math.pow(2, Math.ceil(pow));
        return ops;
    }
    /**
     * Wrapper for {@link ru.jango.j0util.BmpUtil.ScaleType#resolveScale(int, int, int, int)}, witch
     * also checks bounds for {@link #MAX_TEXTURE_SIZE}.
     */
    protected static PointF resolveScale(ScaleType scaleType, int destW,
            int destH, int srcW, int srcH) {
        destW = Math.min(destW, MAX_TEXTURE_SIZE);
        destH = Math.min(destH, MAX_TEXTURE_SIZE);

        PointF scales = scaleType.resolveScale(destW, destH, srcW, srcH);
        if (srcW * scales.x > MAX_TEXTURE_SIZE
                || srcH * scales.y > MAX_TEXTURE_SIZE)
            scales = ScaleType.PROPORTIONAL_FIT.resolveScale(destW, destH,
                    srcW, srcH);

        return scales;
    }
}

Related Tutorials