Search in sources :

Example 46 with Size

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

the class VideoManagerImpl method setPosterframeResizeUploadfile.

/**
 * Sets the posterframe resize uploadfile. Tries to fit image to dimensions of video.
 *
 * @param videoResource the video resource
 * @param posterframe the newPosterFile
 */
@Override
public void setPosterframeResizeUploadfile(OLATResource videoResource, VFSLeaf newPosterFile) {
    VideoMeta videoMetadata = getVideoMetadata(videoResource);
    Size posterRes = imageHelper.getSize(newPosterFile, FILETYPE_JPG);
    // file size needs to be bigger than target resolution, otherwise use image as it comes
    if (posterRes != null && posterRes.getHeight() != 0 && posterRes.getWidth() != 0 && posterRes.getHeight() >= videoMetadata.getHeight() && posterRes.getWidth() >= videoMetadata.getWidth()) {
        VFSLeaf oldPosterFile = getPosterframe(videoResource);
        oldPosterFile.delete();
        VFSContainer masterContainer = getMasterContainer(videoResource);
        LocalFileImpl newPoster = (LocalFileImpl) masterContainer.createChildLeaf(FILENAME_POSTER_JPG);
        // to shrink image file, resolution ratio needs to be equal, otherwise crop from top left corner
        if (posterRes.getHeight() / posterRes.getWidth() == videoMetadata.getHeight() / videoMetadata.getWidth()) {
            imageHelper.scaleImage(newPosterFile, newPoster, videoMetadata.getWidth(), videoMetadata.getHeight(), true);
        } else {
            Crop cropSelection = new Crop(0, 0, videoMetadata.getHeight(), videoMetadata.getWidth());
            imageHelper.cropImage(((LocalFileImpl) newPosterFile).getBasefile(), newPoster.getBasefile(), cropSelection);
        }
    } else {
        setPosterframe(videoResource, newPosterFile);
    }
}
Also used : Crop(org.olat.core.commons.services.image.Crop) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VideoMeta(org.olat.modules.video.VideoMeta) Size(org.olat.core.commons.services.image.Size) VFSContainer(org.olat.core.util.vfs.VFSContainer) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl)

Example 47 with Size

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

the class CatalogManager method setImage.

public boolean setImage(VFSLeaf newImageFile, CatalogEntryRef re) {
    VFSLeaf currentImage = getImage(re);
    if (currentImage != null) {
        if (currentImage instanceof MetaTagged) {
            MetaInfo info = ((MetaTagged) currentImage).getMetaInfo();
            if (info != null) {
                info.clearThumbnails();
            }
        }
        currentImage.delete();
    }
    String extension = FileUtils.getFileSuffix(newImageFile.getName());
    if (StringHelper.containsNonWhitespace(extension)) {
        extension = extension.toLowerCase();
    }
    boolean ok = false;
    VFSContainer catalogResourceHome = getCatalogResourcesHome();
    try {
        if ("jpeg".equals(extension) || "jpg".equals(extension)) {
            VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".jpg");
            ok = VFSManager.copyContent(newImageFile, repoImage);
        } else if ("png".equals(extension)) {
            VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".png");
            ok = VFSManager.copyContent(newImageFile, repoImage);
        } else {
            // scale to default and png
            VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".png");
            Size size = imageHelper.scaleImage(newImageFile, repoImage, 570, 570, true);
            ok = size != null;
        }
    } catch (Exception e) {
        log.error("", e);
    }
    return ok;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Size(org.olat.core.commons.services.image.Size) VFSContainer(org.olat.core.util.vfs.VFSContainer) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo)

Example 48 with Size

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

the class VideoDisplayController method loadVideo.

/**
 * Internal helper to do the actual video loading, checking for transcoded versions and captions
 * @param ureq
 * @param video
 */
private void loadVideo(UserRequest ureq, VFSLeaf video) {
    mainVC.contextPut("title", entry.getDisplayname());
    String desc = (descriptionText != null ? descriptionText : entry.getDescription());
    setText(desc, "description");
    String authors = entry.getAuthors();
    mainVC.contextPut("authors", (StringHelper.containsNonWhitespace(authors) ? authors : null));
    if (video != null) {
        // get resolution of master video resource
        Size masterResolution = videoManager.getVideoResolutionFromOLATResource(entry.getOlatResource());
        String masterTitle = videoManager.getDisplayTitleForResolution(masterResolution.getHeight(), getTranslator());
        String masterSize = " (" + Formatter.formatBytes(videoManager.getVideoMetadata(entry.getOlatResource()).getSize()) + ")";
        boolean addMaster = true;
        // Mapper for Video
        String masterMapperId = "master-" + entry.getOlatResource().getResourceableId();
        String masterUrl = registerCacheableMapper(ureq, masterMapperId, new VideoMediaMapper(videoManager.getMasterContainer(entry.getOlatResource())));
        mainVC.contextPut("masterUrl", masterUrl);
        // Mapper for versions specific because not in same base as the resource itself
        String transcodingMapperId = "transcoding-" + entry.getOlatResource().getResourceableId();
        VFSContainer transcodedContainer = videoManager.getTranscodingContainer(entry.getOlatResource());
        String transcodedUrl = registerCacheableMapper(ureq, transcodingMapperId, new VideoMediaMapper(transcodedContainer));
        mainVC.contextPut("transcodedUrl", transcodedUrl);
        // Add transcoded versions
        List<VideoTranscoding> videos = videoManager.getVideoTranscodings(entry.getOlatResource());
        List<VideoTranscoding> readyToPlayVideos = new ArrayList<>();
        List<String> displayTitles = new ArrayList<>();
        int preferredAvailableResolution = 0;
        for (VideoTranscoding videoTranscoding : videos) {
            if (videoTranscoding.getStatus() == VideoTranscoding.TRANSCODING_STATUS_DONE) {
                readyToPlayVideos.add(videoTranscoding);
                // Check if at least one has equal height, else use master as resource
                addMaster &= videoTranscoding.getHeight() < masterResolution.getHeight();
                // Use the users preferred resolution or the next higher resolution
                if (videoTranscoding.getResolution() >= userPreferredResolution.intValue()) {
                    preferredAvailableResolution = readyToPlayVideos.size() - 1;
                }
                // Calculate title. Standard title for standard resolution, original title if not standard resolution
                String title = videoManager.getDisplayTitleForResolution(videoTranscoding.getResolution(), getTranslator());
                displayTitles.add(title);
            }
        }
        mainVC.contextPut("addMaster", addMaster);
        mainVC.contextPut("masterTitle", masterTitle + masterSize);
        mainVC.contextPut("videos", readyToPlayVideos);
        mainVC.contextPut("displayTitles", displayTitles);
        mainVC.contextPut("useSourceChooser", Boolean.valueOf(readyToPlayVideos.size() > 1));
        mainVC.contextPut(GUIPREF_KEY_PREFERRED_RESOLUTION, preferredAvailableResolution);
        // Check for null-value posters
        VFSLeaf poster = videoManager.getPosterframe(entry.getOlatResource());
        mainVC.contextPut("usePoster", Boolean.valueOf(poster != null && poster.getSize() > 0));
        // Load the track from config
        Map<String, String> trackfiles = new HashMap<String, String>();
        Map<String, VFSLeaf> configTracks = videoManager.getAllTracks(entry.getOlatResource());
        for (HashMap.Entry<String, VFSLeaf> track : configTracks.entrySet()) {
            trackfiles.put(track.getKey(), track.getValue().getName());
        }
        mainVC.contextPut("trackfiles", trackfiles);
        // Load video chapter if available
        mainVC.contextPut("hasChapters", videoManager.hasChapters(entry.getOlatResource()));
        // Add duration without preloading video
        String duration = entry.getExpenditureOfWork();
        if (!StringHelper.containsNonWhitespace(duration)) {
            duration = "00:00";
        }
        mainVC.contextPut("duration", duration);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) HashMap(java.util.HashMap) Size(org.olat.core.commons.services.image.Size) VFSContainer(org.olat.core.util.vfs.VFSContainer) VideoTranscoding(org.olat.modules.video.VideoTranscoding) ArrayList(java.util.ArrayList) VideoMediaMapper(org.olat.modules.video.manager.VideoMediaMapper)

Example 49 with Size

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

the class ItemFormController method fileUploaded.

/**
 * Helper to process a uploaded file. Adjust the filename and the file
 * suffix. Guess the width and height and fill the appropriate fields.
 */
private void fileUploaded() {
    file.clearError();
    if (file.isUploadSuccess()) {
        String newFilename = file.getUploadFileName().toLowerCase().replaceAll(" ", "_");
        file.setUploadFileName(newFilename);
        VFSLeaf movie = new LocalFileImpl(file.getUploadFile());
        if (newFilename.endsWith("mov") || newFilename.endsWith("m4v")) {
            // when uploading a video from an iOS device.
            if (movieService.isMP4(movie, newFilename)) {
                newFilename = newFilename.substring(0, newFilename.length() - 3) + "mp4";
                file.setUploadFileName(newFilename);
            }
        }
        if (validateFilename(newFilename)) {
            // try to detect width and height for movies, prefill for user if possible
            Size size = movieService.getSize(movie, FileUtils.getFileSuffix(newFilename));
            if (size != null) {
                if (size.getWidth() > 1) {
                    widthEl.setValue(Integer.toString(size.getWidth()));
                }
                if (size.getHeight() > 1) {
                    heightEl.setValue(Integer.toString(size.getHeight()));
                }
            }
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Size(org.olat.core.commons.services.image.Size) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl)

Example 50 with Size

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

the class OLATUpgrade_11_3_0 method processVideoResource.

private boolean processVideoResource(RepositoryEntry entry) {
    try {
        OLATResource videoResource = entry.getOlatResource();
        if (!videoManager.hasMasterContainer(videoResource)) {
            log.error("RepoEntry: " + entry.getKey() + " has no valid master container.");
            // log error but return true to proceed
            return true;
        }
        // update track files on file system
        VFSContainer masterContainer = videoManager.getMasterContainer(videoResource);
        if (videoManager.isMetadataFileValid(videoResource)) {
            VideoMetadata metafromXML = videoManager.readVideoMetadataFile(videoResource);
            for (Entry<String, String> track : metafromXML.getAllTracks().entrySet()) {
                VFSItem item = masterContainer.resolve(track.getValue());
                if (item != null && item instanceof VFSLeaf) {
                    String path = VideoManagerImpl.TRACK + track.getKey() + VideoManagerImpl.DOT + FilenameUtils.getExtension(track.getValue());
                    // check if modified track file already exists
                    if (masterContainer.resolve(path) == null) {
                        VFSLeaf target = masterContainer.createChildLeaf(path);
                        VFSManager.copyContent((VFSLeaf) item, target);
                    }
                }
            }
        } else {
            log.error("RepoEntry: " + entry.getKey() + " has no valid Video Metadata XML file.");
        }
        // create meta data entries on database
        if (videoManager.hasVideoFile(videoResource)) {
            File videoFile = videoManager.getVideoFile(videoResource);
            String fileName = videoFile.getName();
            long size = videoFile.length();
            String format = FilenameUtils.getExtension(fileName);
            if (videoManager.hasVideoMetadata(videoResource)) {
                VideoMetaImpl entity = new VideoMetaImpl();
                entity.setVideoResource(videoResource);
                entity.setFormat(format);
                entity.setCreationDate(new Date());
                entity.setLastModified(new Date());
                Size resolution = videoManager.getVideoResolutionFromOLATResource(videoResource);
                entity.setHeight(resolution.getHeight());
                entity.setWidth(resolution.getWidth());
                entity.setSize(size);
                entity.setLength(entry.getExpenditureOfWork());
                dbInstance.getCurrentEntityManager().persist(entity);
            }
        } else {
            log.error("RepoEntry: " + entry.getKey() + " has no valid resource.");
        }
        return true;
    } catch (Exception e) {
        log.error("Update Metadata failed", e);
        return false;
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Size(org.olat.core.commons.services.image.Size) VFSContainer(org.olat.core.util.vfs.VFSContainer) OLATResource(org.olat.resource.OLATResource) VFSItem(org.olat.core.util.vfs.VFSItem) VideoMetadata(org.olat.modules.video.VideoMetadata) Date(java.util.Date) VideoMetaImpl(org.olat.modules.video.model.VideoMetaImpl) File(java.io.File)

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