Changeset 29784 for other-projects/nz-flag-design/trunk
- Timestamp:
- 2015-03-13T10:25:17+13:00 (9 years ago)
- Location:
- other-projects/nz-flag-design/trunk/similarity-2d
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/nz-flag-design/trunk/similarity-2d/display-flags.jsp
r29766 r29784 45 45 <script> 46 46 var is_jsp_active = false; 47 var img_list = [ " Images/nz.gif" ];47 var img_list = [ "import/nz.gif" ]; 48 48 </script> 49 49 -
other-projects/nz-flag-design/trunk/similarity-2d/flag-functions.jsp
r29621 r29784 1 2 1 <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %> 3 4 2 <%! 5 3 public int GCD(int a, int b) { … … 38 36 } 39 37 40 41 42 38 public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) 43 39 { … … 73 69 } 74 70 */ 75 76 77 71 %> 78 -
other-projects/nz-flag-design/trunk/similarity-2d/index.jsp
r29765 r29784 14 14 // Set up a fallback position in case this page is accessed outside of a JSP context 15 15 // (i.e., the JSP blocks aren't executed) 16 var img_list = [ " Images/nz.gif" ];16 var img_list = [ "import/nz.gif" ]; 17 17 </script> 18 18 … … 85 85 String docBase = getServletContext().getRealPath("/"); 86 86 File similarityDir = new File(docBase,"similarity-2d"); 87 File imagesDir = new File(similarityDir,"Images"); 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"); 88 92 89 93 ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif"); 90 94 91 File imageUrlBase = new File( "Images");95 File imageUrlBase = new File(imagesStr); 92 96 93 97 String action = request.getParameter("action"); … … 95 99 if ((action != null) && action.equals("ratios")) { 96 100 97 Map<String, Integer> ratioFreq = new HashMap<String, Integer>(); 98 Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>(); 99 100 for (final File fileEntry : imageFileList) { 101 102 //out.println("flag " + fileEntry.getName()); 103 104 BufferedImage bimg = ImageIO.read(fileEntry); 105 int width = bimg.getWidth(); 106 int height = bimg.getHeight(); 107 int gcd = GCD(width,height); 108 109 int gcd_width = width / gcd; 110 int gcd_height = height / gcd; 111 String ratioStr = gcd_width + ":" + gcd_height; 112 113 if (!ratioFreq.containsKey(ratioStr)) { 114 ratioFreq.put(ratioStr,1); 115 ratioList.put(ratioStr,new ArrayList<File>()); 116 } 117 else { 118 ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1); 119 ratioList.get(ratioStr).add(fileEntry); 120 } 121 122 //double ratio = (double)width/(double)height; 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 />"); 123 162 124 //out.println(width + " x " + height + " ratio: " + ratioStr); 125 //out.println("<br/>"); 126 127 128 } 129 130 Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq); 131 //printMap(sortedRatioFreq); 132 for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) { 133 out.println("[Key] : " + entry.getKey() 134 + " [Value] : " + entry.getValue() + "<br />"); 135 } 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(); 136 197 137 198 /* 138 // Print out frequency counts 139 Iterator<String> keyIterator = ratioFreq.keySet().iterator(); 140 141 while (keyIterator.hasNext()){ 142 String key = keyIterator.next(); 143 Integer freq = ratioFreq.get(key); 144 out.println(key + ": " + freq + "<br />"); 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 145 214 } 146 */147 148 215 } 149 216 else {
Note:
See TracChangeset
for help on using the changeset viewer.