Java Image Create makeImageResizer(String srcImgPath, String targerPath, int scaledWidth, int scaledHeight, boolean preserveAlpha)

Here you can find the source of makeImageResizer(String srcImgPath, String targerPath, int scaledWidth, int scaledHeight, boolean preserveAlpha)

Description

make Image Resizer

License

Apache License

Declaration

public static void makeImageResizer(String srcImgPath, String targerPath, int scaledWidth, int scaledHeight,
        boolean preserveAlpha) throws IOException 

Method Source Code


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

import javax.imageio.ImageIO;
import java.awt.*;

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

public class Main {

    public static void makeImageResizer(String srcImgPath, String targerPath, int scaledWidth, int scaledHeight,
            boolean preserveAlpha) throws IOException {
        BufferedImage originalImage = ImageIO.read(new File(srcImgPath));

        int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
        BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
        Graphics2D g = scaledBI.createGraphics();
        if (preserveAlpha) {
            g.setComposite(AlphaComposite.Src);
        }/*w  w  w  . j  av a  2 s  .c o m*/
        g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
        g.dispose();

        ImageIO.write(scaledBI, getImageFileSuffix(srcImgPath), new File(targerPath));
    }

    public static String getImageFileSuffix(String imagePath) {
        String imageSuffix = "jpg";
        int index = imagePath.lastIndexOf(".");
        if (index > -1) {
            imageSuffix = imagePath.substring(index + 1);
        }
        return imageSuffix;
    }
}

Related

  1. getImage(final String name)
  2. getImage(ImageIcon imageIcon)
  3. getImage(JComponent component)
  4. getImage(Object obj, String filePath)
  5. makeImage(double[][] levels)