sitchof

Untitled

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