Search in sources :

Example 41 with Size

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

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 42 with Size

use of org.olat.core.commons.services.image.Size 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)

Example 43 with Size

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

the class OpenXMLDocument method registerImage.

private DocReference registerImage(File image, double widthCm) {
    DocReference ref;
    if (fileToImagesMap.containsKey(image)) {
        ref = fileToImagesMap.get(image);
    } else {
        String id = generateId();
        Size size = ImageUtils.getImageSize(image);
        OpenXMLSize emuSize = OpenXMLUtils.convertPixelToEMUs(size, DPI, widthCm);
        String filename = getUniqueFilename(image);
        ref = new DocReference(id, filename, emuSize, image);
        fileToImagesMap.put(image, ref);
    }
    return ref;
}
Also used : Size(org.olat.core.commons.services.image.Size)

Example 44 with Size

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

the class HotspotEditorController method formOK.

@Override
protected void formOK(UserRequest ureq) {
    if (readOnly)
        return;
    itemBuilder.setTitle(titleEl.getValue());
    // set the question with the text entries
    String questionText = textEl.getRawValue();
    itemBuilder.setQuestion(questionText);
    itemBuilder.setResponsive(responsiveEl.isAtLeastSelected(1));
    File objectImg = null;
    if (backgroundImage != null) {
        objectImg = backgroundImage;
    } else if (initialBackgroundImage != null) {
        objectImg = initialBackgroundImage;
    }
    if (cardinalityEl.isOneSelected()) {
        String selectedCardinality = cardinalityEl.getSelectedKey();
        itemBuilder.setCardinality(Cardinality.valueOf(selectedCardinality));
    }
    boolean updateHotspot = true;
    if (objectImg != null) {
        String filename = objectImg.getName();
        String mimeType = WebappHelper.getMimeType(filename);
        Size currentSize = imageService.getSize(new LocalFileImpl(objectImg), null);
        Size size = currentSize;
        if (resizeEl.isVisible() && !resizeEl.isSelected(0)) {
            int maxSize = Integer.parseInt(resizeEl.getSelectedKey());
            if (maxSize < currentSize.getHeight() || maxSize < currentSize.getWidth()) {
                String extension = FileUtils.getFileSuffix(filename);
                size = imageService.scaleImage(objectImg, extension, objectImg, maxSize, maxSize, false);
                setBackgroundSize(size);
                scaleHotspot(currentSize, size);
                optimizeResizeEl(size, false);
                updateHotspot = false;
            }
        }
        int height = -1;
        int width = -1;
        if (size != null) {
            height = size.getHeight();
            width = size.getWidth();
        }
        String relPath = itemFile.getParentFile().toPath().relativize(objectImg.toPath()).toString();
        itemBuilder.setBackground(relPath, mimeType, height, width);
    }
    if (updateHotspot) {
        updateHotspots(ureq);
    }
    if (layoutEl.isOneSelected()) {
        String selectedLayout = layoutEl.getSelectedKey();
        for (HotspotLayouts layout : HotspotLayouts.values()) {
            itemBuilder.removeHotspotInteractionClass(layout.cssClass());
        }
        itemBuilder.addHotspotInteractionClass(selectedLayout);
    }
    if (shadowEl.isAtLeastSelected(1)) {
        itemBuilder.removeHotspotInteractionClass(QTI21Constants.CSS_HOTSPOT_DISABLE_SHADOW);
    } else {
        itemBuilder.addHotspotInteractionClass(QTI21Constants.CSS_HOTSPOT_DISABLE_SHADOW);
    }
    fireEvent(ureq, new AssessmentItemEvent(AssessmentItemEvent.ASSESSMENT_ITEM_CHANGED, itemBuilder.getAssessmentItem(), QTI21QuestionType.hotspot));
}
Also used : HotspotLayouts(org.olat.ims.qti21.QTI21Constants.HotspotLayouts) Size(org.olat.core.commons.services.image.Size) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) File(java.io.File) AssessmentItemEvent(org.olat.ims.qti21.ui.editor.events.AssessmentItemEvent)

Example 45 with Size

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

the class VideoManagerImpl method getVideoResolutionFromOLATResource.

@Override
public Size getVideoResolutionFromOLATResource(OLATResource videoResource) {
    VFSContainer masterContainer = getMasterContainer(videoResource);
    VFSLeaf targetFile = (VFSLeaf) masterContainer.resolve(FILENAME_VIDEO_MP4);
    Size videoSize = movieService.getSize(targetFile, FILETYPE_MP4);
    if (videoSize == null) {
        videoSize = new Size(800, 600, false);
    }
    return videoSize;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Size(org.olat.core.commons.services.image.Size) VFSContainer(org.olat.core.util.vfs.VFSContainer)

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