Search in sources :

Example 6 with Size

use of org.olat.core.commons.services.image.Size in project OpenOLAT by OpenOLAT.

the class MovieServiceImpl method getSize.

@Override
public Size getSize(VFSLeaf media, String suffix) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return null;
    }
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            org.jcodec.common.model.Size size = demuxer1.getMovie().getDisplaySize();
            // Case 1: standard case, get dimension from movie
            int w = size.getWidth();
            int h = size.getHeight();
            // Case 2: landscape movie from iOS: width and height is negative, no dunny why
            if (w < 0 && h < 0) {
                w = 0 - w;
                h = 0 - h;
            }
            if (w == 0) {
                // something in the track box.
                try {
                    // This code is the way it is just because I don't know
                    // how to safely read the rotation/portrait/landscape
                    // flag of the movie. Those mp4 guys are really
                    // secretive folks, did not find any documentation about
                    // this. Best guess.
                    org.jcodec.common.model.Size size2 = demuxer1.getVideoTrack().getBox().getCodedSize();
                    w = size2.getHeight();
                    h = size2.getWidth();
                } catch (Exception e) {
                    log.debug("can not get size from box " + e.getMessage());
                }
            }
            return new Size(w, h, false);
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract size of: " + media, e);
        }
    } else if (suffix.equals("flv")) {
        try (InputStream stream = new FileInputStream(file)) {
            FLVParser infos = new FLVParser();
            infos.parse(stream);
            if (infos.getWidth() > 0 && infos.getHeight() > 0) {
                int w = infos.getWidth();
                int h = infos.getHeight();
                return new Size(w, h, false);
            }
        } catch (Exception e) {
            log.error("Cannot extract size of: " + media, e);
        }
    }
    return null;
}
Also used : MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) FileInputStream(java.io.FileInputStream) VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) FLVParser(org.olat.core.commons.services.video.spi.FLVParser) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 7 with Size

use of org.olat.core.commons.services.image.Size in project OpenOLAT by OpenOLAT.

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)

Example 8 with Size

use of org.olat.core.commons.services.image.Size in project OpenOLAT by OpenOLAT.

the class ImageComponent method setMaxWithAndHeightToFitWithin.

/**
 * Call this method to display the image within a given box of width and
 * height. The method does NOT manipulate the image itself, it does only
 * adjust the images width and height tag. <br />
 * The image will made displayed smaller, it will not enlarge the image since
 * this always looks bad. The scaling is done in a way to get an image that is
 * smaller than the maxWidth or smaller than the maxHeight, depending on whith
 * of the sizes produce a smaller scaling factor. <br />
 * To scale an image on the filesystem to another width and height, use the
 * ImageHelper.scaleImage() method.
 *
 * @param maxWidth
 * @param maxHeight
 */
public void setMaxWithAndHeightToFitWithin(int maxWidth, int maxHeight) {
    if (media == null || !media.exists()) {
        scalingFactor = Float.NaN;
        realSize = null;
        scaledSize = null;
        return;
    }
    try {
        Size size = getRealSize();
        if (size == null) {
            return;
        }
        int realWidth = size.getWidth();
        int realHeight = size.getHeight();
        // calculate scaling factor
        scalingFactor = 1f;
        if (realWidth > maxWidth) {
            float scalingWidth = 1f / realWidth * maxWidth;
            scalingFactor = (scalingWidth < scalingFactor ? scalingWidth : scalingFactor);
        }
        if (realHeight > maxHeight) {
            float scalingHeight = 1f / realHeight * maxHeight;
            scalingFactor = (scalingHeight < scalingFactor ? scalingHeight : scalingFactor);
        }
        realSize = new Size(realWidth, realHeight, false);
        scaledSize = new Size(Math.round(realWidth * scalingFactor), Math.round(realHeight * scalingFactor), false);
        setDirty(true);
    } catch (Exception e) {
        // log error, don't do anything else
        log.error("Problem while setting image size to fit " + maxWidth + "x" + maxHeight + " for resource::" + media, e);
    }
}
Also used : Size(org.olat.core.commons.services.image.Size)

Example 9 with Size

use of org.olat.core.commons.services.image.Size in project OpenOLAT by OpenOLAT.

the class ImageRenderer method renderMovie.

private void renderMovie(StringOutput sb, ImageComponent ic) {
    // Use configured calculated scaled size, fallback to default size
    int width = 320;
    int height = 240;
    Size size = ic.getScaledSize();
    if (size != null) {
        width = size.getWidth();
        // +20 because of toolbar
        height = size.getHeight() + 20;
    }
    // Add video name with mime type ending for better browser support
    String mapperUrl = ic.getMapperUrl();
    String name = ic.getMedia().getName();
    if (name.lastIndexOf('.') > 0) {
        mapperUrl += "/" + name;
    } else {
        mapperUrl += "/video." + ic.getSuffix(ic.getMimeType());
    }
    // Add poster image if available
    String poster = null;
    if (ic.getPoster() != null) {
        poster = ic.getPosterMapperUrl() + "/" + ic.getPoster().getName();
    }
    // Provide own component dispatch ID and wrap in div
    String compId = "o_c" + ic.getDispatchID();
    // START component
    sb.append("<div id='").append(compId).append("' class='o_video'>");
    // The inner component
    String imgId = "mov_" + ic.getDispatchID();
    sb.append("<div id='").append(imgId).append("' name='").append(imgId).append("' style='width:").append(width).append("px; height:").append(height).append("px;' class='o_video_wrapper'></div>").append("<script type='text/javascript'>").append("/* <![CDATA[ */").append("BPlayer.insertPlayer('").append(Settings.createServerURI()).append(mapperUrl);
    sb.append("','").append(imgId).append("',").append(width).append(",").append(height).append(",'video'");
    if (poster != null) {
        sb.append(",null,null,null,null,null,null,'").append(poster).append("'");
    }
    sb.append(");").append("/* ]]> */").append("</script>").append(// ENDcomponent
    "</div>");
}
Also used : Size(org.olat.core.commons.services.image.Size)

Example 10 with Size

use of org.olat.core.commons.services.image.Size in project OpenOLAT by OpenOLAT.

the class ImageRenderer method renderImage.

private void renderImage(StringOutput sb, ImageComponent ic) {
    // Provide own component dispatch ID and wrap in div
    String compId = "o_c" + ic.getDispatchID();
    Size scaledSize = ic.getScaledSize();
    boolean cropEnabled = ic.isCropSelectionEnabled();
    if (cropEnabled) {
        // wrapper for cropper.js
        sb.append("<div style='");
        if (scaledSize != null) {
            sb.append("width:").append(scaledSize.getWidth()).append("px;");
            sb.append("height:").append(scaledSize.getHeight()).append("px;");
        }
        sb.append("'>");
    }
    boolean divWrapper = ic.isDivImageWrapper();
    if (divWrapper) {
        // START component
        sb.append("<div id='").append(compId).append("' class='o_image'>");
    }
    // The inner component
    String imgId = divWrapper ? "o_img" + ic.getDispatchID() : compId;
    sb.append("<img").append(" id='").append(imgId).append("'");
    if (scaledSize != null) {
        sb.append(" width=\"").append(scaledSize.getWidth()).append("\"");
        sb.append(" height=\"").append(scaledSize.getHeight()).append("\"");
    }
    String mapperUrl = ic.getMapperUrl();
    String name = ic.getMedia().getName();
    if (name.lastIndexOf('.') > 0) {
        mapperUrl += "/" + name + "?" + System.nanoTime();
    } else {
        mapperUrl += "/?" + System.nanoTime();
    }
    sb.append(" src='").append(mapperUrl).append("' alt=\"");
    if (StringHelper.containsNonWhitespace(ic.getAlt())) {
        sb.append(ic.getAlt());
    } else {
        sb.append("*");
    }
    sb.append("\" />");
    if (cropEnabled) {
        sb.append("<input id='").append(imgId).append("_x' name='").append(imgId).append("_x' type='hidden' value='' />").append("<input id='").append(imgId).append("_y' name='").append(imgId).append("_y' type='hidden' value='' />").append("<input id='").append(imgId).append("_w' name='").append(imgId).append("_w' type='hidden' value='' />").append("<input id='").append(imgId).append("_h' name='").append(imgId).append("_h' type='hidden' value='' />");
        sb.append("<script type='text/javascript'>\n").append("/* <![CDATA[ */ \n").append("jQuery(function() {\n").append("  jQuery('#").append(imgId).append("').cropper({\n").append("    aspectRatio:1,\n").append("    done: function(crop) {\n").append("      jQuery('input#").append(imgId).append("_x').val(crop.x1);\n").append("    	 jQuery('input#").append(imgId).append("_y').val(crop.y1);\n").append("    	 jQuery('input#").append(imgId).append("_w').val(crop.width);\n").append("    	 jQuery('input#").append(imgId).append("_h').val(crop.height);\n").append("    }").append("  });").append("});").append("/* ]]> */\n").append("</script>");
    }
    // ENDcomponent
    sb.append("</div>", divWrapper);
    sb.append("</div>", cropEnabled);
}
Also used : Size(org.olat.core.commons.services.image.Size)

Aggregations

Size (org.olat.core.commons.services.image.Size)76 File (java.io.File)24 IOException (java.io.IOException)22 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)22 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)20 VFSContainer (org.olat.core.util.vfs.VFSContainer)14 FileInputStream (java.io.FileInputStream)12 InputStream (java.io.InputStream)12 ImageInputStream (javax.imageio.stream.ImageInputStream)12 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)12 BufferedImage (java.awt.image.BufferedImage)10 ImageReader (javax.imageio.ImageReader)8 CannotGenerateThumbnailException (org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)8 FinalSize (org.olat.core.commons.services.thumbnail.FinalSize)7 CMMException (java.awt.color.CMMException)6 ArrayList (java.util.ArrayList)6 VideoMeta (org.olat.modules.video.VideoMeta)5 RandomAccessFile (java.io.RandomAccessFile)4 Date (java.util.Date)4 List (java.util.List)4