Search in sources :

Example 6 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class ImageMagickHelper method scaleImage.

@Override
public Size scaleImage(File image, String imgExt, VFSLeaf scaledImage, int maxWidth, int maxHeight) {
    File scaledBaseFile = extractIOFile(scaledImage);
    FinalSize finalSize = generateThumbnail(image, scaledBaseFile, false, maxWidth, maxHeight, false);
    if (finalSize != null) {
        return new Size(finalSize.getWidth(), finalSize.getHeight(), true);
    }
    return null;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) File(java.io.File)

Example 7 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class ImageMagickHelper method executeProcess.

private final FinalSize executeProcess(File thumbnailFile, Process proc) {
    FinalSize rv = null;
    StringBuilder errors = new StringBuilder();
    StringBuilder output = new StringBuilder();
    String line;
    InputStream stderr = proc.getErrorStream();
    InputStreamReader iserr = new InputStreamReader(stderr);
    BufferedReader berr = new BufferedReader(iserr);
    line = null;
    try {
        while ((line = berr.readLine()) != null) {
            errors.append(line);
        }
    } catch (IOException e) {
    // 
    }
    InputStream stdout = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stdout);
    BufferedReader br = new BufferedReader(isr);
    line = null;
    try {
        while ((line = br.readLine()) != null) {
            output.append(line);
        }
    } catch (IOException e) {
    // 
    }
    if (log.isDebug()) {
        log.debug("Error: " + errors.toString());
        log.debug("Output: " + output.toString());
    }
    try {
        int exitValue = proc.waitFor();
        if (exitValue == 0) {
            rv = extractSizeFromOutput(thumbnailFile, output);
            if (rv == null) {
                // sometimes verbose info of convert is in stderr
                rv = extractSizeFromOutput(thumbnailFile, errors);
            }
        }
    } catch (InterruptedException e) {
    // 
    }
    if (rv == null) {
        log.warn("Could not generate thumbnail: " + thumbnailFile, null);
    }
    return rv;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) IOException(java.io.IOException)

Example 8 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class ImageMagickHelper method thumbnailPDF.

@Override
public Size thumbnailPDF(VFSLeaf pdfFile, VFSLeaf thumbnailFile, int maxWidth, int maxHeight) {
    File baseFile = extractIOFile(pdfFile);
    File thumbnailBaseFile = extractIOFile(thumbnailFile);
    FinalSize finalSize = generateThumbnail(baseFile, thumbnailBaseFile, true, maxWidth, maxHeight, false);
    if (finalSize != null) {
        return new Size(finalSize.getWidth(), finalSize.getHeight(), true);
    }
    return null;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) File(java.io.File)

Example 9 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class ImageMagickHelper method extractSizeFromChuck.

private FinalSize extractSizeFromChuck(String chuck) {
    FinalSize size = null;
    if (chuck.indexOf('x') > 0) {
        String[] sizes = chuck.split("x");
        if (sizes.length == 2) {
            try {
                int width = Integer.parseInt(sizes[0]);
                int height = Integer.parseInt(sizes[1]);
                return new FinalSize(width, height);
            } catch (NumberFormatException e) {
                // not a number, it's possible
                if (log.isDebug()) {
                    log.debug("Not a size: " + chuck, null);
                }
            }
        }
    }
    return size;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize)

Example 10 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class MovieServiceImpl method generateThumbnail.

@Override
public FinalSize generateThumbnail(VFSLeaf file, VFSLeaf thumbnailFile, int maxWidth, int maxHeight, boolean fill) throws CannotGenerateThumbnailException {
    FinalSize size = null;
    if (file instanceof LocalFileImpl && thumbnailFile instanceof LocalFileImpl) {
        try {
            WorkThreadInformations.setInfoFiles(null, file);
            WorkThreadInformations.set("Generate thumbnail (video) VFSLeaf=" + file);
            File baseFile = ((LocalFileImpl) file).getBasefile();
            File scaledImage = ((LocalFileImpl) thumbnailFile).getBasefile();
            BufferedImage frame = FrameGrab.getFrame(baseFile, 20);
            Size scaledSize = ImageHelperImpl.calcScaledSize(frame, maxWidth, maxHeight);
            if (ImageHelperImpl.writeTo(frame, scaledImage, scaledSize, "jpeg")) {
                size = new FinalSize(scaledSize.getWidth(), scaledSize.getHeight());
            }
        // NullPointerException can be thrown if the jcodec cannot handle the codec of the movie
        // ArrayIndexOutOfBoundsException
        } catch (Exception | AssertionError e) {
            log.error("", e);
        } finally {
            WorkThreadInformations.unset();
        }
    }
    return size;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)

Aggregations

FinalSize (org.olat.core.commons.services.thumbnail.FinalSize)14 File (java.io.File)8 Size (org.olat.core.commons.services.image.Size)6 CannotGenerateThumbnailException (org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)4 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)4 BufferedImage (java.awt.image.BufferedImage)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 RandomAccessFile (java.io.RandomAccessFile)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2