document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.image.*;
  2. import javax.imageio.*;
  3. import java.io.*;
  4.  
  5.  
  6. public class ImageFileManager
  7. {
  8. // A constant for the image format that this writer uses for writing.
  9. // Available formats are "jpg" and "png".
  10. private static final String IMAGE_FORMAT = "jpg";
  11.  
  12. /**
  13. * Read an image file from disk and return it as an image. This method
  14. * can read JPG and PNG file formats. In case of any problem (e.g the file
  15. * does not exist, is in an undecodable format, or any other read error)
  16. * this method returns null.
  17. *
  18. * @param imageFile The image file to be loaded.
  19. * @return The image object or null is it could not be read.
  20. */
  21. public static OFImage loadImage(File imageFile)
  22. {
  23. try {
  24. BufferedImage image = ImageIO.read(imageFile);
  25. if(image == null || (image.getWidth(null) < 0)) {
  26. // we could not load the image - probably invalid file format
  27. return null;
  28. }
  29. return new OFImage(image);
  30. }
  31. catch(IOException exc) {
  32. return null;
  33. }
  34. }
  35.  
  36. /**
  37. * Write an image file to disk. The file format is JPG. In case of any
  38. * problem the method just silently returns.
  39. *
  40. * @param image The image to be saved.
  41. * @param file The file to save to.
  42. */
  43. public static void saveImage(OFImage image, File file)
  44. {
  45. try {
  46. ImageIO.write(image, IMAGE_FORMAT, file);
  47. }
  48. catch(IOException exc) {
  49. return;
  50. }
  51. }
  52. }
');