xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r813020 [8/8] - in /websites/staging/xmlgraphics/trunk/content: ./ batik/ batik/demo/ batik/dev/ batik/tools/ batik/using/ batik/using/scripting/
Date Sun, 15 Apr 2012 05:46:24 GMT
Added: websites/staging/xmlgraphics/trunk/content/batik/using/transcoder.html
==============================================================================
--- websites/staging/xmlgraphics/trunk/content/batik/using/transcoder.html (added)
+++ websites/staging/xmlgraphics/trunk/content/batik/using/transcoder.html Sun Apr 15 05:46:22 2012
@@ -0,0 +1,348 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html lang="en">
+  <head>
+    <title>Transcoder API</title>
+
+    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
+    <meta property="og:image" content="http://www.apache.org/images/asf_logo.gif" />
+<!--
+    <link rel="stylesheet" type="text/css" media="screen" href="http://www.apache.org/css/style.css">
+    <link rel="stylesheet" type="text/css" media="screen" href="http://www.apache.org/css/code.css">
+-->
+    <link href="/css/xmlgraphics.css" rel="stylesheet" type="text/css">
+
+    </style>
+
+    
+
+    
+    
+  </head>
+
+  <body>
+	  <div id="banner">&nbsp;
+	  </div>
+
+	  <div id="navigation">
+	  <h1 id="xml-graphics">XML Graphics</h1>
+<ul>
+<li><a href="/">Overview</a></li>
+<li><a href="/team.html">Who We Are</a></li>
+<li><a href="/legal.html">Legal Stuff</a></li>
+<li><a href="/charter.html">Project Charter</a></li>
+<li><a href="/mail.html">Mailing Lists</a></li>
+<li><a href="/repo.html">Code Repositories</a></li>
+<li><a href="http://www.apache.org/foundation/sponsorship.html">ASF Sponsorship Program</a></li>
+<li><a href="http://www.apache.org/foundation/thanks.html">ASF Thanks</a></li>
+</ul>
+<h1 id="subprojects">Subprojects</h1>
+<ul>
+<li><a href="/batik/">Batik</a></li>
+<li><a href="/fop/">FOP</a></li>
+<li><a href="/commons/">Commons</a></li>
+</ul>
+<form name="search" id="search" action="http://www.google.com/search" method="get">
+  <input value="xmlgraphics.apache.org" name="sitesearch" type="hidden"/>
+  <input type="text" name="q" id="query" /><br />
+  <input type="submit" id="submit" value="Search" />
+</form>
+	  </div>
+	
+	  <div id="bannertext">
+        <a href="/"><img src="/images/apache-xml-graphics.gif" alt="The Apache XML Graphics Project" width="220" heigh="51" /></a>
+        <h1>Transcoder API</h1>
+      </div>
+        <p><a href="/">Home</a>&nbsp;&raquo&nbsp;<a href="/batik/">Batik</a>&nbsp;&raquo&nbsp;<a href="/batik/using/">Using</a></p>
+      </div>
+      <div id="content" class="grid_16"><div class="section-content"><p>The goal of the transcoder API (package <code>org.apache.batik.transcoder</code> ) is to provide a generic API for transcoding an input to an output. First, this document explains the basic transcoder API that <a href="../javadoc/org/apache/batik/transcoder/Transcoder.html">Transcoder</a> , <a href="../javadoc/org/apache/batik/transcoder/TranscoderInput.html">TranscoderInput</a> and <a href="../javadoc/org/apache/batik/transcoder/TranscoderOutput.html">TranscoderOutput</a> define, and thus all transcoders have in common. Next, it describes how to use the image transcoder API (package <code>org.apache.batik.transcoder.image</code> ), which lets you rasterize an SVG document fragment to a raster image such as JPEG, PNG or TIFF.</p>
+<h1 id="transcoderAPI">The transcoder API</h1>
+<p>The <code>org.apache.batik.transcoder</code> package defines 5 major classes:</p>
+<dl>
+<dt>Transcoder</dt>
+<dd>The <a href="../javadoc/org/apache/batik/transcoder/Transcoder.html">Transcoder</a> interface defines the interface that all transcoders implement. You can transcode a specific input using a specific output by invoking the <code>transcode</code> method. Although there is no assumption on the input and output format, a specific transcoder may or may not support a particular type of input or output. For example, the image transcoders accept an SVG <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</a> , a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.html">Reader</a> , an <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html">InputStream</a> , or a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">URI</a> as an input but only supports a byte stream for the output.</dd>
+<dt>TranscoderInput</dt>
+<dd>The <a href="../javadoc/org/apache/batik/transcoder/TranscoderInput.html">TranscoderInput</a> interface defines the input of a transcoder. There are various ways to create an input and the most common are already part of the API. The default implementation lets you create an input using a <code>org.w3c.dom.Document</code> , a <code>Reader</code> , an <code>InputStream</code> , a <code>org.xml.sax.XMLReader</code> , or a <code>URI</code> .</dd>
+<dt>TranscoderOutput</dt>
+<dd>The <a href="../javadoc/org/apache/batik/transcoder/TranscoderOutput.html">TranscoderOutput</a> interface defines the output of a transcoder. There are various ways to create an output and the most commons are already part of the API. The default implementation lets you create an output using a <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</a> , a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Writer.html">Writer</a> , an <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html">OutputStream</a> , a <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/XMLFilter.html">XMLFilter</a> , or a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">URI</a> .</dd>
+<dt>TranscodingHints</dt>
+<dd>The <a href="../javadoc/org/apache/batik/transcoder/TranscodingHints.html">TranscodingHints</a> class contains different hints that can be used to control the various options or parameters of a transcoder. Each transcoder provides its own set of hints. A hint is specified by (key, value) pair. For example, the <a href="../javadoc/org/apache/batik/transcoder/image/JPEGTranscoder.html">JPEGTranscoder</a> provides a hint to control the encoding quality.</dd>
+<dt>ErrorHandler</dt>
+<dd>The <a href="../javadoc/org/apache/batik/transcoder/ErrorHandler.html">ErrorHandler</a> class provides a way to get any errors or warnings that might occur while transcoding. A default implementation is provided but you can, for example, implement your own handler that displays a dialog instead of stack trace.</dd>
+</dl>
+<h1 id="howtousetranscoderAPI">How to use the image transcoder API</h1>
+<p>The <code>org.apache.batik.transcoder.image</code> package provides an easy way to transcode an SVG document to a raster image such as JPEG, PNG or TIFF. Additional raster image formats can be added by subclassing the <code>ImageTranscoder</code> class and implementing the <code>writeImage</code> method. Although, in next sections, the examples will use the JPEG transcoder, the PNG transcoder works the same way.</p>
+<h2 id="createImage">Creating an image</h2>
+<p>The following example, using the <code>JPEGTranscoder</code> , shows how to transform an SVG document to a JPEG image.
+import java.io.*;</p>
+<p>import org.apache.batik.transcoder.image.JPEGTranscoder;
+import org.apache.batik.transcoder.TranscoderInput;
+import org.apache.batik.transcoder.TranscoderOutput;</p>
+<p>public class SaveAsJPEG {</p>
+<div class="codehilite"><pre><span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="n">throws</span> <span class="n">Exception</span> <span class="p">{</span>
+
+    <span class="sr">//</span> <span class="n">Create</span> <span class="n">a</span> <span class="n">JPEG</span> <span class="n">transcoder</span>
+    <span class="n">JPEGTranscoder</span> <span class="n">t</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JPEGTranscoder</span><span class="p">();</span>
+
+    <span class="sr">//</span> <span class="n">Set</span> <span class="n">the</span> <span class="n">transcoding</span> <span class="n">hints</span><span class="o">.</span>
+    <span class="n">t</span><span class="o">.</span><span class="n">addTranscodingHint</span><span class="p">(</span><span class="n">JPEGTranscoder</span><span class="o">.</span><span class="n">KEY_QUALITY</span><span class="p">,</span>
+                         <span class="k">new</span> <span class="n">Float</span><span class="p">(</span><span class="mf">.8</span><span class="p">));</span>
+
+    <span class="sr">//</span> <span class="n">Create</span> <span class="n">the</span> <span class="n">transcoder</span> <span class="n">input</span><span class="o">.</span>
+    <span class="n">String</span> <span class="n">svgURI</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">toURL</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">();</span>
+    <span class="n">TranscoderInput</span> <span class="n">input</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TranscoderInput</span><span class="p">(</span><span class="n">svgURI</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Create</span> <span class="n">the</span> <span class="n">transcoder</span> <span class="n">output</span><span class="o">.</span>
+    <span class="n">OutputStream</span> <span class="n">ostream</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileOutputStream</span><span class="p">(</span><span class="s">&quot;out.jpg&quot;</span><span class="p">);</span>
+    <span class="n">TranscoderOutput</span> <span class="n">output</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TranscoderOutput</span><span class="p">(</span><span class="n">ostream</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Save</span> <span class="n">the</span> <span class="n">image</span><span class="o">.</span>
+    <span class="n">t</span><span class="o">.</span><span class="n">transcode</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="n">output</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Flush</span> <span class="ow">and</span> <span class="nb">close</span> <span class="n">the</span> <span class="n">stream</span><span class="o">.</span>
+    <span class="n">ostream</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
+    <span class="n">ostream</span><span class="o">.</span><span class="nb">close</span><span class="p">();</span>
+    <span class="n">System</span><span class="o">.</span><span class="nb">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
+<span class="p">}</span>
+</pre></div>
+
+
+<p>}
+The code creates a <code>JPEGTranscoder</code> and sets a transcoding hint. The hint indicates the encoding quality. Then, an input and an output are created. The input is created using the first command line argument, which should be a URI. The output is a byte stream for a file called <code>out.jpg</code> . Finally, the <code>transcode</code> method is invoked and the byte stream is closed.</p>
+<p>Although not shown above, the program might have specified additional hints to indicate a user stylesheet, the preferred language of the document or the background color.</p>
+<p>To try this program:</p>
+<ol>
+<li>
+<p>Save the above program as <code>SaveAsJPEG.java</code> .</p>
+</li>
+<li>
+<p>Compile the program.</p>
+</li>
+<li>
+<p>Choose an SVG file to transcode, and perform the transcoding by running:
+java SaveAsJPEG filename.svg</p>
+</li>
+<li>
+<p>Inspect the output file <code>out.jpg</code> .</p>
+</li>
+</ol>
+<h2 id="defineSizeOfImage">Defining the size of the image</h2>
+<p>By adding the following line of code to the previous example, you will specify the raster image size (in pixels). The transcoding hint <code>KEY_WIDTH</code> lets you specify the raster image width. If the raster image height is not provided (using the <code>KEY_HEIGHT</code> ), the transcoder will compute the raster image height by keeping the aspect ratio of the SVG document.
+t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Float(100));
+The transcoder will have the same behavior if you specify the <code>KEY_HEIGHT</code> without initializing the <code>KEY_WIDTH</code> . In all cases (even if both keys are provided), the transcoder will preserve the apsect ratio of the SVG document.</p>
+<h2 id="selectAreaOfIntrest">Selecting an area of interest</h2>
+<p>The image transcoder lets you specify an area of interest (that is, a part of the SVG document). The key <code>KEY_AOI</code> allows you to select the region of the SVG document to render. The value of this key must be a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Rectangle.html">java.awt.Rectangle</a> specified in pixels, using the coordinate space of the SVG document. The following example shows how you can split an SVG document into 4 tiles.
+import java.io.<em>;
+import java.awt.</em>;</p>
+<p>import org.apache.batik.transcoder.image.JPEGTranscoder;
+import org.apache.batik.transcoder.TranscoderInput;
+import org.apache.batik.transcoder.TranscoderOutput;</p>
+<p>public class SaveAsJPEGTiles {</p>
+<div class="codehilite"><pre><span class="n">JPEGTranscoder</span> <span class="n">trans</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JPEGTranscoder</span><span class="p">();</span>
+
+<span class="n">public</span> <span class="n">SaveAsJPEGTiles</span><span class="p">()</span> <span class="p">{</span>
+    <span class="n">trans</span><span class="o">.</span><span class="n">addTranscodingHint</span><span class="p">(</span><span class="n">JPEGTranscoder</span><span class="o">.</span><span class="n">KEY_QUALITY</span><span class="p">,</span>
+                             <span class="k">new</span> <span class="n">Float</span><span class="p">(</span><span class="mf">.8</span><span class="p">));</span>
+<span class="p">}</span>
+
+<span class="n">public</span> <span class="n">void</span> <span class="n">tile</span><span class="p">(</span><span class="n">String</span> <span class="n">inputFilename</span><span class="p">,</span>
+                 <span class="n">String</span> <span class="n">outputFilename</span><span class="p">,</span>
+                 <span class="n">Rectangle</span> <span class="n">aoi</span><span class="p">)</span> <span class="n">throws</span> <span class="n">Exception</span> <span class="p">{</span>
+    <span class="sr">//</span> <span class="n">Set</span> <span class="n">hints</span> <span class="n">to</span> <span class="n">indicate</span> <span class="n">the</span> <span class="n">dimensions</span> <span class="n">of</span> <span class="n">the</span> <span class="n">output</span> <span class="n">image</span>
+    <span class="sr">//</span> <span class="ow">and</span> <span class="n">the</span> <span class="n">input</span> <span class="n">area</span> <span class="n">of</span> <span class="n">interest</span><span class="o">.</span>
+    <span class="n">trans</span><span class="o">.</span><span class="n">addTranscodingHint</span><span class="p">(</span><span class="n">JPEGTranscoder</span><span class="o">.</span><span class="n">KEY_WIDTH</span><span class="p">,</span>
+                             <span class="k">new</span> <span class="n">Float</span><span class="p">(</span><span class="n">aoi</span><span class="o">.</span><span class="n">width</span><span class="p">));</span><span class="n">trans</span><span class="o">.</span><span class="n">addTranscodingHint</span><span class="p">(</span><span class="n">JPEGTranscoder</span><span class="o">.</span><span class="n">KEY_HEIGHT</span><span class="p">,</span>
+                             <span class="k">new</span> <span class="n">Float</span><span class="p">(</span><span class="n">aoi</span><span class="o">.</span><span class="n">height</span><span class="p">));</span><span class="n">trans</span><span class="o">.</span><span class="n">addTranscodingHint</span><span class="p">(</span><span class="n">JPEGTranscoder</span><span class="o">.</span><span class="n">KEY_AOI</span><span class="p">,</span> <span class="n">aoi</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Transcode</span> <span class="n">the</span> <span class="n">file</span><span class="o">.</span>
+    <span class="n">String</span> <span class="n">svgURI</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="p">(</span><span class="n">inputFilename</span><span class="p">)</span><span class="o">.</span><span class="n">toURL</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">();</span>
+    <span class="n">TranscoderInput</span> <span class="n">input</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TranscoderInput</span><span class="p">(</span><span class="n">svgURI</span><span class="p">);</span>
+    <span class="n">OutputStream</span> <span class="n">ostream</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileOutputStream</span><span class="p">(</span><span class="n">outputFilename</span><span class="p">);</span>
+    <span class="n">TranscoderOutput</span> <span class="n">output</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TranscoderOutput</span><span class="p">(</span><span class="n">ostream</span><span class="p">);</span>
+    <span class="n">trans</span><span class="o">.</span><span class="n">transcode</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="n">output</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Flush</span> <span class="ow">and</span> <span class="nb">close</span> <span class="n">the</span> <span class="n">output</span><span class="o">.</span>
+    <span class="n">ostream</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
+    <span class="n">ostream</span><span class="o">.</span><span class="nb">close</span><span class="p">();</span>
+<span class="p">}</span>
+
+<span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="n">throws</span> <span class="n">Exception</span> <span class="p">{</span>
+    <span class="sr">//</span> <span class="n">Rasterize</span> <span class="n">the</span> <span class="n">samples</span><span class="o">/</span><span class="n">anne</span><span class="o">.</span><span class="n">svg</span> <span class="n">document</span> <span class="ow">and</span> <span class="n">save</span> <span class="n">it</span>
+    <span class="sr">//</span> <span class="n">as</span> <span class="n">four</span> <span class="n">tiles</span><span class="o">.</span>
+    <span class="n">SaveAsJPEGTiles</span> <span class="n">p</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SaveAsJPEGTiles</span><span class="p">();</span>
+    <span class="n">String</span> <span class="n">in</span> <span class="o">=</span> <span class="s">&quot;samples/anne.svg&quot;</span><span class="p">;</span>
+    <span class="nb">int</span> <span class="n">documentWidth</span> <span class="o">=</span> <span class="mi">450</span><span class="p">;</span>
+    <span class="nb">int</span> <span class="n">documentHeight</span> <span class="o">=</span> <span class="mi">500</span><span class="p">;</span>
+    <span class="nb">int</span> <span class="n">dw2</span> <span class="o">=</span> <span class="n">documentWidth</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span>
+    <span class="nb">int</span> <span class="n">dh2</span> <span class="o">=</span> <span class="n">documentHeight</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span>
+    <span class="n">p</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">in</span><span class="p">,</span> <span class="s">&quot;tileTopLeft.jpg&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">Rectangle</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">dw2</span><span class="p">,</span> <span class="n">dh2</span><span class="p">));</span>
+    <span class="n">p</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">in</span><span class="p">,</span> <span class="s">&quot;tileTopRight.jpg&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">Rectangle</span><span class="p">(</span><span class="n">dw2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">dw2</span><span class="p">,</span> <span class="n">dh2</span><span class="p">));</span>
+    <span class="n">p</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">in</span><span class="p">,</span> <span class="s">&quot;tileBottomLeft.jpg&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">Rectangle</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dh2</span><span class="p">,</span> <span class="n">dw2</span><span class="p">,</span> <span class="n">dh2</span><span class="p">));</span>
+    <span class="n">p</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">in</span><span class="p">,</span> <span class="s">&quot;tileBottomRight.jpg&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">Rectangle</span><span class="p">(</span><span class="n">dw2</span><span class="p">,</span> <span class="n">dh2</span><span class="p">,</span> <span class="n">dw2</span><span class="p">,</span> <span class="n">dh2</span><span class="p">));</span>
+    <span class="n">System</span><span class="o">.</span><span class="nb">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
+<span class="p">}</span>
+</pre></div>
+
+
+<p>}
+This code splits the same document <code>anne.svg</code> into four tiles of the same size. Considering the document and its original size, we can determine four regions. Then we rasterize each region using the <code>KEY_AOI</code> key. Note that we also specify the image width and height to be the same as the area of interest width and height (so we keep a 1:1 zoom factor). You can of course use different values for the <code>KEY_WIDTH</code> and <code>KEY_HEIGHT</code> , that do not match the dimensions of the <code>KEY_AOI</code> key. In that case, first the area of interest will determine which part of the SVG document has to be rendered, then that part could be zoomed in or out depending on the specified raster image size.</p>
+<p>To try this program:</p>
+<ol>
+<li>
+<p>Save the above program as <code>SaveAsJPEGTiles.java</code> .</p>
+</li>
+<li>
+<p>Compile the program.</p>
+</li>
+<li>
+<p>Copy the <code>samples/anne.svg</code> document from the Batik distribution into the current directory.</p>
+</li>
+<li>
+<p>Run the program by typing:
+java SaveAsJPEGTiles</p>
+</li>
+<li>
+<p>Inspect the output files <code>tileTopRight.jpg</code> , <code>tileTopRight.jpg</code> , <code>tileBottomRight.jpg</code> and <code>tileBottomLeft.jpg</code> .</p>
+</li>
+</ol>
+<h2 id="otherTransHints">Other transcoding hints</h2>
+<p>The <code>ImageTranscoder</code> provides additional <code>TranscodingHints</code> that let you customize the generated images.</p>
+<dl>
+<dt><code>ImageTranscoder.KEY_MEDIA</code></dt>
+<dd>This hint lets you choose the CSS medium to use. The author of the SVG document to transcode can control CSS media using the <a href="http://www.w3.org/TR/REC-CSS2/media.html">CSS media rule</a> . For example:
+trans.addTranscodingHint(ImageTranscoder.KEY_MEDIA, "print");</dd>
+<dt><code>ImageTranscoder.KEY_ALTERNATE_STYLESHEET</code></dt>
+<dd>This hint lets you choose an alternate stylesheet the author of the SVG document to transcode might have provided using the <a href="http://www.w3.org/TR/xml-stylesheet/">xml-stylesheet</a> processing instruction. For exxample:
+trans.addTranscodingHint(ImageTranscoder.KEY_ALTERNATE_STYLESHEET,
+                         alternateStylesheetName);</dd>
+<dt><code>ImageTranscoder.KEY_USER_STYLESHEET_URI</code></dt>
+<dd>This hint lets you use a user stylesheet. A user stylesheet can override some styles of the SVG document to transcode. For example:
+trans.addTranscodingHint(ImageTranscoder.KEY_USER_STYLESHEET_URI, "http://...");</dd>
+<dt><code>ImageTranscoder.KEY_PIXEL_TO_MM</code></dt>
+<dd>This hint lets you use the pixel to millimeter conversion factor. This factor is used to determine how units are converted into pixels. For example:
+// 96dpi
+trans.addTranscodingHint(ImageTranscoder.KEY_PIXEL_TO_MM, new Float(0.2645833f));
+or
+// 72pi
+trans.addTranscodingHint(ImageTranscoder.KEY_PIXEL_TO_MM, new Float(0.3528f));</dd>
+<dt><code>ImageTranscoder.KEY_BACKGROUND_COLOR</code></dt>
+<dd>This hint lets you choose a background color. For example:
+trans.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);</dd>
+</dl>
+<h2 id="genImagefromSVGDOM">Generating an image from an SVG DOM tree</h2>
+<p>The following code creates and saves an SVG DOM tree to a raster image.
+import java.io.*;</p>
+<p>import org.apache.batik.transcoder.image.JPEGTranscoder;
+import org.apache.batik.transcoder.TranscoderInput;
+import org.apache.batik.transcoder.TranscoderOutput;
+import org.apache.batik.dom.svg.SVGDOMImplementation;</p>
+<p>import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.DOMImplementation;</p>
+<p>public class DOMRasterizer {</p>
+<div class="codehilite"><pre><span class="n">public</span> <span class="n">Document</span> <span class="n">createDocument</span><span class="p">()</span> <span class="p">{</span>
+
+    <span class="sr">//</span> <span class="n">Create</span> <span class="n">a</span> <span class="k">new</span> <span class="n">document</span><span class="o">.</span>
+    <span class="n">DOMImplementation</span> <span class="n">impl</span> <span class="o">=</span> <span class="n">SVGDOMImplementation</span><span class="o">.</span><span class="n">getDOMImplementation</span><span class="p">();</span>
+    <span class="n">String</span> <span class="n">svgNS</span> <span class="o">=</span> <span class="n">SVGDOMImplementation</span><span class="o">.</span><span class="n">SVG_NAMESPACE_URI</span><span class="p">;</span>
+    <span class="n">Document</span> <span class="n">document</span> <span class="o">=</span>
+        <span class="n">impl</span><span class="o">.</span><span class="n">createDocument</span><span class="p">(</span><span class="n">svgNS</span><span class="p">,</span> <span class="s">&quot;svg&quot;</span><span class="p">,</span> <span class="n">null</span><span class="p">);</span>
+    <span class="n">Element</span> <span class="n">root</span> <span class="o">=</span> <span class="n">document</span><span class="o">.</span><span class="n">getDocumentElement</span><span class="p">();</span>
+    <span class="n">root</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;width&quot;</span><span class="p">,</span> <span class="s">&quot;450&quot;</span><span class="p">);</span>
+    <span class="n">root</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;height&quot;</span><span class="p">,</span> <span class="s">&quot;500&quot;</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Add</span> <span class="n">some</span> <span class="n">content</span> <span class="n">to</span> <span class="n">the</span> <span class="n">document</span><span class="o">.</span>
+    <span class="n">Element</span> <span class="n">e</span><span class="p">;</span>
+    <span class="n">e</span> <span class="o">=</span> <span class="n">document</span><span class="o">.</span><span class="n">createElementNS</span><span class="p">(</span><span class="n">svgNS</span><span class="p">,</span> <span class="s">&quot;rect&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="s">&quot;10&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;y&quot;</span><span class="p">,</span> <span class="s">&quot;10&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;width&quot;</span><span class="p">,</span> <span class="s">&quot;200&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;height&quot;</span><span class="p">,</span> <span class="s">&quot;300&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;style&quot;</span><span class="p">,</span> <span class="s">&quot;fill:red;stroke:black;stroke-width:4&quot;</span><span class="p">);</span>
+    <span class="n">root</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">e</span><span class="p">);</span>
+
+    <span class="n">e</span> <span class="o">=</span> <span class="n">document</span><span class="o">.</span><span class="n">createElementNS</span><span class="p">(</span><span class="n">svgNS</span><span class="p">,</span> <span class="s">&quot;circle&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;cx&quot;</span><span class="p">,</span> <span class="s">&quot;225&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;cy&quot;</span><span class="p">,</span> <span class="s">&quot;250&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;r&quot;</span><span class="p">,</span> <span class="s">&quot;100&quot;</span><span class="p">);</span>
+    <span class="n">e</span><span class="o">.</span><span class="n">setAttributeNS</span><span class="p">(</span><span class="n">null</span><span class="p">,</span> <span class="s">&quot;style&quot;</span><span class="p">,</span> <span class="s">&quot;fill:green;fill-opacity:.5&quot;</span><span class="p">);</span>
+    <span class="n">root</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">e</span><span class="p">);</span>
+
+    <span class="k">return</span> <span class="n">document</span><span class="p">;</span>
+<span class="p">}</span>
+
+<span class="n">public</span> <span class="n">void</span> <span class="n">save</span><span class="p">(</span><span class="n">Document</span> <span class="n">document</span><span class="p">)</span> <span class="n">throws</span> <span class="n">Exception</span> <span class="p">{</span>
+
+    <span class="sr">//</span> <span class="n">Create</span> <span class="n">a</span> <span class="n">JPEGTranscoder</span> <span class="ow">and</span> <span class="n">set</span> <span class="n">its</span> <span class="n">quality</span> <span class="n">hint</span><span class="o">.</span>
+    <span class="n">JPEGTranscoder</span> <span class="n">t</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JPEGTranscoder</span><span class="p">();</span>
+    <span class="n">t</span><span class="o">.</span><span class="n">addTranscodingHint</span><span class="p">(</span><span class="n">JPEGTranscoder</span><span class="o">.</span><span class="n">KEY_QUALITY</span><span class="p">,</span>
+                         <span class="k">new</span> <span class="n">Float</span><span class="p">(</span><span class="mf">.8</span><span class="p">));</span>
+
+    <span class="sr">//</span> <span class="n">Set</span> <span class="n">the</span> <span class="n">transcoder</span> <span class="n">input</span> <span class="ow">and</span> <span class="n">output</span><span class="o">.</span>
+    <span class="n">TranscoderInput</span> <span class="n">input</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TranscoderInput</span><span class="p">(</span><span class="n">document</span><span class="p">);</span>
+    <span class="n">OutputStream</span> <span class="n">ostream</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileOutputStream</span><span class="p">(</span><span class="s">&quot;out.jpg&quot;</span><span class="p">);</span>
+    <span class="n">TranscoderOutput</span> <span class="n">output</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TranscoderOutput</span><span class="p">(</span><span class="n">ostream</span><span class="p">);</span>
+
+    <span class="sr">//</span> <span class="n">Perform</span> <span class="n">the</span> <span class="n">transcoding</span><span class="o">.</span>
+    <span class="n">t</span><span class="o">.</span><span class="n">transcode</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="n">output</span><span class="p">);</span>
+    <span class="n">ostream</span><span class="o">.</span><span class="n">flush</span><span class="p">();</span>
+    <span class="n">ostream</span><span class="o">.</span><span class="nb">close</span><span class="p">();</span>
+<span class="p">}</span>
+
+<span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="n">throws</span> <span class="n">Exception</span> <span class="p">{</span>
+    <span class="sr">//</span> <span class="n">Runs</span> <span class="n">the</span> <span class="n">example</span><span class="o">.</span>
+    <span class="n">DOMRasterizer</span> <span class="n">rasterizer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DOMRasterizer</span><span class="p">();</span>
+    <span class="n">Document</span> <span class="n">document</span> <span class="o">=</span> <span class="n">rasterizer</span><span class="o">.</span><span class="n">createDocument</span><span class="p">();</span>
+    <span class="n">rasterizer</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">document</span><span class="p">);</span>
+    <span class="n">System</span><span class="o">.</span><span class="nb">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
+<span class="p">}</span>
+</pre></div>
+
+
+<p>}</p>
+<p>This code is divided into two distinct parts.</p>
+<dl>
+<dt>Creating an SVG DOM tree</dt>
+<dd>See the <code>createDocument</code> method.</dd>
+</dl>
+<p>Three steps are required at this time. The first step is to get the Batik SVG DOM implementation (via the <code>SVGDOMImplementation</code> class). Then, you can create a <code>org.w3c.dom.Document</code> (which also implements the <code>SVGDocument</code> interface) by invoking the <code>createDocument</code> method with the SVG namespace URI and the <code>svg</code> document element name. Finally, you can get the document element and start building your DOM tree.</p>
+<dl>
+<dt>Rasterizing the DOM</dt>
+<dd>See the <code>save</code> method.</dd>
+</dl>
+<p>Similar to the previous examples, you can transcode an SVG document to a raster image by creating a <code>TranscoderInput</code> , but passing to it the SVG Document this time.</p>
+<p>To try this example:</p>
+<ol>
+<li>
+<p>Save the code as <code>DOMRasterizer.java</code> .</p>
+</li>
+<li>
+<p>Compile the program.</p>
+</li>
+<li>
+<p>Run it, by typingL
+java DOMRasterizer</p>
+</li>
+<li>
+<p>Inspect the output file <code>out.jpg</code> .</p>
+</li>
+</ol></div></div>
+      <div class="clear"></div>
+
+	  <div id="footer">
+		<a alt="Apache Software Foundation" href="http://www.apache.org">
+		  <img id="asf-logo" alt="Apache Software Foundation" src="/images/feather-small.gif"/ width="100">
+		</a>
+		<div class="copyright">
+		  <p>
+			Copyright &copy; 2011 The Apache Software Foundation, Licensed under
+			the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.
+			<br />
+			Apache, Apache XML Graphics, the Apache feather logo, and the Apache XML Graphics logos are
+			trademarks of <a href="http://www.apache.org">The Apache Software Foundation</a>. All other
+			marks mentioned may be trademarks or registered trademarks of their respective owners.
+			<br />
+		  </p>
+		</div> 
+	  </div>
+  </body>
+</html>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org


Mime
View raw message