Skip to Content.
Sympa Menu

idok-commit - [idok-commit] idok commit r6 - trunk/java/ch/idok/dmsd/impl/extractor/jpeg

idok-commit AT lists.psi.ch

Subject: Commit emails of the iDok project

List archive

[idok-commit] idok commit r6 - trunk/java/ch/idok/dmsd/impl/extractor/jpeg


Chronological Thread 
  • From: "AFS account Stadler Hans Christian" <stadler_h AT savannah.psi.ch>
  • To: idok-commit AT lists.psi.ch
  • Subject: [idok-commit] idok commit r6 - trunk/java/ch/idok/dmsd/impl/extractor/jpeg
  • Date: Thu, 28 Feb 2008 15:28:52 +0100
  • List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
  • List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>

Author: stadler_h
Date: Thu Feb 28 15:28:52 2008
New Revision: 6

Log:
Remove reliance on Suns image io readers.

Modified:
trunk/java/ch/idok/dmsd/impl/extractor/jpeg/JpegExtractorFactory.java

Modified:
trunk/java/ch/idok/dmsd/impl/extractor/jpeg/JpegExtractorFactory.java
==============================================================================
--- trunk/java/ch/idok/dmsd/impl/extractor/jpeg/JpegExtractorFactory.java
(original)
+++ trunk/java/ch/idok/dmsd/impl/extractor/jpeg/JpegExtractorFactory.java
Thu Feb 28 15:28:52 2008
@@ -25,31 +25,22 @@
*/

import java.io.ByteArrayInputStream;
-import java.io.IOException;
+import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.metadata.IIOMetadata;
-import javax.imageio.metadata.IIOMetadataNode;
-import javax.imageio.stream.MemoryCacheImageInputStream;
-
-import org.w3c.dom.Node;
-
import ch.idok.common.errorhandling.DmsException;
import ch.idok.common.errorhandling.ErrorType;
import ch.idok.dmsd.config.Config;
import ch.idok.dmsd.indexer.ContentExtractor;
import ch.idok.dmsd.indexer.ContentExtractorFactory;

+import com.drew.imaging.jpeg.JpegMetadataReader;
+import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
+import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
-import com.drew.metadata.exif.ExifDirectory;
-import com.drew.metadata.exif.ExifReader;
-import com.drew.metadata.iptc.IptcDirectory;
-import com.drew.metadata.iptc.IptcReader;

/**
* @brief This factory class is able to create extractor objects that can
handle
@@ -63,12 +54,6 @@
*/
private final String mimeType = "image/jpeg";

- /** @brief EXIF metadata tag. */
- public static final int EXIF = 0xE1;
-
- /** @brief IPTC metadata tag. */
- public static final int IPTC = 0xED;
-
/**
* @brief The content extractor class for plain text.
*/
@@ -96,39 +81,38 @@

/** @see ContentExtractor.getMetaData() */
public Map<String, String> getMetaData() throws DmsException {
+ InputStream bais = null;
try {
if (raw != null) {
- MemoryCacheImageInputStream iis = new
MemoryCacheImageInputStream(
- new ByteArrayInputStream(raw));
- Iterator<ImageReader> iterator = ImageIO
- .getImageReadersByMIMEType(mimeType);
- ImageReader reader = null;
- while (iterator.hasNext()) {
- reader = iterator.next();
- if
(reader.getOriginatingProvider().canDecodeInput(iis)) {
- reader.setInput(iis);
- break;
+ bais = new ByteArrayInputStream(raw);
+ Metadata metadata =
JpegMetadataReader.readMetadata(bais);
+ Iterator<?> dirIterator =
metadata.getDirectoryIterator();
+ while (dirIterator.hasNext()) {
+ Directory dir = (Directory) dirIterator.next();
+ Iterator<?> tagIterator = dir.getTagIterator();
+ while (tagIterator.hasNext()) {
+ Tag tag = (Tag) tagIterator.next();
+ String desc = null;
+ try {
+ desc = tag.getDescription();
+ } catch (MetadataException ex) {
+ /* ignore */
+ }
+ meta.put(tag.getTagName(),
+ desc == null ? "(unknown tag)" : desc);
}
}
- if (!iterator.hasNext())
- DmsException.throwIt(ErrorType.DOC_HANDLING, this,
- "Cannot extract EXIF data",
- "No appropriate image reader was found");
- IIOMetadata metadata = reader.getImageMetadata(0);
- Node metaNode = metadata.getAsTree(metadata
- .getNativeMetadataFormatName());
- getMetadata(meta, metaNode, EXIF);
- getMetadata(meta, metaNode, IPTC);
- raw = null;
}
- } catch (IOException ex) {
- DmsException.throwIt(ErrorType.FILE_ACCESS, this,
- "Cannot read from jpeg image",
- "method canDecodeInput failed");
- } catch (MetadataException ex) {
- DmsException.throwIt(ErrorType.READ_DOC, this,
- "Reading jpeg metadata failed.", ex
+ } catch (JpegProcessingException ex) {
+ DmsException.throwIt(ErrorType.CONTENT_EXTRACTION, this,
+ "Cannot extract exif data from jpeg image", ex
.getLocalizedMessage(), ex);
+ } finally {
+ raw = null;
+ try {
+ bais.close();
+ } catch (Throwable th) { /* ignore */
+ }
}
return meta;
}
@@ -138,73 +122,6 @@
raw = null;
meta = null;
}
-
- /**
- * @brief Retrieve meta data directory
- * @param node
- * The image data node
- * @param tag
- * The metadata tag
- * @return Directory for the metadata
- * @throws MetadataException
- * If the metadata cannot be read from the picture
- * @throws DmsException
- */
- private Directory getDirectory(Node node, int tag)
- throws MetadataException, DmsException {
- if (node.getNodeName() == "unknown") {
- if (Integer.parseInt(node.getAttributes().getNamedItem(
- "MarkerTag").getNodeValue()) == EXIF) {
- byte[] data = (byte[]) ((IIOMetadataNode) node)
- .getUserObject();
- if (tag == EXIF)
- return new ExifReader(data).extract().getDirectory(
- ExifDirectory.class);
- else if (tag == IPTC)
- return new IptcReader(data).extract().getDirectory(
- IptcDirectory.class);
- else
- throw new DmsException(ErrorType.INTERNAL, this,
- "Internat bug detected", "Wrong metadata
tag: "
- + tag);
- }
- }
- Node child = node.getFirstChild();
- while (child != null) {
- Directory directory = getDirectory(child, tag);
- if (directory != null)
- return directory;
- child = child.getNextSibling();
- }
- return null;
- }
-
- /**
- * @brief Add metadata in the image to the matadata map
- * @param metaData
- * The metadata map
- * @param node
- * The image node
- * @param tag
- * The metadata tag
- * @throws MetadataException
- * If the metadata cannot be read from the picture
- * @throws DmsException
- * If an internal error occurred
- */
- private void getMetadata(Map<String, String> metaData, Node node,
- int tag) throws MetadataException, DmsException {
- Directory directory;
- directory = getDirectory(node, tag);
- if (directory == null)
- return;
- Iterator<?> iter = directory.getTagIterator();
- while (iter.hasNext()) {
- Tag metaTag = (Tag) iter.next();
- metaData.put("auto:" + metaTag.getTagName(), metaTag
- .getDescription());
- }
- }
}

/** @brief Constructor. */



  • [idok-commit] idok commit r6 - trunk/java/ch/idok/dmsd/impl/extractor/jpeg, AFS account Stadler Hans Christian, 02/28/2008

Archive powered by MHonArc 2.6.19.

Top of Page