package ru.pp.maxcom; import org.w3c.dom.Element; import org.w3c.dom.Document; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Attr; import java.io.File; import com.drew.metadata.Metadata; import com.drew.metadata.Directory; import com.drew.metadata.MetadataException; import com.drew.metadata.exif.ExifDirectory; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.imaging.jpeg.JpegProcessingException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public class ExifXsl { public final static String EXIF_NS = "http://maxcom.pp.ru/photo/exif"; final Directory exifDirectory; public ExifXsl(String filename) throws JpegProcessingException { File jpegFile = new File(filename); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); exifDirectory = metadata.getDirectory(ExifDirectory.class); } public Element getExif() throws JpegProcessingException, ParserConfigurationException, MetadataException { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation().createDocument(EXIF_NS, "exif", null); Element camera = document.createElement("camera"); camera.appendChild(document.createTextNode(exifDirectory.getString(ExifDirectory.TAG_MODEL))); document.getDocumentElement().appendChild(camera); Element exposure = document.createElement("exposure"); exposure.setAttribute("aperture", exifDirectory.getString(ExifDirectory.TAG_FNUMBER)); exposure.setAttribute("speed", exifDirectory.getString(ExifDirectory.TAG_EXPOSURE_TIME)); exposure.setAttribute("iso", exifDirectory.getString(ExifDirectory.TAG_ISO_EQUIVALENT)); document.getDocumentElement().appendChild(exposure); Element date = document.createElement("date"); date.appendChild(document.createTextNode(exifDirectory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL).toString())); document.getDocumentElement().appendChild(date); Element lens = document.createElement("lens"); lens.appendChild(document.createTextNode(exifDirectory.getString(ExifDirectory.TAG_FOCAL_LENGTH))); document.getDocumentElement().appendChild(lens); return document.getDocumentElement(); } }