Created
December 10, 2018 09:24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.*; | |
import javax.imageio.*; | |
import java.io.*; | |
/** | |
* Ujian Akhir Semester | |
* Kelas PBO B | |
* @author Hendra Ramadani (05111740000055) | |
* 10 December 2018 | |
*/ | |
public class ImageFileManager | |
{ | |
// A constant for the image format that this writer uses for writing. | |
// Available formats are "jpg" and "png". | |
private static final String IMAGE_FORMAT = "jpg"; | |
/** | |
* Read an image file from disk and return it as an image. This method | |
* can read JPG and PNG file formats. In case of any problem (e.g the file | |
* does not exist, is in an undecodable format, or any other read error) | |
* this method returns null. | |
* | |
* @param imageFile The image file to be loaded. | |
* @return The image object or null is it could not be read. | |
*/ | |
public static OFImage loadImage(File imageFile) | |
{ | |
try { | |
BufferedImage image = ImageIO.read(imageFile); | |
if(image == null || (image.getWidth(null) < 0)) { | |
// we could not load the image - probably invalid file format | |
return null; | |
} | |
return new OFImage(image); | |
} | |
catch(IOException exc) { | |
return null; | |
} | |
} | |
/** | |
* Write an image file to disk. The file format is JPG. In case of any | |
* problem the method just silently returns. | |
* | |
* @param image The image to be saved. | |
* @param file The file to save to. | |
*/ | |
public static void saveImage(OFImage image, File file) | |
{ | |
try { | |
ImageIO.write(image, IMAGE_FORMAT, file); | |
} | |
catch(IOException exc) { | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment