Java Image Load loadImage(final String src)

Here you can find the source of loadImage(final String src)

Description

Loads image from specified source file

License

Open Source License

Declaration


public static BufferedImage loadImage(final String src) 

Method Source Code


//package com.java2s;
/*//w w  w .  ja v  a2  s .  c  o  m
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

public class Main {
    /**
     * Loads image from specified source file
     */

    public static BufferedImage loadImage(final String src) {
        return loadImage(new File(src));
    }

    public static BufferedImage loadImage(final File file) {
        try {
            return ImageIO.read(file);
        } catch (final Throwable e) {
            return null;
        }
    }

    /**
     * Loads image from URL
     */

    public static ImageIcon loadImage(final URL url) {
        try {
            //            URLConnection uc = url.openConnection ();
            //            ProxyManager.setProxySettings ( uc );
            //            InputStream inputStream = uc.getInputStream ();
            //            ImageIcon imageIcon = loadImage ( inputStream );
            //            inputStream.close ();
            //            return imageIcon;
            return new ImageIcon(url);
        } catch (final Throwable e) {
            return null;
        }
    }

    /**
     * Loads image from specified resource near class
     */

    public static ImageIcon loadImage(final Class nearClass, final String src) {
        try {
            return new ImageIcon(nearClass.getResource(src));
        } catch (final Throwable e) {
            return null;
        }
    }

    /**
     * Loads image from InputStream
     */

    public static ImageIcon loadImage(final InputStream inputStream) {
        try {
            return new ImageIcon(ImageIO.read(inputStream));
        } catch (final Throwable e) {
            return null;
        }
    }
}

Related

  1. getImageFromClassResource(Class cls, String resource)
  2. getImageFromFile(String fileName)
  3. getImageFromSource(JPanel source, Dimension dim)
  4. getLocal(String imagePath)
  5. loadImage(File original)
  6. loadImage(InputStream s)
  7. loadImage(Object parent, String pathToImage)
  8. loadImage(String filename)
  9. loadImage(String filePath, int imageSize)