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

Last change on this file since 29562 was 29562, checked in by davidb, 6 years ago

Changed so the default is to show the NZ flag if the JSP blocks of code note executed (because the page is rendered on a non-JSP capable web server)

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