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

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

DOCTYPE added

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