Advertisement
Guest User

Untitled

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