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;
}
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;
}
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;
}
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));
}
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;
}
Aggregations