Java Image Resize resizeImage(ImageIcon tmpIcon)

Here you can find the source of resizeImage(ImageIcon tmpIcon)

Description

resize Image

License

Open Source License

Declaration

private static ImageIcon resizeImage(ImageIcon tmpIcon) 

Method Source Code


//package com.java2s;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    public final static String jpeg = "jpeg";
    public final static String jpg = "jpg";
    public final static String gif = "gif";
    public final static String png = "png";

    private static ImageIcon resizeImage(ImageIcon tmpIcon) {
        if (tmpIcon != null) {
            if (tmpIcon.getIconWidth() > 90) {
                tmpIcon = new ImageIcon(tmpIcon.getImage().getScaledInstance(100, -1, Image.SCALE_DEFAULT));
            }/*  ww w  . ja v a2  s  . co  m*/
        }

        return tmpIcon;
    }

    public static BufferedImage getImage(String foldername, String filename) {

        BufferedImage image = null;

        String path = getImageLocation(foldername, filename);
        File file = getStoreFile(path);

        if (file != null) {
            System.out.println("File Path: " + file.getAbsolutePath());
            try {
                image = ImageIO.read(file);
            } catch (IOException ioe) {
                System.err.println("Error reading image: " + ioe.getMessage());
            }
        }

        return image;
    }

    public static String getImageLocation(String foldername, String filename) {
        //"./.images/logo/"
        return "./.images/" + foldername + "/" + filename;
    }

    public static File getStoreFile(String filename) {

        Path path = Paths.get(filename + "." + png);
        File file = path.toFile();
        if (file.exists()) {
            return file;
        }

        path = Paths.get(filename + "." + jpeg);
        file = path.toFile();
        if (file.exists()) {
            return file;
        }

        path = Paths.get(filename + "." + jpg);
        file = path.toFile();
        if (file.exists()) {
            return file;
        }

        path = Paths.get(filename + "." + gif);
        file = path.toFile();
        if (file.exists()) {
            return file;
        }

        return null;

    }
}

Related

  1. getResizedImage(Image originalImage, Dimension newSize)
  2. getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)
  3. getResizedImage(String path, int height, int width)
  4. resize(Image img, int newWidth, float quality)
  5. resize(String srcImageFile, String result, int newWidth, float quality)
  6. resizeImage(String imagePath, int width, int height)
  7. resizeImage(String loadFile, String saveFile, int maxDim)