Java BufferedImage Thumbnail thumbnail(File input, File output, int sizeX, int sizeY)

Here you can find the source of thumbnail(File input, File output, int sizeX, int sizeY)

Description

Create a file containing a thumbnail image from another file.

License

LGPL

Parameter

Parameter Description
input file to be read
output file to be generated
sizeX Width of generated image in pixels
sizeY Height of generated image in pixels

Declaration

public static void thumbnail(File input, File output, int sizeX, int sizeY) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.Graphics;

public class Main {
    /** //from w w w. j a  va  2  s  . c o  m
     * Determines quantity of diagnostic output to be generated. 
     * @see #getDebugLevel()
     * @see #setDebugLevel(int)
     */
    protected static int debugLevel = 0;

    /**
     * Create a file containing a thumbnail image from another file.
     * @param inputFile Name of file to be read
     * @param outputFile Name of file to be generated
     * @param sizeX Width of generated image in pixels
     * @param sizeY Height of generated image in pixels
     * @see BufferedImage
     * @see javax.imageio.ImageIO
     * @see java.awt.image.RenderedImage
     */
    public static void thumbnail(String inputFile, String outputFile, int sizeX, int sizeY) throws IOException {
        thumbnail(new File(inputFile), new File(outputFile), sizeX, sizeY);
    }

    /**
     * Create a file containing a thumbnail image from another file.
     * @param input  file to be read
     * @param output  file to be generated
     * @param sizeX Width of generated image in pixels
     * @param sizeY Height of generated image in pixels
     * @see BufferedImage
     * @see javax.imageio.ImageIO
     * @see java.awt.image.RenderedImage
     */
    public static void thumbnail(File input, File output, int sizeX, int sizeY) throws IOException {
        if (debugLevel > 2) {
            System.out.println("Arguments: " + input.getCanonicalPath() + ", " + output.getCanonicalPath() + ", "
                    + Integer.toString(sizeX) + ", " + Integer.toString(sizeY));
        }
        Integer targetX = 0;
        Integer targetY = 0;
        if (sizeX <= 0 && sizeY <= 0) {
            throw new IOException("Either target width or target height must be positive");
        } else if (sizeX == 0 || sizeY == 0) {
            throw new IOException("Zero not acceptable for target width or target height");
        }
        String inputFileName = input.getCanonicalPath();
        BufferedImage image = null;
        BufferedImage thumb = null;
        Image intermediate = null;
        String format = null;
        String suffix = null;
        FileOutputStream outputStream = null;
        String outputName = output.getName();
        suffix = outputName.substring(outputName.lastIndexOf(".") + 1);
        if (suffix == null) {
            throw new IOException("No suffix found for output file " + outputName + " (null value)");
        } else if (suffix.equalsIgnoreCase("jpg") || suffix.equalsIgnoreCase("jpeg")) {
            format = "jpeg";
        } else if (suffix.equalsIgnoreCase("png")) {
            format = "png";
        } else if (suffix.equalsIgnoreCase("gif")) {
            format = "gif";
        } else {
            throw new IOException("Unable to process suffix " + suffix + " for file " + outputName);
        }
        try {
            image = javax.imageio.ImageIO.read(new FileInputStream(input));
        } catch (FileNotFoundException e) {
            throw new IOException("Unable to find file " + inputFileName + " " + e.getMessage());
        } catch (IOException e) {
            throw new IOException("Unexpected error while reading file " + inputFileName + " " + e.getMessage());
        }
        if (sizeX <= 0 || sizeY <= 0) {
            if (debugLevel > 2) {
                System.out.println("Must adjust one parameter");
            }
            Double ratio = (double) image.getWidth() / (double) image.getHeight();
            if (sizeX < 0) {
                targetY = sizeY;
                targetX = (int) (Math.floor((double) sizeY * ratio));
            } else if (sizeY < 0) {
                targetX = sizeX;
                targetY = (int) (Math.floor((double) sizeX / ratio));
            }
        } else if (sizeX > 0 && sizeY > 0) {
            targetX = sizeX;
            targetY = sizeY;
        }
        intermediate = image.getScaledInstance(targetX, targetY, BufferedImage.SCALE_FAST);
        thumb = new BufferedImage(targetX, targetY, BufferedImage.TYPE_INT_RGB);
        Graphics g = thumb.createGraphics();
        g.drawImage(intermediate, 0, 0, null);
        outputStream = new FileOutputStream(output);
        ImageIO.write(thumb, format, outputStream);
    }

    /**
     * Makes file with thumbnail image
     * @param input Input file
     * @param output Output file
     * @param size Height and width of output image in pixels
     * @throws IOException
     */
    public static void thumbnail(String input, String output, int size) throws IOException {
        thumbnail(new File(input), new File(output), size, size);
    }

    /**
     * Makes file with thumbnail image
     * @param input Input file
     * @param output Output file
     * @throws IOException
     */
    public static void thumbnail(String input, String output) throws IOException {
        thumbnail(new File(input), new File(output), 150, 150);
    }
}

Related

  1. thumbnail(final InputStream inputStream, final int width, final int height, final OutputStream outputStream)
  2. thumbnail(int imageWidth, int imageHeight, File originFileName, File thumbFileName)