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

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

Restructured so a NZ flag is produced if page is requested through a non-JSP active web server

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