-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtilities.java
More file actions
58 lines (52 loc) · 2.16 KB
/
ImageUtilities.java
File metadata and controls
58 lines (52 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
public class ImageUtilities {
public static final File SOURCE_DIR = new File("./SourceFiles");
public static final File DESTINATION_DIR = new File("./ResultFiles");
public static final String IMAGE_TYPE = "png";
public static final FilenameFilter FILTER = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.matches(".*" + IMAGE_TYPE + "$");
}
};
// Reads an image from the given file. The file is typically located
// in the source directory.
public static BufferedImage readImage(File file) {
try {
return ImageIO.read(file);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
// Writes an image to a file in the destination directory. Before writing
// the file, the file name has the tag appeneded to it after the file name
// and before the file extention.
public static void writeImage(BufferedImage image, File file, String tag) {
if(!tag.matches("^\\w+$")) {
throw new RuntimeException("Tag must be non-empty alphanumeric: " + tag);
}
if(!DESTINATION_DIR.isDirectory()) {
throw new RuntimeException("Cannot find destination directory");
}
try {
String fn = file.getName();
String newFn = fn.substring(0,fn.length()-(IMAGE_TYPE.length()+1)) + "_" + tag + "." + IMAGE_TYPE;
File destinationFile = new File(DESTINATION_DIR, newFn);
ImageIO.write(image, IMAGE_TYPE, destinationFile);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
// Returns an array of file objects for the image files located in the
// source directory.
public static File[] getImageFiles() {
if(!SOURCE_DIR.isDirectory()) {
throw new RuntimeException("Cannot find source directory");
}
return SOURCE_DIR.listFiles(FILTER);
}
}