Decodes an image File to Bitmap resizing the image to be inSampleSize times smaller then the original. - Android Graphics

Android examples for Graphics:Bitmap File

Description

Decodes an image File to Bitmap resizing the image to be inSampleSize times smaller then the original.

Demo Code

/*******************************************************************************
 * Copyright 2013 AppGlu, Inc.// w w w .j ava2 s.c  o  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
//package com.java2s;
import java.io.File;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    /**
     * Decodes an image File to Bitmap resizing the image to be <code>inSampleSize</code> times smaller then the original.
     * @param file the image file reference
     * @param inSampleSize how much smaller that the image will be, for example, <code>inSampleSize</code> equals 2 will return an image 1/2 the size of the original
     * @return the resized Bitmap
     */
    public static Bitmap decodeSampledBitmapFromFile(File file,
            int inSampleSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = inSampleSize;

        return BitmapFactory.decodeFile(file.getPath(), options);
    }

    /**
     * Decodes an image File to Bitmap resizing the image to fit the <code>requestedWidth</code> and <code>requestedHeight</code> dimensions.
     * @param file the image file reference
     * @param requestedWidth the final image width will be close to the requestedWidth (value is in pixels)
     * @param requestedHeight the final image height will be close to the requestedHeight (value is in pixels)
     * @return the resized Bitmap
     */
    public static Bitmap decodeSampledBitmapFromFile(File file,
            int requestedWidth, int requestedHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(file.getPath(), options);

        int inSampleSize = calculateSampleSize(options, requestedWidth,
                requestedHeight);

        return decodeSampledBitmapFromFile(file, inSampleSize);
    }

    /**
     * Giving a max width and height in pixels this method will calculate a good value to use in BitmapFactory.Options.inSampleSize.
     */
    public static int calculateSampleSize(BitmapFactory.Options options,
            int requestedWidth, int requestedHeight) {
        // Raw height and width of image
        int width = options.outWidth;
        int height = options.outHeight;
        int inSampleSize = 1;

        if (width > requestedWidth || height > requestedHeight) {
            // Calculate ratios of height and width to requested height and width
            final int widthRatio = Math.round((float) width
                    / (float) requestedWidth);
            final int heightRatio = Math.round((float) height
                    / (float) requestedHeight);

            // 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