Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static Bitmap decodeBitmapFromFileToSize(String filePath, int reqWidth, int reqHeight,
            boolean maintainAspectRatio) {
        Bitmap original = BitmapFactory.decodeFile(filePath);
        if (original == null) {
            return null;
        }
        if (original.getWidth() == reqWidth || original.getHeight() == reqHeight) {
            return original;
        }

        if (maintainAspectRatio) {
            int oriWidth = original.getWidth(), oriHeight = original.getHeight();
            if (oriWidth >= oriHeight) {
                float aRatio = (float) oriHeight / (float) oriWidth;
                reqHeight = (int) (reqWidth * aRatio);
            } else if (oriHeight > oriWidth) {
                float aRatio = (float) oriWidth / (float) oriHeight;
                reqWidth = (int) (reqHeight * aRatio);
            }
        }

        Bitmap scaled = Bitmap.createScaledBitmap(original, reqWidth, reqHeight, false);
        if (scaled == null) {
            return original;
        }
        if (scaled != original) {
            original.recycle();
            original = null;
        }
        return scaled;
    }
}