source: other-projects/nz-flag-design/trunk/similarity-2d/index.jsp@ 29765

Last change on this file since 29765 was 29765, checked in by davidb, 9 years ago

Tidy up on character encoding of JSP page. Done to avoid warning in IE

File size: 5.1 KB
Line 
1<%@ page contentType="text/html; charset=UTF-8" %>
2<!DOCTYPE html>
3<html>
4 <head>
5 <meta charset="utf-8"/>
6 </head>
7
8
9 <body>
10
11 <div style="display: none;">
12
13 <script>
14 // Set up a fallback position in case this page is accessed outside of a JSP context
15 // (i.e., the JSP blocks aren't executed)
16 var img_list = [ "Images/nz.gif" ];
17 </script>
18
19 <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
20
21 <%!
22 public int GCD(int a, int b) {
23 if (b==0) return a;
24 return GCD(b,a%b);
25 }
26
27 ArrayList<File> listFilesForFolder(final File folder, String matchExt) {
28 ArrayList<File> fileList = new ArrayList<File>();
29
30 for (final File fileEntry : folder.listFiles()) {
31 if (fileEntry.isDirectory()) {
32 listFilesForFolder(fileEntry,matchExt);
33 } else {
34 if (fileEntry.getName().toLowerCase().endsWith(matchExt)) {
35 fileList.add(fileEntry);
36 }
37 }
38 }
39
40 return fileList;
41 }
42
43
44
45 public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap)
46 {
47
48 // Convert Map to List
49 List<Map.Entry<String, Integer>> list
50 = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
51
52 // Sort list with comparator, to compare the Map values
53 Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
54 public int compare(Map.Entry<String, Integer> o1,
55 Map.Entry<String, Integer> o2) {
56 return (o2.getValue()).compareTo(o1.getValue());
57 }
58 });
59
60 // Convert sorted map back to a Map
61 Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
62 for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
63 Map.Entry<String, Integer> entry = it.next();
64 sortedMap.put(entry.getKey(), entry.getValue());
65 }
66
67 return sortedMap;
68 }
69/*
70 public static void printMap(Map<String, Integer> map)
71 {
72 for (Map.Entry<String, Integer> entry : map.entrySet()) {
73 out.println("[Key] : " + entry.getKey()
74 + " [Value] : " + entry.getValue() + "br />");
75 }
76 }
77 */
78
79
80 %>
81
82
83 <%
84
85 String docBase = getServletContext().getRealPath("/");
86 File similarityDir = new File(docBase,"similarity-2d");
87 File imagesDir = new File(similarityDir,"Images");
88
89 ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
90
91 File imageUrlBase = new File("Images");
92
93 String action = request.getParameter("action");
94
95 if ((action != null) && action.equals("ratios")) {
96
97 Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
98 Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
99
100 for (final File fileEntry : imageFileList) {
101
102 //out.println("flag " + fileEntry.getName());
103
104 BufferedImage bimg = ImageIO.read(fileEntry);
105 int width = bimg.getWidth();
106 int height = bimg.getHeight();
107 int gcd = GCD(width,height);
108
109 int gcd_width = width / gcd;
110 int gcd_height = height / gcd;
111 String ratioStr = gcd_width + ":" + gcd_height;
112
113 if (!ratioFreq.containsKey(ratioStr)) {
114 ratioFreq.put(ratioStr,1);
115 ratioList.put(ratioStr,new ArrayList<File>());
116 }
117 else {
118 ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
119 ratioList.get(ratioStr).add(fileEntry);
120 }
121
122 //double ratio = (double)width/(double)height;
123
124 //out.println(width + " x " + height + " ratio: " + ratioStr);
125 //out.println("<br/>");
126
127
128 }
129
130 Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
131 //printMap(sortedRatioFreq);
132 for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
133 out.println("[Key] : " + entry.getKey()
134 + " [Value] : " + entry.getValue() + "<br />");
135 }
136
137/*
138 // Print out frequency counts
139 Iterator<String> keyIterator = ratioFreq.keySet().iterator();
140
141 while (keyIterator.hasNext()){
142 String key = keyIterator.next();
143 Integer freq = ratioFreq.get(key);
144 out.println(key + ": " + freq + "<br />");
145 }
146*/
147
148 }
149 else {
150 out.println("<script>");
151 out.println("var dynamic_img_list = [ ");
152 boolean first_entry = true;
153 for (final File fileEntry : imageFileList) {
154 String flagImageUrl = imageUrlBase + "/" + fileEntry.getName();
155
156 //out.println("<img src=\"" + flagImageUrl + "\">");
157 if (first_entry) {
158 out.println("\"" + flagImageUrl + "\"");
159 first_entry = false;
160 }
161 else {
162 out.println(",\"" + flagImageUrl + "\"");
163 }
164 }
165 out.println("];");
166
167 out.println("img_list = dynamic_img_list;");
168
169 out.println("</script>");
170 }
171
172
173 %>
174
175 </div>
176
177 <script>
178
179 var i;
180 for (i=0; i<img_list.length; i++) {
181 document.write("<img src=\"" + img_list[i] + "\" style=\"padding: 4px;\">\n");
182 }
183
184 </script>
185
186 </body>
187
188</html>
Note: See TracBrowser for help on using the repository browser.