source: other-projects/GlamED/trunk/src/org/honours/agents/GenerateThumbnails.java@ 26637

Last change on this file since 26637 was 26637, checked in by davidb, 11 years ago

Adding in agent to generate thumbnails for collection space feature.

File size: 4.0 KB
Line 
1package org.honours.agents;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileOutputStream;
7import java.io.FileWriter;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.OutputStream;
11import java.util.ArrayList;
12import java.util.List;
13
14import net.coobird.thumbnailator.Thumbnails;
15import net.coobird.thumbnailator.name.Rename;
16
17import org.expeditee.gui.Frame;
18import org.honours.CollectionSpace.CollectionSpace;
19import org.honours.collection.Collection;
20import org.honours.collection.CollectionItem;
21
22public class GenerateThumbnails {
23 private Collection _collection;
24 private String _newDirectory;
25
26 private List<File> _imageFiles = new ArrayList<File>();
27 private List<File> _originalFiles = new ArrayList<File>();
28
29 public Frame process(Frame frame) {
30 try{
31 //obtain collection the frame belongs to.
32 _collection = Collection.findCollection(frame);
33
34 if(_collection == null)
35 return null;
36
37 _newDirectory = "expeditee" + File.separator + "thumbnails-" + _collection.getName();
38
39 new File(_newDirectory).mkdir();
40
41 FileWriter fw = new FileWriter(_newDirectory + File.separator + "thumbnails.inf");
42 BufferedWriter bw = new BufferedWriter(fw);
43
44 for(int i = 0; i < _collection.getItems().size(); i++){
45 CollectionItem ci = _collection.getItems().get(i);
46 String thumbnailFileName = getThumbnailFileName(ci.getAssociatedFile());
47 System.err.println("NEED TO FIND OUT thumbnail file name: " + thumbnailFileName + " *********");
48 moveImageToThumbnailDirectory(ci,thumbnailFileName,i+1,bw);
49 }
50
51 resize();
52 cleanThumbnailsFolder();
53
54 bw.close();
55 }catch(Exception ex){
56 ex.printStackTrace();
57 }
58
59 return null;
60 }
61
62
63 private void resize(){
64 try{
65 Thumbnails.of(new File(_newDirectory).listFiles())
66 /*.size(CollectionSpace._imageSize,CollectionSpace._imageSize)*/
67 .outputFormat("jpg")
68 .forceSize(CollectionSpace._imageSize,CollectionSpace._imageSize)
69 .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
70 }catch(Exception e){
71 e.printStackTrace();
72 }
73 }
74
75 /**
76 * Once thumbnails are generated, remove original larger images.
77 */
78 private void cleanThumbnailsFolder(){
79
80 for(int i = 0; i < _originalFiles.size(); i++){
81
82 }
83 }
84
85 private String getThumbnailFileName(String associatedFile){
86
87 if(associatedFile.endsWith(".JPG") || associatedFile.endsWith(".jpg") ||
88 associatedFile.endsWith(".JPEG") || associatedFile.endsWith(".jpeg") ||
89 associatedFile.endsWith(".PNG") || associatedFile.endsWith(".png")){
90 return associatedFile;
91 }else{
92 return "images" + File.separator + "no-image-available.png";
93 }
94 }
95
96 private void moveImageToThumbnailDirectory(CollectionItem ci, String thumbnail, int iter,BufferedWriter bw)
97 {
98
99 File orig = new File(thumbnail);
100 //TODO: Add orig to a list of original files for removing later.
101 _originalFiles.add(orig);
102
103 File replace = new File(_newDirectory + File.separator + _collection.getName() +
104 iter + ".png");
105 replace = new File(_newDirectory + File.separator + _collection.getName() + iter/* + ".png"*/);
106 if(!replace.exists()){
107 try{
108 replace.createNewFile();
109 }catch(IOException e){
110 e.printStackTrace();
111 }
112 }
113
114 InputStream in = null;
115 OutputStream out = null;
116
117 try{
118 in = new FileInputStream(orig);
119 out = new FileOutputStream(replace);
120
121 //transfer bytes from in to out.
122 byte[] buf = new byte[1024];
123 int len;
124 while((len = in.read(buf)) > 0){
125 out.write(buf,0,len);
126 }
127 in.close();
128 out.close();
129
130 System.err.println("***** replace... " + replace.getPath());
131
132 //bw.write(replace.getPath() + "," + ci.getFrame().getName() + "\n");
133 bw.write(_newDirectory + File.separator + "thumbnail." + _collection.getName() + iter + ".JPG" + "," + ci.getFrame().getName() + "\n");
134 _imageFiles.add(replace);
135
136 }catch(Exception e){
137 e.printStackTrace();
138 }
139 }
140}
Note: See TracBrowser for help on using the repository browser.