1 | <html>
|
---|
2 | <head>
|
---|
3 | </head>
|
---|
4 | <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
|
---|
5 |
|
---|
6 | <%!
|
---|
7 | public int GCD(int a, int b) {
|
---|
8 | if (b==0) return a;
|
---|
9 | return GCD(b,a%b);
|
---|
10 | }
|
---|
11 |
|
---|
12 | ArrayList<File> listFilesForFolder(final File folder, String matchExt) {
|
---|
13 | ArrayList<File> fileList = new ArrayList<File>();
|
---|
14 |
|
---|
15 | for (final File fileEntry : folder.listFiles()) {
|
---|
16 | if (fileEntry.isDirectory()) {
|
---|
17 | listFilesForFolder(fileEntry,matchExt);
|
---|
18 | } else {
|
---|
19 | if (fileEntry.getName().toLowerCase().endsWith(matchExt)) {
|
---|
20 | fileList.add(fileEntry);
|
---|
21 | }
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | return fileList;
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap)
|
---|
31 | {
|
---|
32 |
|
---|
33 | // Convert Map to List
|
---|
34 | List<Map.Entry<String, Integer>> list
|
---|
35 | = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
|
---|
36 |
|
---|
37 | // Sort list with comparator, to compare the Map values
|
---|
38 | Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
|
---|
39 | public int compare(Map.Entry<String, Integer> o1,
|
---|
40 | Map.Entry<String, Integer> o2) {
|
---|
41 | return (o2.getValue()).compareTo(o1.getValue());
|
---|
42 | }
|
---|
43 | });
|
---|
44 |
|
---|
45 | // Convert sorted map back to a Map
|
---|
46 | Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
|
---|
47 | for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
|
---|
48 | Map.Entry<String, Integer> entry = it.next();
|
---|
49 | sortedMap.put(entry.getKey(), entry.getValue());
|
---|
50 | }
|
---|
51 |
|
---|
52 | return sortedMap;
|
---|
53 | }
|
---|
54 | /*
|
---|
55 | public static void printMap(Map<String, Integer> map)
|
---|
56 | {
|
---|
57 | for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
---|
58 | out.println("[Key] : " + entry.getKey()
|
---|
59 | + " [Value] : " + entry.getValue() + "br />");
|
---|
60 | }
|
---|
61 | }
|
---|
62 | */
|
---|
63 |
|
---|
64 |
|
---|
65 | %>
|
---|
66 |
|
---|
67 | <%
|
---|
68 |
|
---|
69 | String docBase = getServletContext().getRealPath("/");
|
---|
70 | File similarityDir = new File(docBase,"similarity-2d");
|
---|
71 | File imagesDir = new File(similarityDir,"Images");
|
---|
72 |
|
---|
73 | ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
|
---|
74 |
|
---|
75 | File imageUrlBase = new File("Images");
|
---|
76 |
|
---|
77 | String action = request.getParameter("action");
|
---|
78 |
|
---|
79 | if (action.equals("ratios")) {
|
---|
80 |
|
---|
81 | Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
|
---|
82 | Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
|
---|
83 |
|
---|
84 | for (final File fileEntry : imageFileList) {
|
---|
85 |
|
---|
86 | //out.println("flag " + fileEntry.getName());
|
---|
87 |
|
---|
88 | BufferedImage bimg = ImageIO.read(fileEntry);
|
---|
89 | int width = bimg.getWidth();
|
---|
90 | int height = bimg.getHeight();
|
---|
91 | int gcd = GCD(width,height);
|
---|
92 |
|
---|
93 | int gcd_width = width / gcd;
|
---|
94 | int gcd_height = height / gcd;
|
---|
95 | String ratioStr = gcd_width + ":" + gcd_height;
|
---|
96 |
|
---|
97 | if (!ratioFreq.containsKey(ratioStr)) {
|
---|
98 | ratioFreq.put(ratioStr,1);
|
---|
99 | ratioList.put(ratioStr,new ArrayList<File>());
|
---|
100 | }
|
---|
101 | else {
|
---|
102 | ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
|
---|
103 | ratioList.get(ratioStr).add(fileEntry);
|
---|
104 | }
|
---|
105 |
|
---|
106 | //double ratio = (double)width/(double)height;
|
---|
107 |
|
---|
108 | //out.println(width + " x " + height + " ratio: " + ratioStr);
|
---|
109 | //out.println("<br/>");
|
---|
110 |
|
---|
111 |
|
---|
112 | }
|
---|
113 |
|
---|
114 | Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
|
---|
115 | //printMap(sortedRatioFreq);
|
---|
116 | for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
|
---|
117 | out.println("[Key] : " + entry.getKey()
|
---|
118 | + " [Value] : " + entry.getValue() + "<br />");
|
---|
119 | }
|
---|
120 |
|
---|
121 | /*
|
---|
122 | // Print out frequency counts
|
---|
123 | Iterator<String> keyIterator = ratioFreq.keySet().iterator();
|
---|
124 |
|
---|
125 | while (keyIterator.hasNext()){
|
---|
126 | String key = keyIterator.next();
|
---|
127 | Integer freq = ratioFreq.get(key);
|
---|
128 | out.println(key + ": " + freq + "<br />");
|
---|
129 | }
|
---|
130 | */
|
---|
131 |
|
---|
132 | }
|
---|
133 | else {
|
---|
134 |
|
---|
135 | for (final File fileEntry : imageFileList) {
|
---|
136 | File flagImageUrl = new File(imageUrlBase,fileEntry.getName());
|
---|
137 |
|
---|
138 | out.println("<img src=\"" + flagImageUrl.getPath() + "\">");
|
---|
139 |
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | %>
|
---|
145 |
|
---|
146 | <body>
|
---|
147 | </body>
|
---|
148 |
|
---|
149 | </html> |
---|