Java BufferedImage Transform transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight)

Here you can find the source of transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight)

Description

transform

License

Apache License

Declaration

public static void transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight)
        throws Exception 

Method Source Code


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

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class Main {

    public static void transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight)
            throws Exception {
        Image image = javax.imageio.ImageIO.read(new File(originalFile));

        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {/*from  w  ww  .j ava  2s .c o m*/
            thumbWidth = (int) (thumbHeight * imageRatio);
        }

        if (imageWidth < thumbWidth && imageHeight < thumbHeight) {
            thumbWidth = imageWidth;
            thumbHeight = imageHeight;
        } else if (imageWidth < thumbWidth)
            thumbWidth = imageWidth;
        else if (imageHeight < thumbHeight)
            thumbHeight = imageHeight;

        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setBackground(Color.WHITE);
        graphics2D.setPaint(Color.WHITE);
        graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

        javax.imageio.ImageIO.write(thumbImage, "JPG", new File(thumbnailFile));
    }
}

Related

  1. copyAndTranslateSubimage(BufferedImage src, Rectangle bounds)
  2. getTransformedImage(BufferedImage image, double scaleX, double scaleY, double shearX, double shearY)
  3. transform(BufferedImage image, int numquadrants)
  4. transform(BufferedImage image, int sx, int sy, int dx, int dy)
  5. TransformGrayToTransparency(BufferedImage image)
  6. transformGrayToTransparency(BufferedImage image)
  7. transformImage(BufferedImage image, AffineTransform transform)
  8. transformImage(BufferedImage image, AffineTransform transform)