/*
|
http-image - Image extensions to jdk.httpserver
|
Copyright (C) 2021 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.httpserver.image;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLDecoder;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.util.Base64;
|
import java.util.logging.Logger;
|
import net.coobird.thumbnailator.Thumbnails;
|
|
/**
|
* Eine Klasse zum Herstellen verkleinerter sowie Base64-enkodierter
|
* Fassungen eines Originalbildes
|
*
|
* @author Ulrich Hilger
|
* @version 1, 13.06.2021
|
*/
|
public class ImageActor {
|
|
private static final Logger logger = Logger.getLogger(ImageActor.class.getName());
|
|
/**
|
* Diese String-Konstanten noetigenfalls in eine Konfigurationsdatei auslagern
|
*/
|
public static final String TN = "_tn"; // 120
|
public static final String KL = "_kl"; // 240
|
public static final String SM = "_sm"; // 500
|
public static final String MT = "_mt"; // 700
|
public static final String GR = "_gr"; // 1200
|
|
public static final String B64 = "_b64"; // Base64-Encoded
|
|
public static final String JPG = ".jpg";
|
public static final String JPEG = ".jpeg";
|
public static final String PNG = ".png";
|
|
/**
|
* Wenn b64 gewuenscht ist verweist tnfile auf dateiname_tn_b64.png Wenn b64
|
* nicht gewuenscht ist verweist tnfile auf dateiname_tn.png
|
*
|
* (oder _kl oder _sm usw. anstelle von _tn)
|
*
|
* @param dir
|
* @param relname
|
* @param indicator
|
* @param gr
|
* @param tnfile
|
* @throws UnsupportedEncodingException
|
* @throws IOException
|
*/
|
public void createImage(File dir, String relname, String indicator, int gr, File tnfile)
|
throws UnsupportedEncodingException, IOException {
|
File b64File = null;
|
if (relname.contains(B64)) {
|
b64File = tnfile;
|
//String tnfilename = tnfile.getName();
|
relname = relname.replace(B64, "");
|
tnfile = new File(tnfile.getParentFile(), relname);
|
}
|
// _tn, usw. entfernen
|
relname = relname.replace(indicator, "");
|
// die Original-Bilddatei
|
File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8"));
|
//logger.fine("imgfile: " + imgfile + ", tnFile: " + tnfile);
|
// Bild vom Original verkleinern
|
if (imgfile.exists() && !tnfile.exists()) {
|
|
Thumbnails.of(imgfile)
|
.size(gr, gr)
|
.keepAspectRatio(true)
|
.outputQuality(0.7)
|
.toFile(tnfile);
|
/*
|
BildVerkleinerer bv = new BildVerkleinerer();
|
bv.verkleinern(imgfile, tnfile, gr, gr, (float) 0.7);
|
*/
|
}
|
b64Image(tnfile, b64File);
|
}
|
|
public void b64Image(File fromImg, File toFile) throws FileNotFoundException, IOException {
|
//logger.fine("fromImg: " + fromImg.getAbsolutePath() + ", toFile: " + toFile.getAbsolutePath());
|
if (toFile != null && !toFile.exists()) {
|
Path originalPath = fromImg.toPath();
|
Path targetPath = toFile.toPath();
|
Base64.Encoder mimeEncoder = Base64.getMimeEncoder();
|
try (OutputStream output = Files.newOutputStream(targetPath)) {
|
Files.copy(originalPath, mimeEncoder.wrap(output));
|
}
|
}
|
}
|
|
|
public void setImgSrc(Datei datei, String ext, File b64File) throws IOException {
|
StringBuilder sb = new StringBuilder();
|
sb.append("data:image/");
|
if(ext.equalsIgnoreCase(ImageActor.JPEG) || ext.equalsIgnoreCase(ImageActor.JPG)) {
|
sb.append("jpeg");
|
} else if(ext.equalsIgnoreCase(ImageActor.PNG)) {
|
sb.append("png");
|
}
|
sb.append(";base64,");
|
byte[] buf = new byte[4096];
|
InputStream is = new FileInputStream(b64File);
|
int bytesRead = is.read(buf);
|
while(bytesRead > -1) {
|
String str = new String(buf, 0, bytesRead);
|
sb.append(str);
|
bytesRead = is.read(buf);
|
}
|
is.close();
|
datei.setImgSrc(sb.toString());
|
}
|
|
}
|