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 = [ "import/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 |
|
---|
88 | String imagesStr = (request.getParameter("imagesDir")!=null) ? request.getParameter("imagesDir") : "import";
|
---|
89 | File imagesDir = new File(similarityDir,imagesStr);
|
---|
90 |
|
---|
91 | File aspectRatioFile = new File(similarityDir,"flag-aspect-ratio.json");
|
---|
92 |
|
---|
93 | ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
|
---|
94 |
|
---|
95 | File imageUrlBase = new File(imagesStr);
|
---|
96 |
|
---|
97 | String action = request.getParameter("action");
|
---|
98 |
|
---|
99 | if ((action != null) && action.equals("ratios")) {
|
---|
100 |
|
---|
101 | if (!aspectRatioFile.exists()) {
|
---|
102 | Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
|
---|
103 | Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
|
---|
104 |
|
---|
105 | for (final File fileEntry : imageFileList) {
|
---|
106 |
|
---|
107 | //out.println("flag " + fileEntry.getName());
|
---|
108 |
|
---|
109 | BufferedImage bimg = ImageIO.read(fileEntry);
|
---|
110 | int width = bimg.getWidth();
|
---|
111 | int height = bimg.getHeight();
|
---|
112 | int gcd = GCD(width,height);
|
---|
113 |
|
---|
114 | int gcd_width = width / gcd;
|
---|
115 | int gcd_height = height / gcd;
|
---|
116 |
|
---|
117 | if ((gcd_width>50) || (gcd_height>50)) {
|
---|
118 | int rough_width = (gcd_width+99)/100;
|
---|
119 | int rough_height = (gcd_height+99)/100;
|
---|
120 |
|
---|
121 | int rough_gcd = GCD(rough_width,rough_height);
|
---|
122 |
|
---|
123 | gcd_width = rough_width / rough_gcd;
|
---|
124 | gcd_height = rough_height / rough_gcd;
|
---|
125 | }
|
---|
126 |
|
---|
127 | String ratioStr = gcd_width + ":" + gcd_height;
|
---|
128 |
|
---|
129 | if (!ratioFreq.containsKey(ratioStr)) {
|
---|
130 | ratioFreq.put(ratioStr,1);
|
---|
131 | ratioList.put(ratioStr,new ArrayList<File>());
|
---|
132 | }
|
---|
133 | else {
|
---|
134 | ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
|
---|
135 | ratioList.get(ratioStr).add(fileEntry);
|
---|
136 | }
|
---|
137 |
|
---|
138 | //double ratio = (double)width/(double)height;
|
---|
139 |
|
---|
140 | //out.println(width + " x " + height + " ratio: " + ratioStr);
|
---|
141 | //out.println("<br/>");
|
---|
142 |
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | try {
|
---|
147 |
|
---|
148 | FileWriter foutFileWriter = new FileWriter(aspectRatioFile);
|
---|
149 | PrintWriter fout = new PrintWriter(foutFileWriter);
|
---|
150 |
|
---|
151 | //out.println("<script>");
|
---|
152 | //out.print("var flagsRatioFreq = ");
|
---|
153 | fout.println("{");
|
---|
154 |
|
---|
155 | Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
|
---|
156 | //printMap(sortedRatioFreq);
|
---|
157 | int c = 0;
|
---|
158 |
|
---|
159 | for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
|
---|
160 | //out.println("[Key] : " + entry.getKey()
|
---|
161 | // + " [Value] : " + entry.getValue() + "<br />");
|
---|
162 |
|
---|
163 | String ratio_key = entry.getKey();
|
---|
164 | Integer ratio_freq = entry.getValue();
|
---|
165 |
|
---|
166 | if (ratio_freq>1) {
|
---|
167 | if (c>0) {
|
---|
168 | fout.print(", ");
|
---|
169 | }
|
---|
170 | fout.println("\"" + ratio_key + "\" : ");
|
---|
171 | fout.println(" { \"freq\" : " + entry.getValue());
|
---|
172 |
|
---|
173 | ArrayList<File> flag_list = ratioList.get(ratio_key);
|
---|
174 | int flag_list_len = flag_list.size();
|
---|
175 |
|
---|
176 | fout.print(" ,\"flags\" : [");
|
---|
177 | for (int i=0; i<flag_list_len; i++) {
|
---|
178 | if (i>0) {
|
---|
179 | fout.print(", ");
|
---|
180 | }
|
---|
181 |
|
---|
182 | String flag_file_str = imageUrlBase + "/" + flag_list.get(i).getName();
|
---|
183 |
|
---|
184 | fout.print("\""+flag_file_str+"\"");
|
---|
185 | }
|
---|
186 | fout.println("]");
|
---|
187 | fout.println(" }");
|
---|
188 |
|
---|
189 | c++;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | fout.println("}");
|
---|
193 |
|
---|
194 | //out.print(";");
|
---|
195 | //out.println("</script>");
|
---|
196 | fout.close();
|
---|
197 |
|
---|
198 | /*
|
---|
199 | // Print out frequency counts
|
---|
200 | Iterator<String> keyIterator = ratioFreq.keySet().iterator();
|
---|
201 |
|
---|
202 | while (keyIterator.hasNext()){
|
---|
203 | String key = keyIterator.next();
|
---|
204 | Integer freq = ratioFreq.get(key);
|
---|
205 | out.println(key + ": " + freq + "<br />");
|
---|
206 | }
|
---|
207 | */
|
---|
208 |
|
---|
209 | }
|
---|
210 | catch (IOException e) {
|
---|
211 | e.printStackTrace();
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|
215 | }
|
---|
216 | else {
|
---|
217 | out.println("<script>");
|
---|
218 | out.println("var dynamic_img_list = [ ");
|
---|
219 | boolean first_entry = true;
|
---|
220 | for (final File fileEntry : imageFileList) {
|
---|
221 | String flagImageUrl = imageUrlBase + "/" + fileEntry.getName();
|
---|
222 |
|
---|
223 | //out.println("<img src=\"" + flagImageUrl + "\">");
|
---|
224 | if (first_entry) {
|
---|
225 | out.println("\"" + flagImageUrl + "\"");
|
---|
226 | first_entry = false;
|
---|
227 | }
|
---|
228 | else {
|
---|
229 | out.println(",\"" + flagImageUrl + "\"");
|
---|
230 | }
|
---|
231 | }
|
---|
232 | out.println("];");
|
---|
233 |
|
---|
234 | out.println("img_list = dynamic_img_list;");
|
---|
235 |
|
---|
236 | out.println("</script>");
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | %>
|
---|
241 |
|
---|
242 | </div>
|
---|
243 |
|
---|
244 | <script>
|
---|
245 |
|
---|
246 | var i;
|
---|
247 | for (i=0; i<img_list.length; i++) {
|
---|
248 | document.write("<img src=\"" + img_list[i] + "\" style=\"padding: 4px;\">\n");
|
---|
249 | }
|
---|
250 |
|
---|
251 | </script>
|
---|
252 |
|
---|
253 | </body>
|
---|
254 |
|
---|
255 | </html>
|
---|