Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * This method is used to create a sample size for a bitmap given the
 * required size and the Options class for the bitmap.
 * 
 * Run this method after first running
 * 
 * <pre>
 * <code>
 * final BitmapFactory.Options foo = new BitmapFactory.Options();
 * foo.inJustDecodeBounds = true; 
 * BitmapFactory.decodeResource(Resources, int, foo);
 * </code>
 * </pre>
 * 
 * Then set the output to <code>foo.inSampleSize</code> and then decode the
 * image.
 * 
 * (If using the same BitmapFactory, remember to change
 * <code>inJustDecodeBounds</code> back to false.)
 * 
 * This method was taken from the Developer tutorial on the android website
 * (licensed under Creative Commons) The original source can be found here:
 * {@link http
 * ://developer.android.com/training/displaying-bitmaps/load-bitmap.html}
 * 
 * @param options
 *            A bitmap options class created with
 * @param reqWidth
 *            The preferred width of the image.
 * @param reqHeight
 *            The preferred height of the image.
 * @return The sample size (to be used set to options.inSampleSize)
 */

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

import android.util.Log;

public class Main {
    private static String TAG = "BitmapHelper";

    /**
     * Allows decoding of a bitmap with a specified height and width.
     * 
     * Modified from the Android developer tutorial. Original source can be
     * found at {@link http
     * ://developer.android.com/training/displaying-bitmaps/load-bitmap.html}
     * 
     * @param filename
     *            The filename of the file to be decoded.
     * @param reqWidth
     *            The preferred width of the output bitmap.
     * @param reqHeight
     *            The preferred height of the output bitmap.
     * @return the decoded bitmap, or null
     */
    public static Bitmap decodeSampledBitmapFromFile(String filename, float reqWidth, float reqHeight) {
        Log.v(TAG, "Recieved " + filename + " with (w,h): (" + reqWidth + ", " + reqHeight + ").");
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap decodedBitmap = BitmapFactory.decodeFile(filename, options);
        Log.v(TAG, "The Bitmap is " + decodedBitmap.toString());
        return decodedBitmap;
    }

    /**
     * This method is used to create a sample size for a bitmap given the
     * required size and the Options class for the bitmap.
     * 
     * Run this method after first running
     * 
     * <pre>
     * <code>
     * final BitmapFactory.Options foo = new BitmapFactory.Options();
     * foo.inJustDecodeBounds = true; 
     * BitmapFactory.decodeResource(Resources, int, foo);
     * </code>
     * </pre>
     * 
     * Then set the output to <code>foo.inSampleSize</code> and then decode the
     * image.
     * 
     * (If using the same BitmapFactory, remember to change
     * <code>inJustDecodeBounds</code> back to false.)
     * 
     * This method was taken from the Developer tutorial on the android website
     * (licensed under Creative Commons) The original source can be found here:
     * {@link http
     * ://developer.android.com/training/displaying-bitmaps/load-bitmap.html}
     * 
     * @param options
     *            A bitmap options class created with
     * @param reqWidth
     *            The preferred width of the image.
     * @param reqHeight
     *            The preferred height of the image.
     * @return The sample size (to be used set to options.inSampleSize)
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, float reqWidth, float reqHeight) {
        // 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(height / reqHeight);
            final int widthRatio = Math.round(width / 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;
        }
        Log.v(TAG, "inSampleSize = " + inSampleSize);
        return inSampleSize;
    }
}