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.Bitmap;

import android.util.Log;

public class Main {
    /**
     * Scales an image maintaining aspect ratio
     *
     * @param original  original image
     * @param maxWidth  maximum width
     * @param maxHeight maximum height
     * @return a Bitmap representing the thumbnail
     */
    public static Bitmap createThumbnailForImage(Bitmap original, int maxWidth, int maxHeight) {
        int width = original.getWidth();
        int height = original.getHeight();

        Log.v("Pictures", "Width and height are " + width + "--" + height);

        if (width > height) {
            // landscape
            float ratio = (float) width / maxWidth;
            width = maxWidth;
            height = (int) (height / ratio);
        } else if (height > width) {
            // portrait
            float ratio = (float) height / maxHeight;
            height = maxHeight;
            width = (int) (width / ratio);
        } else {
            // square
            height = maxHeight;
            width = maxWidth;
        }

        Log.v("Pictures", "after scaling Width and height are " + width + "--" + height);

        return Bitmap.createScaledBitmap(original, width, height, true);
    }
}