use of de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter in project goobi-workflow by intranda.
the class MetadatenImagesHelper method scaleFile.
/**
* scale given image file to png using internal embedded content server
*
* @throws ImageManagerException
* @throws IOException
* @throws ImageManipulatorException
*/
public void scaleFile(String inFileName, String outFileName, int inSize, int intRotation) throws ContentLibException, IOException, ImageManipulatorException {
ConfigurationHelper conf = ConfigurationHelper.getInstance();
Path inPath = Paths.get(inFileName);
URI s3URI = null;
try {
s3URI = new URI("s3://" + conf.getS3Bucket() + "/" + S3FileUtils.path2Key(inPath));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
log.error(e);
}
log.trace("start scaleFile");
int tmpSize = inSize;
if (tmpSize < 1) {
tmpSize = 1;
}
log.trace("Scale to " + tmpSize + "%");
if (ConfigurationHelper.getInstance().getContentServerUrl() == null) {
log.trace("api");
ImageManager im = null;
JpegInterpreter pi = null;
try {
im = conf.useS3() ? new ImageManager(s3URI) : new ImageManager(inPath.toUri());
// im = new ImageManager(Paths.get(inFileName).toUri());
log.trace("im");
ImageInterpreter ii = im.getMyInterpreter();
Dimension inputResolution = new Dimension((int) ii.getXResolution(), (int) ii.getYResolution());
log.trace("input resolution: " + inputResolution.width + "x" + inputResolution.height + "dpi");
Dimension outputResolution = new Dimension(144, 144);
log.trace("output resolution: " + outputResolution.width + "x" + outputResolution.height + "dpi");
Dimension dim = new Dimension(tmpSize * outputResolution.width / inputResolution.width, tmpSize * outputResolution.height / inputResolution.height);
log.trace("Absolute scale: " + dim.width + "x" + dim.height + "%");
RenderedImage ri = im.scaleImageByPixel(dim, ImageManager.SCALE_BY_PERCENT, intRotation);
log.trace("ri");
pi = new JpegInterpreter(ri);
log.trace("pi");
pi.setXResolution(outputResolution.width);
log.trace("xres = " + pi.getXResolution());
pi.setYResolution(outputResolution.height);
log.trace("yres = " + pi.getYResolution());
FileOutputStream outputFileStream = new FileOutputStream(outFileName);
log.trace("output");
pi.writeToStream(null, outputFileStream);
log.trace("write stream");
outputFileStream.close();
log.trace("close stream");
} finally {
if (im != null) {
im.close();
}
if (pi != null) {
pi.close();
}
}
} else {
String imageURIString = conf.useS3() ? s3URI.toString() : inFileName;
String cs = conf.getContentServerUrl() + imageURIString + "&scale=" + tmpSize + "&rotate=" + intRotation + "&format=jpg";
cs = cs.replace("\\", "/");
log.trace("url: " + cs);
URL csUrl = new URL(cs);
CloseableHttpClient httpclient = null;
HttpGet method = null;
InputStream istr = null;
OutputStream fos = null;
try {
httpclient = HttpClientBuilder.create().build();
method = new HttpGet(csUrl.toString());
log.trace("get");
Integer contentServerTimeOut = ConfigurationHelper.getInstance().getGoobiContentServerTimeOut();
Builder builder = RequestConfig.custom();
builder.setSocketTimeout(contentServerTimeOut);
RequestConfig rc = builder.build();
method.setConfig(rc);
byte[] response = httpclient.execute(method, HttpClientHelper.byteArrayResponseHandler);
if (response == null) {
log.error("Response stream is null");
return;
}
istr = new ByteArrayInputStream(response);
fos = new FileOutputStream(outFileName);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = istr.read(buf)) > 0) {
fos.write(buf, 0, len);
}
} catch (Exception e) {
log.error("Unable to connect to url " + cs, e);
return;
} finally {
method.releaseConnection();
if (httpclient != null) {
httpclient.close();
}
if (istr != null) {
try {
istr.close();
} catch (IOException e) {
log.error(e);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
log.error(e);
}
}
}
}
}
Aggregations