View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.sequencer.image;
25  
26  import java.io.InputStream;
27  import org.modeshape.graph.property.NameFactory;
28  import org.modeshape.graph.property.Path;
29  import org.modeshape.graph.property.PathFactory;
30  import org.modeshape.graph.sequencer.SequencerOutput;
31  import org.modeshape.graph.sequencer.StreamSequencer;
32  import org.modeshape.graph.sequencer.StreamSequencerContext;
33  
34  /**
35   * A sequencer that processes the binary content of an image file, extracts the metadata for the image, and then writes that image
36   * metadata to the repository.
37   * <p>
38   * This sequencer produces data that corresponds to the following structure:
39   * <ul>
40   * <li><strong>image:metadata</strong> node of type <code>image:metadata</code>
41   * <ul>
42   * <li><strong>jcr:mimeType</strong> - optional string property for the mime type of the image</li>
43   * <li><strong>jcr:encoding</strong> - optional string property for the encoding of the image</li>
44   * <li><strong>image:formatName</strong> - string property for the name of the format</li>
45   * <li><strong>image:width</strong> - optional integer property for the image's width in pixels</li>
46   * <li><strong>image:height</strong> - optional integer property for the image's height in pixles</li>
47   * <li><strong>image:bitsPerPixel</strong> - optional integer property for the number of bits per pixel</li>
48   * <li><strong>image:progressive</strong> - optional boolean property specifying whether the image is stored in a progressive
49   * (i.e., interlaced) form</li>
50   * <li><strong>image:numberOfImages</strong> - optional integer property for the number of images stored in the file; defaults to
51   * 1</li>
52   * <li><strong>image:physicalWidthDpi</strong> - optional integer property for the physical width of the image in dots per inch</li>
53   * <li><strong>image:physicalHeightDpi</strong> - optional integer property for the physical height of the image in dots per inch</li>
54   * <li><strong>image:physicalWidthInches</strong> - optional double property for the physical width of the image in inches</li>
55   * <li><strong>image:physicalHeightInches</strong> - optional double property for the physical height of the image in inches</li>
56   * </ul>
57   * </li>
58   * </ul>
59   * </p>
60   * <p>
61   * This structure could be extended in the future to add EXIF and IPTC metadata as child nodes. For example, EXIF metadata is
62   * structured as tags in directories, where the directories form something like namespaces, and which are used by different camera
63   * vendors to store custom metadata. This structure could be mapped with each directory (e.g. "EXIF" or "Nikon Makernote" or
64   * "IPTC") as the name of a child node, with the EXIF tags values stored as either properties or child nodes.
65   * </p>
66   */
67  public class ImageMetadataSequencer implements StreamSequencer {
68  
69      public static final String METADATA_NODE = "image:metadata";
70      public static final String IMAGE_PRIMARY_TYPE = "jcr:primaryType";
71      public static final String IMAGE_MIXINS = "jcr:mixinTypes";
72      public static final String IMAGE_MIME_TYPE = "jcr:mimeType";
73      public static final String IMAGE_ENCODING = "jcr:encoding";
74      public static final String IMAGE_FORMAT_NAME = "image:formatName";
75      public static final String IMAGE_WIDTH = "image:width";
76      public static final String IMAGE_HEIGHT = "image:height";
77      public static final String IMAGE_BITS_PER_PIXEL = "image:bitsPerPixel";
78      public static final String IMAGE_PROGRESSIVE = "image:progressive";
79      public static final String IMAGE_NUMBER_OF_IMAGES = "image:numberOfImages";
80      public static final String IMAGE_PHYSICAL_WIDTH_DPI = "image:physicalWidthDpi";
81      public static final String IMAGE_PHYSICAL_HEIGHT_DPI = "image:physicalHeightDpi";
82      public static final String IMAGE_PHYSICAL_WIDTH_INCHES = "image:physicalWidthInches";
83      public static final String IMAGE_PHYSICAL_HEIGHT_INCHES = "image:physicalHeightInches";
84  
85      /**
86       * {@inheritDoc}
87       * 
88       * @see StreamSequencer#sequence(InputStream, SequencerOutput, StreamSequencerContext)
89       */
90      public void sequence( InputStream stream,
91                            SequencerOutput output,
92                            StreamSequencerContext context ) {
93  
94          ImageMetadata metadata = new ImageMetadata();
95          metadata.setInput(stream);
96          metadata.setDetermineImageNumber(true);
97          metadata.setCollectComments(true);
98  
99          // Process the image stream and extract the metadata ...
100         if (!metadata.check()) {
101             metadata = null;
102         }
103 
104         // Generate the output graph if we found useful metadata ...
105         if (metadata != null) {
106             NameFactory nameFactory = context.getValueFactories().getNameFactory();
107             PathFactory pathFactory = context.getValueFactories().getPathFactory();
108             String metadataNodePath = METADATA_NODE;
109             Path metadataNode = pathFactory.create(metadataNodePath);
110 
111             // Place the image metadata into the output map ...
112             output.setProperty(metadataNode, nameFactory.create(IMAGE_PRIMARY_TYPE), "image:metadata");
113             // output.psetProperty(metadataNode, nameFactory.create(IMAGE_MIXINS), "");
114             output.setProperty(metadataNode, nameFactory.create(IMAGE_MIME_TYPE), metadata.getMimeType());
115             // output.setProperty(metadataNode, nameFactory.create(IMAGE_ENCODING), "");
116             output.setProperty(metadataNode, nameFactory.create(IMAGE_FORMAT_NAME), metadata.getFormatName());
117             output.setProperty(metadataNode, nameFactory.create(IMAGE_WIDTH), metadata.getWidth());
118             output.setProperty(metadataNode, nameFactory.create(IMAGE_HEIGHT), metadata.getHeight());
119             output.setProperty(metadataNode, nameFactory.create(IMAGE_BITS_PER_PIXEL), metadata.getBitsPerPixel());
120             output.setProperty(metadataNode, nameFactory.create(IMAGE_PROGRESSIVE), metadata.isProgressive());
121             output.setProperty(metadataNode, nameFactory.create(IMAGE_NUMBER_OF_IMAGES), metadata.getNumberOfImages());
122             output.setProperty(metadataNode, nameFactory.create(IMAGE_PHYSICAL_WIDTH_DPI), metadata.getPhysicalWidthDpi());
123             output.setProperty(metadataNode, nameFactory.create(IMAGE_PHYSICAL_HEIGHT_DPI), metadata.getPhysicalHeightDpi());
124             output.setProperty(metadataNode, nameFactory.create(IMAGE_PHYSICAL_WIDTH_INCHES), metadata.getPhysicalWidthInch());
125             output.setProperty(metadataNode, nameFactory.create(IMAGE_PHYSICAL_HEIGHT_INCHES), metadata.getPhysicalHeightInch());
126         }
127     }
128 }