⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extending.fm3.html

📁 IMAGEIO package 的使用说明jdk1.4新出的 包。有效改进图片读写方式。
💻 HTML
📖 第 1 页 / 共 3 页
字号:
	} 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 &#34;stream&#34; metadata, and use &#34;image&#34; 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(&#34;imageIndex != 0!&#34;);		}		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(&#34;END&#34;)) {					break;				}				String value = stream.readUTF();				stream.readUnsignedByte();				metadata.keywords.add(keyword);				metadata.values.add(value);			} catch (IIOException e) {				throw new IIOException(&#34;Exception reading metadata&#34;,				                       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(&#34;KeywordValuePair&#34;);			node.setAttribute(&#34;keyword&#34;, (String)keywordIter.next());			node.setAttribute(&#34;value&#34;, (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, &#34;Root must be &#34; + nativeMetadataFormatName);		}		node = node.getFirstChild();		while (node != null) {			if (!node.getNodeName().equals(&#34;KeywordValuePair&#34;)) {				fatal(node, &#34;Node name not KeywordValuePair!&#34;);			}			NamedNodeMap attributes = node.getAttributes();			Node keywordNode = attributes.getNamedItem(&#34;keyword&#34;);			Node valueNode = attributes.getNamedItem(&#34;value&#34;);			if (keywordNode == null || valueNode == null) {				fatal(node, &#34;Keyword or value missing!&#34;);			}			// 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 &#34;database&#34; 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(&#34;com.mycompany.imageio.MyFormatMetadata_1.0&#34;,		      CHILD_POLICY_REPEAT);		// Set up the &#34;KeywordValuePair&#34; node, which has no children		addElement(&#34;KeywordValuePair&#34;,		           &#34;com.mycompany.imageio.MyFormatMetadata_1.0&#34;,		           CHILD_POLICY_EMPTY);		// Set up attribute &#34;keyword&#34; which is a String that is required		// and has no default value		addAttribute(&#34;KeywordValuePair&#34;, &#34;keyword&#34;, DATATYPE_STRING,		             true, null);		// Set up attribute &#34;value&#34; which is a String that is required		// and has no default value		addAttribute(&#34;KeywordValuePair&#34;, &#34;value&#34;, DATATYPE_STRING,		             true, null);	}	// Check for legal element name	public boolean canNodeAppear(String elementName,	                             ImageTypeSpecifier imageType) {		return elementName.equals(&#34;KeywordValuePair&#34;);	}	// 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> &#169 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 + -