Search in sources :

Example 6 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageHelper method getOrientation.

public static int getOrientation(final File originalImage) {
    try {
        final ExifIFD0Directory exifIFD0Directory = getMetadata(originalImage).getFirstDirectoryOfType(ExifIFD0Directory.class);
        if (exifIFD0Directory != null && exifIFD0Directory.hasTagName(ExifIFD0Directory.TAG_ORIENTATION)) {
            final Integer orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
            originalImage.setProperty(StructrApp.key(Image.class, "orientation"), orientation);
            return orientation;
        }
    } catch (MetadataException | JSONException | FrameworkException ex) {
        logger.warn("Unable to store orientation information on image {} ({})", new Object[] { originalImage.getName(), originalImage.getId() });
    }
    return 1;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image) MetadataException(com.drew.metadata.MetadataException)

Example 7 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageHelper method createImageNode.

/**
 * Create a new image node from the given image data
 *
 * @param securityContext
 * @param imageData
 * @param contentType
 * @param imageType defaults to Image.class if null
 * @param name
 * @param markAsThumbnail
 * @return image
 * @throws FrameworkException
 * @throws IOException
 */
public static Image createImageNode(final SecurityContext securityContext, final byte[] imageData, final String contentType, final Class<? extends Image> imageType, final String name, final boolean markAsThumbnail) throws FrameworkException, IOException {
    final PropertyMap props = new PropertyMap();
    props.put(AbstractNode.type, imageType == null ? Image.class.getSimpleName() : imageType.getSimpleName());
    props.put(StructrApp.key(Image.class, "isThumbnail"), markAsThumbnail);
    props.put(AbstractNode.name, name);
    final Image newImage = StructrApp.getInstance(securityContext).create(imageType, props);
    if (imageData != null && imageData.length > 0) {
        setFileData(newImage, imageData, contentType);
    }
    return newImage;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image)

Example 8 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageHelper method updateMetadata.

/**
 * Update width and height
 *
 * @param image the image
 * @param fis file input stream
 * @throws FrameworkException
 */
public static void updateMetadata(final File image, final InputStream fis) throws FrameworkException {
    try (final InputStream is = fis) {
        final BufferedImage source = ImageIO.read(is);
        if (source != null) {
            final int sourceWidth = source.getWidth();
            final int sourceHeight = source.getHeight();
            final PropertyMap map = new PropertyMap();
            map.put(StructrApp.key(Image.class, "width"), sourceWidth);
            map.put(StructrApp.key(Image.class, "height"), sourceHeight);
            map.put(StructrApp.key(Image.class, "orientation"), ImageHelper.getOrientation(image));
            image.setProperties(image.getSecurityContext(), map);
        }
    } catch (IOException ex) {
        logger.warn("Unable to read image data", ex);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image) BufferedImage(java.awt.image.BufferedImage)

Example 9 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageHelper method createImage.

/**
 * Create a new image node from the given image data
 *
 * @param securityContext
 * @param imageStream
 * @param contentType
 * @param imageType defaults to Image.class if null
 * @param name
 * @param markAsThumbnail
 * @return image
 * @throws FrameworkException
 * @throws IOException
 */
public static Image createImage(final SecurityContext securityContext, final InputStream imageStream, final String contentType, final Class<? extends Image> imageType, final String name, final boolean markAsThumbnail) throws FrameworkException, IOException {
    final PropertyMap props = new PropertyMap();
    props.put(AbstractNode.type, imageType == null ? Image.class.getSimpleName() : imageType.getSimpleName());
    props.put(StructrApp.key(Image.class, "isThumbnail"), markAsThumbnail);
    props.put(AbstractNode.name, name);
    final Image newImage = StructrApp.getInstance(securityContext).create(imageType, props);
    setFileData(newImage, imageStream, contentType);
    return newImage;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image)

Example 10 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageConverter method convert.

// ~--- methods --------------------------------------------------------
@Override
public Object convert(final Object source) {
    if (source == null) {
        return false;
    }
    try {
        Image img = null;
        try {
            if (source instanceof byte[]) {
                byte[] data = (byte[]) source;
                MagicMatch match = Magic.getMagicMatch(data);
                String mimeType = match.getMimeType();
                if (keyAndClass != null) {
                    img = (Image) ImageHelper.createFile(securityContext, data, mimeType, keyAndClass.getCls());
                } else {
                    ImageHelper.setImageData((Image) currentObject, data, mimeType);
                }
            } else if (source instanceof String) {
                String sourceString = (String) source;
                if (StringUtils.isNotBlank(sourceString)) {
                    if (keyAndClass != null) {
                        // UUID?
                        if (sourceString.length() == 32) {
                            img = (Image) ImageHelper.transformFile(securityContext, sourceString, keyAndClass != null ? keyAndClass.getCls() : null);
                        }
                        if (img == null) {
                            img = (Image) ImageHelper.createFileBase64(securityContext, sourceString, keyAndClass != null ? keyAndClass.getCls() : null);
                        }
                    } else {
                        ImageHelper.decodeAndSetFileData((Image) currentObject, sourceString);
                        ImageHelper.updateMetadata((Image) currentObject);
                    }
                }
            }
        } catch (Throwable t) {
            logger.warn("Cannot create image node from given data", t);
        }
        if (img != null) {
            // manual indexing of UUID needed here to avoid a 404 in the following setProperty call
            img.updateInIndex();
            currentObject.setProperties(securityContext, new PropertyMap(keyAndClass.getPropertyKey(), img));
        }
    } catch (Throwable t) {
        logger.warn("Cannot create image node from given data", t);
    }
    return null;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Image(org.structr.web.entity.Image) MagicMatch(net.sf.jmimemagic.MagicMatch)

Aggregations

Image (org.structr.web.entity.Image)26 FrameworkException (org.structr.common.error.FrameworkException)18 Tx (org.structr.core.graph.Tx)12 PropertyMap (org.structr.core.property.PropertyMap)12 Test (org.junit.Test)9 StructrUiTest (org.structr.web.StructrUiTest)9 BufferedImage (java.awt.image.BufferedImage)8 IOException (java.io.IOException)7 File (org.structr.web.entity.File)6 AbstractFile (org.structr.web.entity.AbstractFile)5 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 SecurityContext (org.structr.common.SecurityContext)3 Relation (org.structr.core.entity.Relation)3 PropertyKey (org.structr.core.property.PropertyKey)3 Folder (org.structr.web.entity.Folder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)2