Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters : Geometry « 2D Graphics GUI « Java






Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters

        
//package com.myapp.util.image;

import java.awt.Dimension;

public class ImageUtil {
    
    /**
     * returns a dimension where width and height are inside the bounds of the
     * maxWidth and maxHeight parameters<br>
     * and the aspect ratio is the same as sourceWidth and sourceHeight.
     * 
     * @param maxWidth
     *            the maximum width of the target dimension
     * @param maxHeight
     *            the maximum height of the target dimension
     * @param sourceWidth
     *            the widht of the source image
     * @param sourceHeight
     *            the height of the source image
     * @return the target dimension that will be the greatest dimension that
     *         <ul>
     *         <li>will keep the ratio of the source images dimension</li>
     *         <li>fits in the bounds of the maximum dimension given</li>
     *         </ul>
     */
    public static Dimension scaleDimensions(int maxWidth,
                                     int maxHeight,
                                     int sourceWidth,
                                     int sourceHeight) {
        float vidH, vidW, maxH, maxW;
        vidW = Integer.valueOf(sourceWidth).floatValue();
        vidH = Integer.valueOf(sourceHeight).floatValue();
        maxW = Integer.valueOf(maxWidth).floatValue();
        maxH = Integer.valueOf(maxHeight).floatValue();
        
//        System.out.println("initial height / width ratio: " + vidH / vidW);
//        System.out.println();
//        System.out.println("vidW "+vidW+", vidH "+vidH);
//        System.out.println("maxW "+maxW+", maxH "+maxH);
//        System.out.println();

        float finalW, finalH;
        finalH = vidH / vidW * maxW;
        finalW = maxW;
        
        if (finalH > maxH) {
            float factor = maxH / finalH;
            finalH = maxH;
            finalW = finalW * factor;
        }

        int w = Float.valueOf(finalW).intValue();
        int h = Float.valueOf(finalH).intValue();
        
        Dimension d = new Dimension(w,h);

//        System.out.println();
//        System.out.println("finalW "+finalW+", finalH "+finalH);
//        System.out.println("final height / width ratio: " + finalH / finalW);
//        System.out.println();
//        System.out.println(d);
        
        return d;
    }

    
    
    // test scaleDimensions
    public static void main(String[] args) {
        int maxWidth = 250;
        int maxHeight = 200;
        int videoWidth = 320;
        int videoHeight = 240;
        Dimension d = scaleDimensions(maxWidth,
                                      maxHeight,
                                      videoWidth,
                                      videoHeight);
        System.out.println(d);
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Collection of geometry utility methods
2.Unions Rectangle2D
3.Interpolates points given in the 2D plane
4.Returns distance between two sets of coords
5.Returns distance between 3D set of coords
6.Returns closest point on segment to point
7.Calculate Angle From
8.Returns distance to segment
9.Hexagon demo
10.Implements an Vector in 3D space.
11.Implementation of the 4 dimensional vector.
12.Quaternion
13.Circle shape
14.Geometry Utilities
15.This is a Polygon that allows the user to flip and swap the points along it's axis.
16.Fast trigonometric operationsFast trigonometric operations
17.A class to represent a latitude and longitude
18.An undirected graph that keeps track of connected components (groups).
19.Generates n logarithmically-spaced points between d1 and d2 using the provided base.