1 | package org.honours.agents;
|
---|
2 |
|
---|
3 | import java.io.BufferedWriter;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.FileWriter;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.io.OutputStream;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import net.coobird.thumbnailator.Thumbnails;
|
---|
15 | import net.coobird.thumbnailator.name.Rename;
|
---|
16 |
|
---|
17 | import org.expeditee.gui.Frame;
|
---|
18 | import org.honours.CollectionSpace.CollectionSpace;
|
---|
19 | import org.honours.collection.Collection;
|
---|
20 | import org.honours.collection.CollectionItem;
|
---|
21 |
|
---|
22 | public 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 | }
|
---|