📄 extending.fm3.html
字号:
} else { processImageComplete(imageIndex); } // If the read was aborted, we still return a partially decoded image return theImage;}</pre></blockquote><blockquote><a name="1001105"><!-- --></a><i><b> Metadata</b></i></blockquote><blockquote><a name="1001097"><!-- --></a>The next set of methods in <code>MyFormatImageReader</code> deal with metadata. Because our hypothetical format only encodes a single image, we may ignore the concept of "stream" metadata, and use "image" metadata only:<p></blockquote><blockquote><pre> MyFormatMetadata metadata = null; // class defined below public IIOMetadata getStreamMetadata() throws IIOException { return null; } public IIOMetadata getImageMetadata(int imageIndex) throws IIOException { if (imageIndex != 0) { throw new IndexOutOfBoundsException("imageIndex != 0!"); } readMetadata(); return metadata; }</pre></blockquote><blockquote><a name="997606"><!-- --></a>The actual work is done by a format-specific method <code>readMetadata</code>, which for this format fills in the keyword/value pairs of the metadata object,<p></blockquote><blockquote><pre> public void readMetadata() throws IIOException { if (metadata != null) { return; } readHeader(); this.metadata = new MyFormatMetadata(); try { while (true) { String keyword = stream.readUTF(); stream.readUnsignedByte(); if (keyword.equals("END")) { break; } String value = stream.readUTF(); stream.readUnsignedByte(); metadata.keywords.add(keyword); metadata.values.add(value); } catch (IIOException e) { throw new IIOException("Exception reading metadata", e); } } }<code></code></pre></blockquote><blockquote><a name="1001093"><!-- --></a><i><b> <code>MyFormatMetadata</code></b></i></blockquote><blockquote><a name="997208"><!-- --></a>Finally, the various interfaces for extracting and editing metadata must be defined. We define a class called <code>MyFormatMetadata</code> that extends the <code>IIOMetadata</code> class, and additionally can store the keyword/value pairs that are allowed in the file format:<p></blockquote><blockquote><pre>package com.mycompany.imageio;import org.w3c.dom.*;import javax.xml.parsers.*; // Package name may change in JDK 1.4import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.imageio.metadata.IIOInvalidTreeException;import javax.imageio.metadata.IIOMetadata;import javax.imageio.metadata.IIOMetadataFormat;import javax.imageio.metadata.IIOMetadataNode;public class MyFormatMetadata extends IIOMetadata { static final boolean standardMetadataFormatSupported = false; static final String nativeMetadataFormatName = "com.mycompany.imageio.MyFormatMetadata_1.0"; static final String nativeMetadataFormatClassName = "com.mycompany.imageio.MyFormatMetadata"; static final String[] extraMetadataFormatNames = null; static final String[] extraMetadataFormatClassNames = null; // Keyword/value pairs List keywords = new ArrayList(); List values = new ArrayList();</pre></blockquote><blockquote><a name="998826"><!-- --></a>The first set of methods are common to most IIOMetadata implementations:<p></blockquote><blockquote><pre> public MyFormatMetadata() { super(standardMetadataFormatSupported, nativeMetadataFormatName, nativeMetadataFormatClassName, extraMetadataFormatNames, extraMetadataFormatClassNames); } public IIOMetadataFormat getMetadataFormat(String formatName) { if (!formatName.equals(nativeMetadataFormatName)) { throw new IllegalArgumentException("Bad format name!"); } return MyFormatMetadataFormat.getDefaultInstance(); }</pre></blockquote><blockquote><a name="998828"><!-- --></a>The most important method for reader plug-ins is <code>getAsTree</code>:<p></blockquote><blockquote><pre> public Node getAsTree(String formatName) { if (!formatName.equals(nativeMetadataFormatName)) { throw new IllegalArgumentException("Bad format name!"); } // Create a root node IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName); // Add a child to the root node for each keyword/value pair Iterator keywordIter = keywords.iterator(); Iterator valueIter = values.iterator(); while (keywordIter.hasNext()) { IIOMetadataNode node = new IIOMetadataNode("KeywordValuePair"); node.setAttribute("keyword", (String)keywordIter.next()); node.setAttribute("value", (String)valueIter.next()); root.appendChild(node); } return root; }</pre></blockquote><blockquote><a name="998830"><!-- --></a>For writer plug-ins, the ability to edit metadata values is obtained by implementing the <code>isReadOnly</code>, <code>reset</code>, and <code>mergeTree</code> methods:<p></blockquote><blockquote><pre> public boolean isReadOnly() { return false; } public void reset() { this.keywords = new ArrayList(); this.values = new ArrayList(); } public void mergeTree(String formatName, Node root) throws IIOInvalidTreeException { if (!formatName.equals(nativeMetadataFormatName)) { throw new IllegalArgumentException("Bad format name!"); } Node node = root; if (!node.getNodeName().equals(nativeMetadataFormatName)) { fatal(node, "Root must be " + nativeMetadataFormatName); } node = node.getFirstChild(); while (node != null) { if (!node.getNodeName().equals("KeywordValuePair")) { fatal(node, "Node name not KeywordValuePair!"); } NamedNodeMap attributes = node.getAttributes(); Node keywordNode = attributes.getNamedItem("keyword"); Node valueNode = attributes.getNamedItem("value"); if (keywordNode == null || valueNode == null) { fatal(node, "Keyword or value missing!"); } // Store keyword and value keywords.add((String)keywordNode.getNodeValue()); values.add((String)valueNode.getNodeValue()); // Move to the next sibling node = node.getNextSibling(); } } private void fatal(Node node, String reason) throws IIOInvalidTreeException { throw new IIOInvalidTreeException(reason, node); }}</pre></blockquote><blockquote><a name="996881"><!-- --></a><i><b> <code>MyFormatMetadataFormat</code></b></i></blockquote><blockquote><a name="999065"><!-- --></a>The tree structure of the metadata may be described using the IIOMetadataFormat interface. An implementation class, IIOMetadataFormatImpl, takes care of maintaining the "database" of information about elements, their attributes, and the parent-child relationships between them:<p></blockquote><blockquote><pre>package com.mycompany.imageio;import javax.imageio.ImageTypeSpecifier;import javax.imageio.metadata.IIOMetadataFormatImpl;public class MyFormatMetadataFormat extends IIOMetadataFormatImpl { // Create a single instance of this class (singleton pattern) private static MyFormatMetadataFormat defaultInstance = new MyFormatMetadataFormat(); // Make constructor private to enforce the singleton pattern private MyFormatMetadataFormat() { // Set the name of the root node // The root node has a single child node type that may repeat super("com.mycompany.imageio.MyFormatMetadata_1.0", CHILD_POLICY_REPEAT); // Set up the "KeywordValuePair" node, which has no children addElement("KeywordValuePair", "com.mycompany.imageio.MyFormatMetadata_1.0", CHILD_POLICY_EMPTY); // Set up attribute "keyword" which is a String that is required // and has no default value addAttribute("KeywordValuePair", "keyword", DATATYPE_STRING, true, null); // Set up attribute "value" which is a String that is required // and has no default value addAttribute("KeywordValuePair", "value", DATATYPE_STRING, true, null); } // Check for legal element name public boolean canNodeAppear(String elementName, ImageTypeSpecifier imageType) { return elementName.equals("KeywordValuePair"); } // Return the singleton instance public static MyFormatMetadataFormat getDefaultInstance() { return defaultInstance; }}</pre></blockquote><blockquote><a name="999124"><!-- --></a><p></blockquote><br><hr><!-- Bug in Communicator w/font: NavBar text disappears for Times 14pt pref. --><!-- font size="-1" --> <a href="imageio_guideTOC.fm.html" tppabs="http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/imageio_guideTOC.fm.html">CONTENTS</a> | <a href="extending.fm2.html" tppabs="http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/extending.fm2.html">PREV</a> | <a href="extending.fm4.html" tppabs="http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/extending.fm4.html">NEXT</a> <!-- | <a href="copyright.fm.html">INDEX</a> --><!-- /font --><hr><font size="-1"><i><A HREF="copyright.fm.html" tppabs="http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/copyright.fm.html">Copyright</a> © 2001 Sun Microsystems, Inc. All Rights Reserved.</i></font><!-- This HTML file was created with Quadralay WebWorks Publisher 3.5.0 --><!-- by Suzette Pelouch --><!-- Last updated: Fri Apr 27 11:23:03 2001 --> </body><script language="JavaScript" src="s_code_remote.js" tppabs="http://java.sun.com/js/omi/jsc/s_code_remote.js"></script></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -