Java BufferedImage Size Get getSize(int width, int height, Image image)

Here you can find the source of getSize(int width, int height, Image image)

Description

get Size

License

Open Source License

Declaration

public static int[] getSize(int width, int height, Image image) 

Method Source Code


//package com.java2s;
import java.awt.Image;

public class Main {

    public static int[] getSize(int width, int height, Image image) {
        int targetWidth = image.getWidth(null);
        int targetHeight = image.getHeight(null);
        double scaling = getScaling(targetWidth, targetHeight, width, height);
        long standardWidth = Math.round(targetWidth * scaling);
        long standardHeight = Math.round(targetHeight * scaling);
        return new int[] { Integer.parseInt(Long.toString(standardWidth)),
                Integer.parseInt(String.valueOf(standardHeight)) };
    }//from   w w  w . j a v a 2 s  .  com

    public static int[] getSize(float scale, Image image) {
        int targetWidth = image.getWidth(null);
        int targetHeight = image.getHeight(null);
        long standardWidth = Math.round(targetWidth * scale);
        long standardHeight = Math.round(targetHeight * scale);
        return new int[] { Integer.parseInt(Long.toString(standardWidth)),
                Integer.parseInt(String.valueOf(standardHeight)) };
    }

    public static int[] getSize(int width, Image image) {
        int targetWidth = image.getWidth(null);
        int targetHeight = image.getHeight(null);
        long height = Math.round((targetHeight * width) / (targetWidth * 1.00f));
        return new int[] { width, Integer.parseInt(String.valueOf(height)) };
    }

    public static double getScaling(double targetWidth, double targetHeight, double standardWidth,
            double standardHeight) {
        double widthScaling = 0d;
        double heightScaling = 0d;
        if (targetWidth > standardWidth) {
            widthScaling = standardWidth / (targetWidth * 1.00d);
        } else {
            widthScaling = 1d;
        }
        if (targetHeight > standardHeight) {
            heightScaling = standardHeight / (targetHeight * 1.00d);
        } else {
            heightScaling = 1d;
        }
        return Math.min(widthScaling, heightScaling);
    }
}

Related

  1. getDimension(BufferedImage img)
  2. getDimension(File f)
  3. getDimension(final Image anImage, final ImageObserver obs)
  4. getDimensions(byte[] image)
  5. getSize(BufferedImage image)
  6. getSizeByHeight(int height, Image image)
  7. getSizedImage(final URL url, final Dimension size)