Guest User

Untitled

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