source: other-projects/nz-flag-design/trunk/similarity-2d/flag-aspect-ratios-json.jsp@ 29968

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

Developed from index.jsp, tailored to produce JSON data encoding which flags have which aspect-ratio

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1<%@ page contentType="text/html; charset=UTF-8" %>
2 <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
3 <%@include file="flag-functions.jsp" %>
4 <%
5 String docBase = getServletContext().getRealPath("/");
6 File similarityDir = new File(docBase,"similarity-2d");
7
8 String imagesStr = (request.getParameter("imagesDir")!=null) ? request.getParameter("imagesDir") : "import";
9 File imagesDir = new File(similarityDir,imagesStr);
10
11 File aspectRatioFile = new File(similarityDir,"flag-aspect-ratio.json");
12
13 ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
14
15 File imageUrlBase = new File(imagesStr);
16
17 if (!aspectRatioFile.exists()) {
18 Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
19 Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
20
21 for (final File fileEntry : imageFileList) {
22
23 BufferedImage bimg = ImageIO.read(fileEntry);
24 int width = bimg.getWidth();
25 int height = bimg.getHeight();
26 int gcd = GCD(width,height);
27
28 int gcd_width = width / gcd;
29 int gcd_height = height / gcd;
30
31 if ((gcd_width>50) || (gcd_height>50)) {
32 int rough_width = (gcd_width+49)/50;
33 int rough_height = (gcd_height+49)/50;
34
35 int rough_gcd = GCD(rough_width,rough_height);
36
37 gcd_width = rough_width / rough_gcd;
38 gcd_height = rough_height / rough_gcd;
39 }
40
41 String ratioStr = gcd_width + ":" + gcd_height;
42
43 if (!ratioFreq.containsKey(ratioStr)) {
44 ratioFreq.put(ratioStr,1);
45 ratioList.put(ratioStr,new ArrayList<File>());
46 ratioList.get(ratioStr).add(fileEntry);
47 }
48 else {
49 ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
50 ratioList.get(ratioStr).add(fileEntry);
51 }
52 }
53 try {
54
55 FileWriter foutFileWriter = new FileWriter(aspectRatioFile);
56 PrintWriter fout = new PrintWriter(foutFileWriter);
57
58 fout.println("{");
59
60 Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
61 int c = 0;
62
63 for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
64
65 String ratio_key = entry.getKey();
66 Integer ratio_freq = entry.getValue();
67
68 if (ratio_freq>1) {
69 if (c>0) {
70 fout.print(", ");
71 }
72 fout.println("\"" + ratio_key + "\" : ");
73 fout.println(" { \"freq\" : " + entry.getValue());
74
75 ArrayList<File> flag_list = ratioList.get(ratio_key);
76 int flag_list_len = flag_list.size();
77
78 fout.print(" ,\"flags\" : [");
79 for (int i=0; i<flag_list_len; i++) {
80 if (i>0) {
81 fout.print(", ");
82 }
83
84 String flag_file_str = imageUrlBase + "/" + flag_list.get(i).getName();
85
86 fout.print("\""+flag_file_str+"\"");
87 }
88 fout.println("]");
89 fout.println(" }");
90
91 c++;
92 }
93 }
94 fout.println("}");
95
96 fout.close();
97
98
99 }
100 catch (IOException e) {
101 e.printStackTrace();
102 }
103
104 }
105 try {
106 FileReader fr = new FileReader(aspectRatioFile);
107 BufferedReader bfr = new BufferedReader(fr);
108
109 String line;
110 while ( ( line = bfr.readLine( ) ) != null ) {
111 out.print(line);
112 }
113 bfr.close();
114 }
115 catch (IOException e) {
116 e.printStackTrace();
117 }
118 %>
Note: See TracBrowser for help on using the repository browser.