commit | author | age
|
ef2fb2
|
1 |
/* |
U |
2 |
neon-image - Image extensions to Neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.neon.image; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.FileInputStream; |
|
22 |
import java.io.FileNotFoundException; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.InputStream; |
|
25 |
import java.io.OutputStream; |
|
26 |
import java.io.UnsupportedEncodingException; |
|
27 |
import java.net.URLDecoder; |
|
28 |
import java.nio.file.Files; |
|
29 |
import java.nio.file.Path; |
|
30 |
import java.util.Base64; |
|
31 |
import net.coobird.thumbnailator.Thumbnails; |
|
32 |
|
|
33 |
/** |
|
34 |
* Eine Klasse zum Herstellen verkleinerter sowie Base64-enkodierter |
|
35 |
* Fassungen eines Originalbildes |
|
36 |
* |
|
37 |
* @author Ulrich Hilger |
|
38 |
* @version 1, 13.06.2021 |
|
39 |
*/ |
|
40 |
public class ImageWorker { |
|
41 |
|
|
42 |
/** |
|
43 |
* Diese String-Konstanten noetigenfalls in eine Konfigurationsdatei auslagern |
|
44 |
*/ |
f7b9ad
|
45 |
private final String TN = "_tn"; // 120 |
U |
46 |
private final String KL = "_kl"; // 240 |
|
47 |
private final String SM = "_sm"; // 500 |
|
48 |
private final String MT = "_mt"; // 700 |
|
49 |
private final String GR = "_gr"; // 1200 |
ef2fb2
|
50 |
|
f7b9ad
|
51 |
private final String B64 = "_b64"; // Base64-Encoded |
ef2fb2
|
52 |
|
f7b9ad
|
53 |
//public static final String JPG = ".jpg"; |
U |
54 |
//public static final String JPEG = ".jpeg"; |
|
55 |
//public static final String PNG = ".png"; |
ef2fb2
|
56 |
|
U |
57 |
public void createImages(String baseDir, String fName, String uriStr) throws IOException { |
f7b9ad
|
58 |
//if(uriStr.endsWith(ImageWorker.JPG) || uriStr.endsWith(ImageWorker.JPEG) || uriStr.endsWith(ImageWorker.PNG)) { |
ef2fb2
|
59 |
File dir = new File(baseDir); |
U |
60 |
File imgfile = new File(dir, fName); |
f7b9ad
|
61 |
//ImageWorker actor = new ImageWorker(); |
U |
62 |
if(uriStr.contains(TN)) { |
|
63 |
createImage(dir, fName, TN, 120, imgfile); |
|
64 |
} else if(uriStr.contains(KL)) { |
|
65 |
createImage(dir, fName, KL, 240, imgfile); |
|
66 |
} else if(uriStr.contains(SM)) { |
|
67 |
createImage(dir, fName, SM, 500, imgfile); |
|
68 |
} else if(uriStr.contains(MT)) { |
|
69 |
createImage(dir, fName, MT, 700, imgfile); |
|
70 |
} else if(uriStr.contains(GR)) { |
|
71 |
createImage(dir, fName, GR, 1200, imgfile); |
|
72 |
} else if(uriStr.contains(B64)) { |
ef2fb2
|
73 |
File b64File = imgfile; |
f7b9ad
|
74 |
String fromName = fName.replace(B64, ""); |
ef2fb2
|
75 |
File fromfile = new File(dir, fromName); |
f7b9ad
|
76 |
b64Image(fromfile, b64File); |
ef2fb2
|
77 |
} |
f7b9ad
|
78 |
//} |
ef2fb2
|
79 |
} |
U |
80 |
|
|
81 |
/** |
|
82 |
* Wenn b64 gewuenscht ist verweist tnfile auf dateiname_tn_b64.png Wenn b64 |
|
83 |
* nicht gewuenscht ist verweist tnfile auf dateiname_tn.png |
|
84 |
* |
|
85 |
* (oder _kl oder _sm usw. anstelle von _tn) |
|
86 |
* |
|
87 |
* @param dir |
|
88 |
* @param relname |
|
89 |
* @param indicator |
|
90 |
* @param gr |
|
91 |
* @param tnfile |
|
92 |
* @throws UnsupportedEncodingException |
|
93 |
* @throws IOException |
|
94 |
*/ |
|
95 |
public void createImage(File dir, String relname, String indicator, int gr, File tnfile) |
|
96 |
throws UnsupportedEncodingException, IOException { |
|
97 |
File b64File = null; |
|
98 |
if (relname.contains(B64)) { |
|
99 |
b64File = tnfile; |
|
100 |
//String tnfilename = tnfile.getName(); |
|
101 |
relname = relname.replace(B64, ""); |
|
102 |
tnfile = new File(tnfile.getParentFile(), relname); |
|
103 |
} |
|
104 |
// _tn, usw. entfernen |
|
105 |
relname = relname.replace(indicator, ""); |
|
106 |
// die Original-Bilddatei |
|
107 |
File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8")); |
|
108 |
//logger.fine("imgfile: " + imgfile + ", tnFile: " + tnfile); |
|
109 |
// Bild vom Original verkleinern |
|
110 |
if (imgfile.exists() && !tnfile.exists()) { |
|
111 |
|
|
112 |
Thumbnails.of(imgfile) |
|
113 |
.size(gr, gr) |
|
114 |
.keepAspectRatio(true) |
|
115 |
.outputQuality(0.7) |
|
116 |
.toFile(tnfile); |
|
117 |
/* |
|
118 |
BildVerkleinerer bv = new BildVerkleinerer(); |
|
119 |
bv.verkleinern(imgfile, tnfile, gr, gr, (float) 0.7); |
|
120 |
*/ |
|
121 |
} |
|
122 |
b64Image(tnfile, b64File); |
|
123 |
} |
|
124 |
|
|
125 |
public void b64Image(File fromImg, File toFile) throws FileNotFoundException, IOException { |
|
126 |
//logger.fine("fromImg: " + fromImg.getAbsolutePath() + ", toFile: " + toFile.getAbsolutePath()); |
|
127 |
if (toFile != null && !toFile.exists()) { |
|
128 |
Path originalPath = fromImg.toPath(); |
|
129 |
Path targetPath = toFile.toPath(); |
|
130 |
Base64.Encoder mimeEncoder = Base64.getMimeEncoder(); |
|
131 |
try (OutputStream output = Files.newOutputStream(targetPath)) { |
|
132 |
Files.copy(originalPath, mimeEncoder.wrap(output)); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
public void setImgSrc(Datei datei, String ext, File b64File) throws IOException { |
|
139 |
StringBuilder sb = new StringBuilder(); |
|
140 |
sb.append("data:image/"); |
f7b9ad
|
141 |
//if(ext.equalsIgnoreCase(ImageWorker.JPEG) || ext.equalsIgnoreCase(ImageWorker.JPG)) { |
U |
142 |
// sb.append("jpeg"); |
|
143 |
//} else if(ext.equalsIgnoreCase(ImageWorker.PNG)) { |
|
144 |
// sb.append("png"); |
|
145 |
//} |
|
146 |
sb.append(ext.toLowerCase()); |
ef2fb2
|
147 |
sb.append(";base64,"); |
U |
148 |
byte[] buf = new byte[4096]; |
|
149 |
InputStream is = new FileInputStream(b64File); |
|
150 |
int bytesRead = is.read(buf); |
|
151 |
while(bytesRead > -1) { |
|
152 |
String str = new String(buf, 0, bytesRead); |
|
153 |
sb.append(str); |
|
154 |
bytesRead = is.read(buf); |
|
155 |
} |
|
156 |
is.close(); |
|
157 |
datei.setImgSrc(sb.toString()); |
|
158 |
} |
|
159 |
|
|
160 |
} |