Returns the size (width and height) of an image from the given input stream. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Resize

Description

Returns the size (width and height) of an image from the given input stream.

Demo Code

/*//from w  ww . j av a 2  s  . c om
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
//package com.java2s;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

public class Main {
    /**
     * Returns the size (width and height) of an image from the given input
     * stream. This method closes the given stream.
     *
     * @param input the input stream with an image
     * @return the size (width and height) of an image from the given input
     *         stream
     */
    public static int[] getImageSize(InputStream input) throws IOException {
        try {
            ImageInputStream imageInput = ImageIO
                    .createImageInputStream(input);
            BufferedImage image = ImageIO.read(imageInput);
            return new int[] { image.getWidth(), image.getHeight() };
        } finally {
            input.close();
        }
    }

    /**
     * Returns the possible size of an image from the given input stream; the
     * returned size does not overcome the specified maximal borders but keeps
     * the ratio of the image. This method closes the given stream.
     *
     * @param input the input stream with an image
     * @param maxWidth the maximal width
     * @param maxHeight the maximal height
     * @return the possible size of an image from the given input stream; the
     *         returned size does not overcome the specified maximal borders but
     *         keeps the ratio of the image
     */
    public static int[] getImageSize(InputStream input, int maxWidth,
            int maxHeight) throws IOException {
        int[] size = getImageSize(input);
        return getNewSize(size[0], size[1], maxWidth, maxHeight);
    }

    /**
     * Calculates new size of an image with the specified max borders keeping
     * the ratio between height and width of the image.
     *
     * @param width the initial width of an image
     * @param height the initial height of an image
     * @param maxWidth the maximal width of an image
     * @param maxHeight the maximal height of an image
     * @return a new size of an image where the height and width don't overcome
     *         the specified borders; the size keeps the initial image ratio
     *         between width and height
     */
    public static int[] getNewSize(int width, int height, int maxWidth,
            int maxHeight) {
        if (width <= maxWidth && height <= maxHeight) {
            return new int[] { width, height };
        }
        double thumbRatio = (double) maxWidth / (double) maxHeight;
        double imageRatio = (double) width / (double) height;
        if (thumbRatio < imageRatio) {
            maxHeight = (int) (maxWidth / imageRatio);
        } else {
            maxWidth = (int) (maxHeight * imageRatio);
        }
        return new int[] { maxWidth, maxHeight };
    }
}

Related Tutorials