All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mindee.image.ImageUtils Maven / Gradle / Ivy

The newest version!
package com.mindee.image;

import java.awt.image.BufferedImage;

/**
 * Image utility class.
 */
public class ImageUtils {

  /**
   * Dimension class for pages/images.
   */
  public static class Dimensions {
    public final Integer width;
    public final Integer height;

    public Dimensions(Integer width, Integer height) {
      this.width = width;
      this.height = height;
    }
  }

  public static Dimensions calculateNewDimensions(
      BufferedImage original,
      Integer maxWidth,
      Integer maxHeight
  ) {
    if (original == null) {
      throw new IllegalArgumentException("Generated image could not be processed for resizing.");
    }

    if (maxWidth == null && maxHeight == null) {
      return new Dimensions(original.getWidth(), original.getHeight());
    }

    double widthRatio =
        maxWidth != null ? (double) maxWidth / original.getWidth() : Double.POSITIVE_INFINITY;
    double heightRatio =
        maxHeight != null ? (double) maxHeight / original.getHeight() : Double.POSITIVE_INFINITY;

    double scaleFactor = Math.min(widthRatio, heightRatio);

    int newWidth = (int) (original.getWidth() * scaleFactor);
    int newHeight = (int) (original.getHeight() * scaleFactor);

    return new Dimensions(newWidth, newHeight);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy