Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.BitmapFactory;

public class Main {
    public static BitmapFactory.Options getBitmapDecodeOptions(String imagePath, int outWidth, int outHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);

        options.inSampleSize = calculateSampleSize(options, outWidth, outHeight);
        options.inJustDecodeBounds = false;
        return options;
    }

    private static int calculateSampleSize(BitmapFactory.Options options, int dstWidth, int dstHeight) {
        // Calculate inSampleSize
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > dstHeight || width > dstWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ((halfHeight / inSampleSize) > dstHeight && (halfWidth / inSampleSize) > dstWidth) {
                inSampleSize <<= 1;
            }
        }
        return inSampleSize;
    }
}