You are viewing a plain text version of this content. The canonical link for it is here.
Posted to photark-commits@incubator.apache.org by lr...@apache.org on 2010/02/21 00:44:12 UTC

svn commit: r912266 - in /incubator/photark/trunk/photark: ./ src/main/java/org/apache/photark/ src/main/java/org/apache/photark/util/ src/test/java/org/apache/photark/services/gallery/filesystem/ src/test/resources/gallery-home/album-1/ src/test/resou...

Author: lresende
Date: Sun Feb 21 00:44:11 2010
New Revision: 912266

URL: http://svn.apache.org/viewvc?rev=912266&view=rev
Log:
PHOTARK-17 - Adding backend support for scanning efix metadata attributes using Apache Commons Sanselan

Added:
    incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java   (with props)
    incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/
    incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java   (with props)
    incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java   (with props)
    incubator/photark/trunk/photark/src/test/resources/gallery-home/album-1/IMG_0735.jpg   (with props)
    incubator/photark/trunk/photark/src/test/resources/gallery-home/album-2/IMG_0735.jpg   (with props)
Removed:
    incubator/photark/trunk/photark/src/test/resources/gallery-home/album-1/file1.txt
    incubator/photark/trunk/photark/src/test/resources/gallery-home/album-2/file2.txt
Modified:
    incubator/photark/trunk/photark/pom.xml
    incubator/photark/trunk/photark/src/main/java/org/apache/photark/Image.java

Modified: incubator/photark/trunk/photark/pom.xml
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/pom.xml?rev=912266&r1=912265&r2=912266&view=diff
==============================================================================
--- incubator/photark/trunk/photark/pom.xml (original)
+++ incubator/photark/trunk/photark/pom.xml Sun Feb 21 00:44:11 2010
@@ -34,7 +34,7 @@
 			<artifactId>tuscany-sca-api</artifactId>
 			<version>2.0-M4</version>
 		</dependency>
-			
+
 		<dependency>
 			<groupId>org.apache.tuscany.sca</groupId>
 			<artifactId>tuscany-data-api</artifactId>
@@ -55,6 +55,12 @@
 		</dependency>
 
 		<dependency>
+			<groupId>org.apache.sanselan</groupId>
+			<artifactId>sanselan</artifactId>
+			<version>0.97-incubator</version>
+		</dependency>
+
+		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>4.5</version>

Modified: incubator/photark/trunk/photark/src/main/java/org/apache/photark/Image.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/src/main/java/org/apache/photark/Image.java?rev=912266&r1=912265&r2=912266&view=diff
==============================================================================
--- incubator/photark/trunk/photark/src/main/java/org/apache/photark/Image.java (original)
+++ incubator/photark/trunk/photark/src/main/java/org/apache/photark/Image.java Sun Feb 21 00:44:11 2010
@@ -19,31 +19,39 @@
 
 package org.apache.photark;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.InputStream;
 import java.util.Date;
 import java.util.List;
-import java.util.Properties;
+
+import org.apache.photark.util.ImageMetadataScanner;
 
 /**
  * Model representing an album image
  */
 public class Image {
-    private String name;
+    private String imageName;
     private Date datePosted;
     private InputStream imageStream;
 
-    private List<Properties> imageProperties;
+    private List<ImageMetadata> imageAttributes;
 
     /**
      * Constructor
-     * @param name Image name
+     * @param imageFile a File representing the image
      * @param datePosted Date when image is being added
      */
-    public Image(String name, Date datePosted) {
-        this.name = name;
+    public Image(File imageFile, Date datePosted) {
+        this.imageName = imageFile.getName();
         this.datePosted = datePosted;
+        try {
+            this.imageStream = new FileInputStream(imageFile);
+        } catch(Exception fnf) {
+            fnf.printStackTrace();
+        }
     }
-
+    
     /**
      * Constructor
      * @param name Image name
@@ -51,7 +59,8 @@
      * @param imageStream Image stream content
      */
     public Image(String name, Date datePosted, InputStream imageStream) {
-        this(name, datePosted);
+        this.imageName = name;
+        this.datePosted = datePosted;
         this.imageStream = imageStream;
     }
 
@@ -60,7 +69,7 @@
      * @return image file name
      */
     public String getName() {
-        return name;
+        return imageName;
     }
 
     /**
@@ -78,5 +87,16 @@
     public InputStream getImageAsStream() {
         return imageStream;
     }
+    
+    /**
+     * Return image metadata retrieved from EFIX properties
+     * @return list of image metadata attributes
+     */
+    public List<ImageMetadata> getImageMetadata() {
+        if(imageAttributes == null) {
+            imageAttributes = ImageMetadataScanner.scanImageMetadata(imageName, imageStream);
+        }
+        return imageAttributes;
+    }
 
 }

Added: incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java?rev=912266&view=auto
==============================================================================
--- incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java (added)
+++ incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java Sun Feb 21 00:44:11 2010
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.photark;
+
+import org.apache.sanselan.formats.tiff.constants.ExifTagConstants;
+
+/**
+ * Represent a EFIX Metatada attribute/value from 
+ * It implements the ExifTagConstants from Sanselan to leverage it's sxit field contants
+ */
+public class ImageMetadata implements ExifTagConstants {
+    private String key;
+    private String value;
+    
+    public ImageMetadata() {
+        
+    }
+    
+    public ImageMetadata(String key, String value) {
+        this.key = key;
+        this.value = value;
+    }
+    
+    /**
+     * Return EFIX Metadata attribute name
+     * @return
+     */
+    public String getKey() {
+        return key;
+    }
+    
+    /**
+     * Set EFIX Metadata attribute name
+     * @param key
+     */
+    public void setKey(String key) {
+        this.key = key;
+    }
+    
+    /**
+     * Get EFIX Metadata attribute value
+     * @return
+     */
+    public String getValue() {
+        return value;
+    }
+    
+    /**
+     * Set EFIX Metadata attribute value
+     * @param value
+     */
+    public void setValue(String value) {
+        this.value = value;
+    }
+}

Propchange: incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/photark/trunk/photark/src/main/java/org/apache/photark/ImageMetadata.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java?rev=912266&view=auto
==============================================================================
--- incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java (added)
+++ incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java Sun Feb 21 00:44:11 2010
@@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.photark.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.photark.ImageMetadata;
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.Sanselan;
+import org.apache.sanselan.common.IImageMetadata;
+import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
+import org.apache.sanselan.formats.tiff.TiffField;
+import org.apache.sanselan.formats.tiff.TiffImageMetadata;
+import org.apache.sanselan.formats.tiff.constants.TagInfo;
+import org.apache.sanselan.formats.tiff.constants.TiffConstants;
+
+public class ImageMetadataScanner {
+
+    public static List<ImageMetadata> scanImageMetadata(String fileName, InputStream imageStream) {
+        List<ImageMetadata> imageAttributes = new ArrayList<ImageMetadata>();
+        try {
+            
+            // get all metadata stored in EXIF format (ie. from JPEG or TIFF). 
+            IImageMetadata metadata = Sanselan.getMetadata(imageStream, fileName);
+            if (metadata instanceof JpegImageMetadata) {
+                    JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+
+                    // Jpeg EXIF metadata is stored in a TIFF-based directory structure
+                    // and is identified with TIFF tags.
+                    // Here we look for the "x resolution" tag, but
+                    // we could just as easily search for any other tag.
+                    // see the TiffConstants file for a list of TIFF tags.
+
+                    // populate various interesting EXIF tags.
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.TIFF_TAG_XRESOLUTION);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.TIFF_TAG_YRESOLUTION);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.EXIF_TAG_CREATE_DATE);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.EXIF_TAG_ISO);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.EXIF_TAG_APERTURE_VALUE);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.EXIF_TAG_BRIGHTNESS_VALUE);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
+                    populateImageMetadata(imageAttributes, jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE);
+
+                    /*
+                    // simple interface to GPS data
+                    TiffImageMetadata exifMetadata = jpegMetadata.getExif();
+                    if (null != exifMetadata)
+                    {
+                        TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
+                        if (null != gpsInfo)
+                        {
+                            String gpsDescription = gpsInfo.toString();
+                            double longitude = gpsInfo.getLongitudeAsDegreesEast();
+                            double latitude = gpsInfo.getLatitudeAsDegreesNorth();
+
+                            System.out.println("    " + "GPS Description: " + gpsDescription);
+                            System.out.println("    " + "GPS Longitude (Degrees East): " + longitude);
+                            System.out.println("    " + "GPS Latitude (Degrees North): " + latitude);
+                        }
+                    }
+
+                    // more specific example of how to manually access GPS values
+                    TiffField gpsLatitudeRefField = jpegMetadata
+                                    .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
+                    TiffField gpsLatitudeField = jpegMetadata
+                                    .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LATITUDE);
+                    TiffField gpsLongitudeRefField = jpegMetadata
+                                    .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
+                    TiffField gpsLongitudeField = jpegMetadata
+                                    .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LONGITUDE);
+                    if (gpsLatitudeRefField != null && gpsLatitudeField != null
+                                    && gpsLongitudeRefField != null
+                                    && gpsLongitudeField != null)
+                    {
+                            // all of these values are strings.
+                            String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
+                            RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField
+                                            .getValue());
+                            String gpsLongitudeRef = (String) gpsLongitudeRefField
+                                            .getValue();
+                            RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField
+                                            .getValue();
+
+                            RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
+                            RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
+                            RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
+
+                            RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
+                            RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
+                            RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
+
+                            // This will format the gps info like so:
+                            //
+                            // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
+                            // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
+
+                            System.out.println("    " + "GPS Latitude: "
+                                            + gpsLatitudeDegrees.toDisplayString() + " degrees, "
+                                            + gpsLatitudeMinutes.toDisplayString() + " minutes, "
+                                            + gpsLatitudeSeconds.toDisplayString() + " seconds "
+                                            + gpsLatitudeRef);
+                            System.out.println("    " + "GPS Longitude: "
+                                            + gpsLongitudeDegrees.toDisplayString() + " degrees, "
+                                            + gpsLongitudeMinutes.toDisplayString() + " minutes, "
+                                            + gpsLongitudeSeconds.toDisplayString() + " seconds "
+                                            + gpsLongitudeRef);
+
+                    }
+                    
+
+                    System.out.println();
+                    */
+                    ArrayList items = jpegMetadata.getItems();
+                    for (int i = 0; i < items.size(); i++)
+                    {
+
+                        Object item = items.get(i);
+                        if( item instanceof TiffImageMetadata.Item) {
+                            TiffImageMetadata.Item tiffItem = (TiffImageMetadata.Item) item;
+                            imageAttributes.add(new ImageMetadata(tiffItem.getKeyword(), tiffItem.getText()));
+                        }
+                        //System.out.println("    " + "item: " + item);
+                        //IImageMetadataItem item = (IImageMetadataItem) items.get(i);
+                        //imageAttributes.add(new ImageMetadata(item.))
+                    }                    
+            }
+
+            //===========
+            
+            /*
+            List<IImageMetadataItem> metadataItems = metadata.getItems();
+            for(IImageMetadataItem metadataItem : metadataItems) {
+                System.out.println(">>" + metadataItem);
+            }
+            */
+        } catch (ImageReadException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return imageAttributes;
+    }
+
+    private static void printTagValue(JpegImageMetadata jpegMetadata, TagInfo tagInfo) throws ImageReadException, IOException {
+        TiffField field = jpegMetadata.findEXIFValue(tagInfo);
+        if (field == null)
+            System.out.println(tagInfo.name + ": " + "Not Found.");
+        else
+            System.out.println(tagInfo.name + ": "
+                               + field.getValueDescription());
+    }
+    
+    /**
+     * Check if an eFix attribute is available and ad that to the metadata attribute list
+     * @param metadataAttributes
+     * @param jpegMetadata
+     * @param tagInfo
+     * @throws ImageReadException
+     * @throws IOException
+     */
+    private static void populateImageMetadata(List<ImageMetadata> metadataAttributes, JpegImageMetadata jpegMetadata, TagInfo tagInfo) throws ImageReadException, IOException {
+        ImageMetadata metadata = getTagValue(jpegMetadata, tagInfo);
+        if(metadata != null) {
+            metadataAttributes.add(metadata);
+        }
+    }
+    
+    /**
+     * Return Efix information wrapped into a ImageMetadata
+     * @param jpegMetadata Collection of the efix metadatas
+     * @param tagInfo the specific tagInfo being retrieved
+     * @return the eFix information wrapped into a ImageMetadata
+     * @throws ImageReadException
+     * @throws IOException
+     */
+    private static ImageMetadata getTagValue(JpegImageMetadata jpegMetadata, TagInfo tagInfo) throws ImageReadException, IOException {
+        ImageMetadata imageMetadata = null;
+        TiffField field = jpegMetadata.findEXIFValue(tagInfo);
+        if (field != null) {
+            imageMetadata = new ImageMetadata(tagInfo.name, field.getValueDescription());
+        }
+        
+        return imageMetadata;
+    }
+
+}

Propchange: incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/photark/trunk/photark/src/main/java/org/apache/photark/util/ImageMetadataScanner.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java?rev=912266&view=auto
==============================================================================
--- incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java (added)
+++ incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java Sun Feb 21 00:44:11 2010
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.photark.services.gallery.filesystem;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.Date;
+
+import org.apache.photark.Image;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ImageMetadataScannerTestCase {
+
+    @Test
+    public void testMetadataScanner() throws URISyntaxException {
+        File imageFile = new File(ImageMetadataScannerTestCase.class.getClassLoader().getResource("gallery-home/album-1/IMG_0735.jpg").toURI());
+        Image image = new Image(imageFile, new Date());
+        Assert.assertNotNull(image.getImageMetadata());
+        Assert.assertTrue(image.getImageMetadata().size() > 0);
+        /* for debug purpose
+        for(ImageMetadata metadata : image.getImageMetadata()) {
+            System.out.println(">>" + metadata.getKey() + "\t" + metadata.getValue());
+        }
+        */
+    }
+}

Propchange: incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/photark/trunk/photark/src/test/java/org/apache/photark/services/gallery/filesystem/ImageMetadataScannerTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/photark/trunk/photark/src/test/resources/gallery-home/album-1/IMG_0735.jpg
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/src/test/resources/gallery-home/album-1/IMG_0735.jpg?rev=912266&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/photark/trunk/photark/src/test/resources/gallery-home/album-1/IMG_0735.jpg
------------------------------------------------------------------------------
    svn:mime-type = image/jpeg

Added: incubator/photark/trunk/photark/src/test/resources/gallery-home/album-2/IMG_0735.jpg
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark/src/test/resources/gallery-home/album-2/IMG_0735.jpg?rev=912266&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/photark/trunk/photark/src/test/resources/gallery-home/album-2/IMG_0735.jpg
------------------------------------------------------------------------------
    svn:mime-type = image/jpeg