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 java.io.File;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

public class Main {

    public static Bitmap decode(File file, int targetWidth, int targetHeight) {

        if (file == null || !file.exists() || file.length() == 0) {

            return null;
        }

        String pathName = file.getPath();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);

        /*
         * If set to a value > 1, requests the decoder to subsample the original
         * image, returning a smaller image to save memory. The sample size is
         * the number of pixels in either dimension that correspond to a single
         * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
         * an image that is 1/4 the width/height of the original, and 1/16 the
         * number of pixels. Any value <= 1 is treated the same as 1. Note: the
         * decoder uses a final value based on powers of 2, any other value will
         * be rounded down to the nearest power of 2.
         */
        options.inSampleSize = computeImageSampleSize(options, targetWidth, targetHeight);

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathName, options);

    }

    private static int computeImageSampleSize(Options options, int targetWidth, int targetHeight) {
        return Math.max(options.outWidth / targetWidth, options.outHeight / targetHeight);

    }
}