Search in sources :

Example 1 with JpegInterpreter

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);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ImageInterpreter(de.unigoettingen.sub.commons.contentlib.imagelib.ImageInterpreter) URISyntaxException(java.net.URISyntaxException) JpegInterpreter(de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter) Dimension(java.awt.Dimension) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) TypeNotAllowedAsChildException(ugh.exceptions.TypeNotAllowedAsChildException) DAOException(de.sub.goobi.helper.exceptions.DAOException) SwapException(de.sub.goobi.helper.exceptions.SwapException) ContentLibException(de.unigoettingen.sub.commons.contentlib.exceptions.ContentLibException) TypeNotAllowedForParentException(ugh.exceptions.TypeNotAllowedForParentException) ImageManipulatorException(de.unigoettingen.sub.commons.contentlib.exceptions.ImageManipulatorException) ImageManagerException(de.unigoettingen.sub.commons.contentlib.exceptions.ImageManagerException) IOException(java.io.IOException) MetadataTypeNotAllowedException(ugh.exceptions.MetadataTypeNotAllowedException) InvalidImagesException(de.sub.goobi.helper.exceptions.InvalidImagesException) ContentFileNotLinkedException(ugh.exceptions.ContentFileNotLinkedException) ImageManager(de.unigoettingen.sub.commons.contentlib.imagelib.ImageManager) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ConfigurationHelper(de.sub.goobi.config.ConfigurationHelper) RenderedImage(java.awt.image.RenderedImage)

Aggregations

ConfigurationHelper (de.sub.goobi.config.ConfigurationHelper)1 DAOException (de.sub.goobi.helper.exceptions.DAOException)1 InvalidImagesException (de.sub.goobi.helper.exceptions.InvalidImagesException)1 SwapException (de.sub.goobi.helper.exceptions.SwapException)1 ContentLibException (de.unigoettingen.sub.commons.contentlib.exceptions.ContentLibException)1 ImageManagerException (de.unigoettingen.sub.commons.contentlib.exceptions.ImageManagerException)1 ImageManipulatorException (de.unigoettingen.sub.commons.contentlib.exceptions.ImageManipulatorException)1 ImageInterpreter (de.unigoettingen.sub.commons.contentlib.imagelib.ImageInterpreter)1 ImageManager (de.unigoettingen.sub.commons.contentlib.imagelib.ImageManager)1 JpegInterpreter (de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter)1 Dimension (java.awt.Dimension)1 RenderedImage (java.awt.image.RenderedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1