<%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %> <%! public int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } ArrayList listFilesForFolder(final File folder, String matchExt) { ArrayList fileList = new ArrayList(); for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry,matchExt); } else { if (fileEntry.getName().toLowerCase().endsWith(matchExt)) { fileList.add(fileEntry); } } } return fileList; } public static Map sortByComparator(Map unsortMap) { // Convert Map to List List> list = new LinkedList>(unsortMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator>() { public int compare(Map.Entry o1, Map.Entry o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); // Convert sorted map back to a Map Map sortedMap = new LinkedHashMap(); for (Iterator> it = list.iterator(); it.hasNext();) { Map.Entry entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } /* public static void printMap(Map map) { for (Map.Entry entry : map.entrySet()) { out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue() + "br />"); } } */ %> <% String docBase = getServletContext().getRealPath("/"); File similarityDir = new File(docBase,"similarity-2d"); File imagesDir = new File(similarityDir,"Images"); ArrayList imageFileList = listFilesForFolder(imagesDir,".gif"); File imageUrlBase = new File("Images"); String action = request.getParameter("action"); if ((action != null) && action.equals("ratios")) { Map ratioFreq = new HashMap(); Map> ratioList = new HashMap>(); for (final File fileEntry : imageFileList) { //out.println("flag " + fileEntry.getName()); BufferedImage bimg = ImageIO.read(fileEntry); int width = bimg.getWidth(); int height = bimg.getHeight(); int gcd = GCD(width,height); int gcd_width = width / gcd; int gcd_height = height / gcd; String ratioStr = gcd_width + ":" + gcd_height; if (!ratioFreq.containsKey(ratioStr)) { ratioFreq.put(ratioStr,1); ratioList.put(ratioStr,new ArrayList()); } else { ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1); ratioList.get(ratioStr).add(fileEntry); } //double ratio = (double)width/(double)height; //out.println(width + " x " + height + " ratio: " + ratioStr); //out.println("
"); } Map sortedRatioFreq = sortByComparator(ratioFreq); //printMap(sortedRatioFreq); for (Map.Entry entry : sortedRatioFreq.entrySet()) { out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue() + "
"); } /* // Print out frequency counts Iterator keyIterator = ratioFreq.keySet().iterator(); while (keyIterator.hasNext()){ String key = keyIterator.next(); Integer freq = ratioFreq.get(key); out.println(key + ": " + freq + "
"); } */ } else { out.println(""); } %>