SHARE
TWEET

Untitled

reyhanzo Dec 15th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  import java.awt.image.*;  
  2.  import javax.imageio.*;  
  3.  import java.io.*;  
  4.  /**  
  5.   * ImageFileManager is a small utility class with static methods to load  
  6.   * and save images.  
  7.   *  
  8.   * The files on disk can be in JPG or PNG image format. For files written  
  9.   * by this class, the format is determined by the constant IMAGE_FORMAT.  
  10.   *  
  11.   * @author Michael Kolling and David J Barnes  
  12.   * @version 2.0  
  13.   */  
  14.  public class ImageFileManager  
  15.  {  
  16.    // A constant for the image format that this writer uses for writing.  
  17.    // Available formats are "jpg" and "png".  
  18.    private static final String IMAGE_FORMAT = "jpg";  
  19.    /**  
  20.     * Read an image file from disk and return it as an image. This method  
  21.     * can read JPG and PNG file formats. In case of any problem (e.g the file  
  22.     * does not exist, is in an undecodable format, or any other read error)  
  23.     * this method returns null.  
  24.     *  
  25.     * @param imageFile The image file to be loaded.  
  26.     * @return      The image object or null is it could not be read.  
  27.     */  
  28.    public static OFImage loadImage(File imageFile)  
  29.    {  
  30.      try {  
  31.        BufferedImage image = ImageIO.read(imageFile);  
  32.        if(image == null || (image.getWidth(null) < 0)) {  
  33.          // we could not load the image - probably invalid file format  
  34.          return null;  
  35.        }  
  36.        return new OFImage(image);  
  37.      }  
  38.      catch(IOException exc) {  
  39.        return null;  
  40.      }  
  41.    }  
  42.    /**  
  43.     * Write an image file to disk. The file format is JPG. In case of any  
  44.     * problem the method just silently returns.  
  45.     *  
  46.     * @param image The image to be saved.  
  47.     * @param file  The file to save to.  
  48.     */  
  49.    public static void saveImage(OFImage image, File file)  
  50.    {  
  51.      try {  
  52.        ImageIO.write(image, IMAGE_FORMAT, file);  
  53.      }  
  54.      catch(IOException exc) {  
  55.        return;  
  56.      }  
  57.    }  
  58.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top