[29765] | 1 | <%@ page contentType="text/html; charset=UTF-8" %>
|
---|
[29563] | 2 | <!DOCTYPE html>
|
---|
[29533] | 3 | <html>
|
---|
[29562] | 4 | <head>
|
---|
[29765] | 5 | <meta charset="utf-8"/>
|
---|
[29562] | 6 | </head>
|
---|
[29561] | 7 |
|
---|
[29562] | 8 |
|
---|
| 9 | <body>
|
---|
| 10 |
|
---|
| 11 | <div style="display: none;">
|
---|
| 12 |
|
---|
[29561] | 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)
|
---|
[29784] | 16 | var img_list = [ "import/nz.gif" ];
|
---|
[29561] | 17 | </script>
|
---|
| 18 |
|
---|
[29533] | 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 |
|
---|
[29561] | 82 |
|
---|
[29533] | 83 | <%
|
---|
| 84 |
|
---|
| 85 | String docBase = getServletContext().getRealPath("/");
|
---|
| 86 | File similarityDir = new File(docBase,"similarity-2d");
|
---|
| 87 |
|
---|
[29784] | 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 |
|
---|
[29533] | 93 | ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
|
---|
| 94 |
|
---|
[29784] | 95 | File imageUrlBase = new File(imagesStr);
|
---|
[29533] | 96 |
|
---|
| 97 | String action = request.getParameter("action");
|
---|
| 98 |
|
---|
[29537] | 99 | if ((action != null) && action.equals("ratios")) {
|
---|
[29533] | 100 |
|
---|
[29784] | 101 | if (!aspectRatioFile.exists()) {
|
---|
| 102 | Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
|
---|
| 103 | Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
|
---|
[29533] | 104 |
|
---|
[29784] | 105 | for (final File fileEntry : imageFileList) {
|
---|
| 106 |
|
---|
| 107 | //out.println("flag " + fileEntry.getName());
|
---|
[29533] | 108 |
|
---|
[29784] | 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;
|
---|
[29533] | 116 |
|
---|
[29784] | 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 | }
|
---|
[29533] | 126 |
|
---|
[29784] | 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/>");
|
---|
[29533] | 142 |
|
---|
| 143 |
|
---|
[29784] | 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 />");
|
---|
[29533] | 162 |
|
---|
[29784] | 163 | String ratio_key = entry.getKey();
|
---|
| 164 | Integer ratio_freq = entry.getValue();
|
---|
[29533] | 165 |
|
---|
[29784] | 166 | if (ratio_freq>1) {
|
---|
| 167 | if (c>0) {
|
---|
| 168 | fout.print(", ");
|
---|
| 169 | }
|
---|
| 170 | fout.println("\"" + ratio_key + "\" : ");
|
---|
| 171 | fout.println(" { \"freq\" : " + entry.getValue());
|
---|
[29533] | 172 |
|
---|
[29784] | 173 | ArrayList<File> flag_list = ratioList.get(ratio_key);
|
---|
| 174 | int flag_list_len = flag_list.size();
|
---|
[29533] | 175 |
|
---|
[29784] | 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();
|
---|
[29533] | 183 |
|
---|
[29784] | 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 |
|
---|
[29533] | 198 | /*
|
---|
[29784] | 199 | // Print out frequency counts
|
---|
| 200 | Iterator<String> keyIterator = ratioFreq.keySet().iterator();
|
---|
[29533] | 201 |
|
---|
[29784] | 202 | while (keyIterator.hasNext()){
|
---|
| 203 | String key = keyIterator.next();
|
---|
| 204 | Integer freq = ratioFreq.get(key);
|
---|
| 205 | out.println(key + ": " + freq + "<br />");
|
---|
| 206 | }
|
---|
[29533] | 207 | */
|
---|
| 208 |
|
---|
[29784] | 209 | }
|
---|
| 210 | catch (IOException e) {
|
---|
| 211 | e.printStackTrace();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | }
|
---|
[29533] | 215 | }
|
---|
| 216 | else {
|
---|
[29561] | 217 | out.println("<script>");
|
---|
| 218 | out.println("var dynamic_img_list = [ ");
|
---|
| 219 | boolean first_entry = true;
|
---|
[29533] | 220 | for (final File fileEntry : imageFileList) {
|
---|
[29537] | 221 | String flagImageUrl = imageUrlBase + "/" + fileEntry.getName();
|
---|
[29533] | 222 |
|
---|
[29561] | 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("];");
|
---|
[29533] | 233 |
|
---|
[29561] | 234 | out.println("img_list = dynamic_img_list;");
|
---|
| 235 |
|
---|
| 236 | out.println("</script>");
|
---|
[29533] | 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | %>
|
---|
| 241 |
|
---|
[29562] | 242 | </div>
|
---|
[29561] | 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 |
|
---|
[29533] | 253 | </body>
|
---|
| 254 |
|
---|
[29537] | 255 | </html>
|
---|