| 1 |
<?xml version="1.0"?> |
| 2 |
|
| 3 |
<!-- |
| 4 |
|
| 5 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 6 |
contributor license agreements. See the NOTICE file distributed with |
| 7 |
this work for additional information regarding copyright ownership. |
| 8 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 9 |
(the "License"); you may not use this file except in compliance with |
| 10 |
the License. You may obtain a copy of the License at |
| 11 |
|
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
Unless required by applicable law or agreed to in writing, software |
| 15 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
See the License for the specific language governing permissions and |
| 18 |
limitations under the License. |
| 19 |
|
| 20 |
--> |
| 21 |
|
| 22 |
<!-- ========================================================================= --> |
| 23 |
<!-- author [email protected] --> |
| 24 |
<!-- version $Id$ --> |
| 25 |
<!-- ========================================================================= --> |
| 26 |
|
| 27 |
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd"> |
| 28 |
<document> |
| 29 |
<header> |
| 30 |
<title>Transcoder API</title> |
| 31 |
</header> |
| 32 |
|
| 33 |
<body> |
| 34 |
<p> |
| 35 |
The goal of the transcoder API (package |
| 36 |
<code>org.apache.batik.transcoder</code>) is to provide a generic |
| 37 |
API for transcoding an input to an output. First, this document |
| 38 |
explains the basic transcoder API that |
| 39 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/Transcoder.html">Transcoder</a>, |
| 40 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/TranscoderInput.html">TranscoderInput</a> |
| 41 |
and |
| 42 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/TranscoderOutput.html">TranscoderOutput</a> |
| 43 |
define, and thus all transcoders have in common. Next, it describes |
| 44 |
how to use the image transcoder API (package |
| 45 |
<code>org.apache.batik.transcoder.image</code>), which lets you |
| 46 |
rasterize an SVG document fragment to a raster image such as JPEG, |
| 47 |
PNG or TIFF. |
| 48 |
</p> |
| 49 |
|
| 50 |
<section id="transcoderAPI"> |
| 51 |
<title>The transcoder API</title> |
| 52 |
<p> |
| 53 |
The <code>org.apache.batik.transcoder</code> package defines 5 major |
| 54 |
classes: |
| 55 |
</p> |
| 56 |
<dl> |
| 57 |
<dt>Transcoder</dt> |
| 58 |
<dd> |
| 59 |
<p> |
| 60 |
The |
| 61 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/Transcoder.html">Transcoder</a> |
| 62 |
interface defines the interface that all transcoders implement. You can |
| 63 |
transcode a specific input using a specific output by invoking the |
| 64 |
<code>transcode</code> method. Although there is no assumption on the |
| 65 |
input and output format, a specific transcoder may or may not support |
| 66 |
a particular type of input or output. For example, the image |
| 67 |
transcoders accept an SVG |
| 68 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</a>, |
| 69 |
a |
| 70 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.html">Reader</a>, |
| 71 |
an |
| 72 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html">InputStream</a>, |
| 73 |
or a |
| 74 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">URI</a> |
| 75 |
as an input but only supports a byte stream for the output. |
| 76 |
</p> |
| 77 |
</dd> |
| 78 |
|
| 79 |
<dt>TranscoderInput</dt> |
| 80 |
<dd> |
| 81 |
<p> |
| 82 |
The |
| 83 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/TranscoderInput.html">TranscoderInput</a> |
| 84 |
interface defines the input of a transcoder. There are various ways to create an |
| 85 |
input and the most common are already part of the API. The default |
| 86 |
implementation lets you create an input using a |
| 87 |
<code>org.w3c.dom.Document</code>, a <code>Reader</code>, an |
| 88 |
<code>InputStream</code>, a <code>org.xml.sax.XMLReader</code>, or a |
| 89 |
<code>URI</code>. |
| 90 |
</p> |
| 91 |
</dd> |
| 92 |
|
| 93 |
<dt>TranscoderOutput</dt> |
| 94 |
<dd> |
| 95 |
<p> |
| 96 |
The |
| 97 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/TranscoderOutput.html">TranscoderOutput</a> |
| 98 |
interface defines the output of a transcoder. There are various ways to create an |
| 99 |
output and the most commons are already part of the API. The default |
| 100 |
implementation lets you create an output using a |
| 101 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html">Document</a>, |
| 102 |
a |
| 103 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Writer.html">Writer</a>, |
| 104 |
an |
| 105 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html">OutputStream</a>, |
| 106 |
a |
| 107 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/XMLFilter.html">XMLFilter</a>, |
| 108 |
or a |
| 109 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">URI</a>. |
| 110 |
</p> |
| 111 |
</dd> |
| 112 |
|
| 113 |
<dt>TranscodingHints</dt> |
| 114 |
<dd> |
| 115 |
<p> |
| 116 |
The |
| 117 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/TranscodingHints.html">TranscodingHints</a> |
| 118 |
class contains different hints that can be used to control |
| 119 |
the various options or parameters of a transcoder. Each transcoder |
| 120 |
provides its own set of hints. A hint is specified by (key, value) |
| 121 |
pair. For example, the |
| 122 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/image/JPEGTranscoder.html">JPEGTranscoder</a> |
| 123 |
provides a hint to control the encoding quality. |
| 124 |
</p> |
| 125 |
</dd> |
| 126 |
|
| 127 |
<dt>ErrorHandler</dt> |
| 128 |
<dd> |
| 129 |
<p> |
| 130 |
The |
| 131 |
<a class="class" href="../javadoc/org/apache/batik/transcoder/ErrorHandler.html">ErrorHandler</a> |
| 132 |
class provides a way to get any errors or warnings that might |
| 133 |
occur while transcoding. A default implementation is provided but |
| 134 |
you can, for example, implement your own handler that displays a dialog |
| 135 |
instead of stack trace. |
| 136 |
</p> |
| 137 |
</dd> |
| 138 |
</dl> |
| 139 |
</section> |
| 140 |
|
| 141 |
<section id="howtousetranscoderAPI"> |
| 142 |
<title>How to use the image transcoder API</title> |
| 143 |
<p> |
| 144 |
The <code>org.apache.batik.transcoder.image</code> package provides an |
| 145 |
easy way to transcode an SVG document to a raster image such as JPEG, |
| 146 |
PNG or TIFF. Additional raster image formats can be added by |
| 147 |
subclassing the <code>ImageTranscoder</code> class and implementing the |
| 148 |
<code>writeImage</code> method. Although, in next sections, the |
| 149 |
examples will use the JPEG transcoder, the PNG transcoder works the |
| 150 |
same way. |
| 151 |
</p> |
| 152 |
|
| 153 |
<section id="createImage"> |
| 154 |
<title>Creating an image</title> |
| 155 |
<p> |
| 156 |
The following example, using the <code>JPEGTranscoder</code>, shows how to |
| 157 |
transform an SVG document to a JPEG image. |
| 158 |
</p> |
| 159 |
<source>import java.io.*; |
| 160 |
|
| 161 |
import org.apache.batik.transcoder.image.JPEGTranscoder; |
| 162 |
import org.apache.batik.transcoder.TranscoderInput; |
| 163 |
import org.apache.batik.transcoder.TranscoderOutput; |
| 164 |
|
| 165 |
public class SaveAsJPEG { |
| 166 |
|
| 167 |
public static void main(String[] args) throws Exception { |
| 168 |
|
| 169 |
// Create a JPEG transcoder |
| 170 |
<strong>JPEGTranscoder t = new JPEGTranscoder();</strong> |
| 171 |
|
| 172 |
// Set the transcoding hints. |
| 173 |
<strong>t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, |
| 174 |
new Float(.8));</strong> |
| 175 |
|
| 176 |
// Create the transcoder input. |
| 177 |
String svgURI = new File(args[0]).toURL().toString(); |
| 178 |
<strong>TranscoderInput input = new TranscoderInput(svgURI);</strong> |
| 179 |
|
| 180 |
// Create the transcoder output. |
| 181 |
OutputStream ostream = new FileOutputStream("out.jpg"); |
| 182 |
<strong>TranscoderOutput output = new TranscoderOutput(ostream);</strong> |
| 183 |
|
| 184 |
// Save the image. |
| 185 |
<strong>t.transcode(input, output);</strong> |
| 186 |
|
| 187 |
// Flush and close the stream. |
| 188 |
ostream.flush(); |
| 189 |
ostream.close(); |
| 190 |
System.exit(0); |
| 191 |
} |
| 192 |
}</source> |
| 193 |
<p> |
| 194 |
The code creates a <code>JPEGTranscoder</code> and sets a |
| 195 |
transcoding hint. The hint indicates the encoding quality. Then, an |
| 196 |
input and an output are created. The input is created using the first |
| 197 |
command line argument, which should be a URI. The output is a byte |
| 198 |
stream for a file called <code>out.jpg</code>. Finally, the |
| 199 |
<code>transcode</code> method is invoked and the byte stream is |
| 200 |
closed. |
| 201 |
</p> |
| 202 |
<p> |
| 203 |
Although not shown above, the program might have specified additional |
| 204 |
hints to indicate a user stylesheet, the preferred language of the |
| 205 |
document or the background color. |
| 206 |
</p> |
| 207 |
<p> |
| 208 |
To try this program: |
| 209 |
</p> |
| 210 |
<ol> |
| 211 |
<li> |
| 212 |
<p>Save the above program as <code>SaveAsJPEG.java</code>. </p> |
| 213 |
</li> |
| 214 |
<li> |
| 215 |
<p>Compile the program.</p> |
| 216 |
</li> |
| 217 |
<li> |
| 218 |
<p>Choose an SVG file to transcode, and perform the transcoding by running:</p> |
| 219 |
<source>java SaveAsJPEG <em>filename</em>.svg</source> |
| 220 |
</li> |
| 221 |
<li> |
| 222 |
<p>Inspect the output file <code>out.jpg</code>.</p> |
| 223 |
</li> |
| 224 |
</ol> |
| 225 |
</section> |
| 226 |
|
| 227 |
<section id="defineSizeOfImage"> |
| 228 |
<title>Defining the size of the image</title> |
| 229 |
<p> |
| 230 |
By adding the following line of code to the previous example, you will |
| 231 |
specify the raster image size (in pixels). The transcoding hint |
| 232 |
<code>KEY_WIDTH</code> lets you specify the raster image width. If the |
| 233 |
raster image height is not provided (using the <code>KEY_HEIGHT</code>), |
| 234 |
the transcoder will compute the raster image height by keeping the |
| 235 |
aspect ratio of the SVG document. |
| 236 |
</p> |
| 237 |
<source>t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Float(100));</source> |
| 238 |
<p> |
| 239 |
The transcoder will have the same behavior if you specify the |
| 240 |
<code>KEY_HEIGHT</code> without initializing the |
| 241 |
<code>KEY_WIDTH</code>. In all cases (even if both keys are provided), |
| 242 |
the transcoder will preserve the apsect ratio of the SVG document. |
| 243 |
</p> |
| 244 |
</section> |
| 245 |
|
| 246 |
<section id="selectAreaOfIntrest"> |
| 247 |
<title>Selecting an area of interest</title> |
| 248 |
<p> |
| 249 |
The image transcoder lets you specify an area of interest (that is, a |
| 250 |
part of the SVG document). The key <code>KEY_AOI</code> allows you to |
| 251 |
select the region of the SVG document to render. The value of this key must |
| 252 |
be a |
| 253 |
<a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Rectangle.html">java.awt.Rectangle</a> |
| 254 |
specified in pixels, using the coordinate space of the SVG document. The |
| 255 |
following example shows how you can split an SVG document into 4 tiles. |
| 256 |
</p> |
| 257 |
<source>import java.io.*; |
| 258 |
import java.awt.*; |
| 259 |
|
| 260 |
import org.apache.batik.transcoder.image.JPEGTranscoder; |
| 261 |
import org.apache.batik.transcoder.TranscoderInput; |
| 262 |
import org.apache.batik.transcoder.TranscoderOutput; |
| 263 |
|
| 264 |
public class SaveAsJPEGTiles { |
| 265 |
|
| 266 |
JPEGTranscoder trans = new JPEGTranscoder(); |
| 267 |
|
| 268 |
public SaveAsJPEGTiles() { |
| 269 |
trans.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, |
| 270 |
new Float(.8)); |
| 271 |
} |
| 272 |
|
| 273 |
public void tile(String inputFilename, |
| 274 |
String outputFilename, |
| 275 |
Rectangle aoi) throws Exception { |
| 276 |
// Set hints to indicate the dimensions of the output image |
| 277 |
// and the input area of interest. |
| 278 |
<strong>trans.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, |
| 279 |
new Float(aoi.width));</strong> |
| 280 |
<strong>trans.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, |
| 281 |
new Float(aoi.height));</strong> |
| 282 |
<strong>trans.addTranscodingHint(JPEGTranscoder.KEY_AOI, aoi);</strong> |
| 283 |
|
| 284 |
// Transcode the file. |
| 285 |
String svgURI = new File(inputFilename).toURL().toString(); |
| 286 |
TranscoderInput input = new TranscoderInput(svgURI); |
| 287 |
OutputStream ostream = new FileOutputStream(outputFilename); |
| 288 |
TranscoderOutput output = new TranscoderOutput(ostream); |
| 289 |
trans.transcode(input, output); |
| 290 |
|
| 291 |
// Flush and close the output. |
| 292 |
ostream.flush(); |
| 293 |
ostream.close(); |
| 294 |
} |
| 295 |
|
| 296 |
public static void main(String[] args) throws Exception { |
| 297 |
// Rasterize the samples/anne.svg document and save it |
| 298 |
// as four tiles. |
| 299 |
SaveAsJPEGTiles p = new SaveAsJPEGTiles(); |
| 300 |
<strong>String in = "samples/anne.svg"; |
| 301 |
int documentWidth = 450; |
| 302 |
int documentHeight = 500;</strong> |
| 303 |
int dw2 = documentWidth / 2; |
| 304 |
int dh2 = documentHeight / 2; |
| 305 |
p.tile(in, "tileTopLeft.jpg", new Rectangle(0, 0, dw2, dh2)); |
| 306 |
p.tile(in, "tileTopRight.jpg", new Rectangle(dw2, 0, dw2, dh2)); |
| 307 |
p.tile(in, "tileBottomLeft.jpg", new Rectangle(0, dh2, dw2, dh2)); |
| 308 |
p.tile(in, "tileBottomRight.jpg", new Rectangle(dw2, dh2, dw2, dh2)); |
| 309 |
System.exit(0); |
| 310 |
} |
| 311 |
}</source> |
| 312 |
<p> |
| 313 |
This code splits the same document <code>anne.svg</code> into four |
| 314 |
tiles of the same size. Considering the document and its original |
| 315 |
size, we can determine four regions. Then we rasterize each region |
| 316 |
using the <code>KEY_AOI</code> key. Note that we also specify the |
| 317 |
image width and height to be the same as the area of interest width and |
| 318 |
height (so we keep a 1:1 zoom factor). You can of course use different |
| 319 |
values for the <code>KEY_WIDTH</code> and <code>KEY_HEIGHT</code>, that |
| 320 |
do not match the dimensions of the <code>KEY_AOI</code> key. In that |
| 321 |
case, first the area of interest will determine which part of the SVG |
| 322 |
document has to be rendered, then that part could be zoomed in or out |
| 323 |
depending on the specified raster image size. |
| 324 |
</p> |
| 325 |
<p> |
| 326 |
To try this program: |
| 327 |
</p> |
| 328 |
<ol> |
| 329 |
<li><p>Save the above program as <code>SaveAsJPEGTiles.java</code>.</p></li> |
| 330 |
<li><p>Compile the program.</p></li> |
| 331 |
<li><p>Copy the <code>samples/anne.svg</code> document from the Batik |
| 332 |
distribution into the current directory.</p></li> |
| 333 |
<li> |
| 334 |
<p>Run the program by typing:</p> |
| 335 |
<source>java SaveAsJPEGTiles</source> |
| 336 |
</li> |
| 337 |
<li> |
| 338 |
<p> |
| 339 |
Inspect the output files <code>tileTopRight.jpg</code>, |
| 340 |
<code>tileTopRight.jpg</code>, <code>tileBottomRight.jpg</code> |
| 341 |
and <code>tileBottomLeft.jpg</code>. |
| 342 |
</p> |
| 343 |
</li> |
| 344 |
</ol> |
| 345 |
</section> |
| 346 |
|
| 347 |
<section id="otherTransHints"> |
| 348 |
<title>Other transcoding hints</title> |
| 349 |
<p> |
| 350 |
The <code>ImageTranscoder</code> provides additional |
| 351 |
<code>TranscodingHints</code> that let you customize the generated |
| 352 |
images. |
| 353 |
</p> |
| 354 |
<dl> |
| 355 |
<dt><code>ImageTranscoder.KEY_MEDIA</code></dt> |
| 356 |
<dd> |
| 357 |
<p> |
| 358 |
This hint lets you choose the CSS medium to use. The author of the SVG document |
| 359 |
to transcode can control CSS media using the |
| 360 |
<a href="http://www.w3.org/TR/REC-CSS2/media.html">CSS media rule</a>. |
| 361 |
For example: |
| 362 |
</p> |
| 363 |
<source>trans.addTranscodingHint(ImageTranscoder.KEY_MEDIA, "print");</source> |
| 364 |
</dd> |
| 365 |
|
| 366 |
<dt><code>ImageTranscoder.KEY_ALTERNATE_STYLESHEET</code></dt> |
| 367 |
<dd> |
| 368 |
<p> |
| 369 |
This hint lets you choose an alternate stylesheet the author of the SVG |
| 370 |
document to transcode might have provided using the |
| 371 |
<a href="http://www.w3.org/TR/xml-stylesheet/">xml-stylesheet</a> |
| 372 |
processing instruction. For example: |
| 373 |
</p> |
| 374 |
<source>trans.addTranscodingHint(ImageTranscoder.KEY_ALTERNATE_STYLESHEET, |
| 375 |
alternateStylesheetName);</source> |
| 376 |
</dd> |
| 377 |
|
| 378 |
<dt><code>ImageTranscoder.KEY_USER_STYLESHEET_URI</code></dt> |
| 379 |
<dd> |
| 380 |
<p> |
| 381 |
This hint lets you use a user stylesheet. A user stylesheet can override some |
| 382 |
styles of the SVG document to transcode. For example: |
| 383 |
</p> |
| 384 |
<source>trans.addTranscodingHint(ImageTranscoder.KEY_USER_STYLESHEET_URI, "http://...");</source> |
| 385 |
</dd> |
| 386 |
|
| 387 |
<dt><code>ImageTranscoder.KEY_PIXEL_TO_MM</code></dt> |
| 388 |
<dd> |
| 389 |
<p> |
| 390 |
This hint lets you use the pixel to millimeter conversion factor. |
| 391 |
This factor is used to determine how units are converted into |
| 392 |
pixels. For example: |
| 393 |
</p> |
| 394 |
<source>// 96dpi |
| 395 |
trans.addTranscodingHint(ImageTranscoder.KEY_PIXEL_TO_MM, new Float(0.2645833f));</source> |
| 396 |
<p>or</p> |
| 397 |
<source>// 72pi |
| 398 |
trans.addTranscodingHint(ImageTranscoder.KEY_PIXEL_TO_MM, new Float(0.3528f));</source> |
| 399 |
</dd> |
| 400 |
|
| 401 |
<dt><code>ImageTranscoder.KEY_BACKGROUND_COLOR</code></dt> |
| 402 |
<dd> |
| 403 |
<p> |
| 404 |
This hint lets you choose a background color. For example: |
| 405 |
</p> |
| 406 |
<source>trans.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);</source> |
| 407 |
</dd> |
| 408 |
</dl> |
| 409 |
</section> |
| 410 |
|
| 411 |
<section id="genImagefromSVGDOM"> |
| 412 |
<title>Generating an image from an SVG DOM tree</title> |
| 413 |
<p> |
| 414 |
The following code creates and saves an SVG DOM tree to |
| 415 |
a raster image. |
| 416 |
</p> |
| 417 |
<source>import java.io.*; |
| 418 |
|
| 419 |
import org.apache.batik.transcoder.image.JPEGTranscoder; |
| 420 |
import org.apache.batik.transcoder.TranscoderInput; |
| 421 |
import org.apache.batik.transcoder.TranscoderOutput; |
| 422 |
import org.apache.batik.dom.svg.SVGDOMImplementation; |
| 423 |
|
| 424 |
import org.w3c.dom.Document; |
| 425 |
import org.w3c.dom.Element; |
| 426 |
import org.w3c.dom.DOMImplementation; |
| 427 |
|
| 428 |
public class DOMRasterizer { |
| 429 |
|
| 430 |
public Document createDocument() { |
| 431 |
|
| 432 |
// Create a new document. |
| 433 |
<strong>DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); |
| 434 |
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; |
| 435 |
Document document = |
| 436 |
impl.createDocument(svgNS, "svg", null); |
| 437 |
Element root = document.getDocumentElement();</strong> |
| 438 |
root.setAttributeNS(null, "width", "450"); |
| 439 |
root.setAttributeNS(null, "height", "500"); |
| 440 |
|
| 441 |
// Add some content to the document. |
| 442 |
Element e; |
| 443 |
e = document.createElementNS(svgNS, "rect"); |
| 444 |
e.setAttributeNS(null, "x", "10"); |
| 445 |
e.setAttributeNS(null, "y", "10"); |
| 446 |
e.setAttributeNS(null, "width", "200"); |
| 447 |
e.setAttributeNS(null, "height", "300"); |
| 448 |
e.setAttributeNS(null, "style", "fill:red;stroke:black;stroke-width:4"); |
| 449 |
root.appendChild(e); |
| 450 |
|
| 451 |
e = document.createElementNS(svgNS, "circle"); |
| 452 |
e.setAttributeNS(null, "cx", "225"); |
| 453 |
e.setAttributeNS(null, "cy", "250"); |
| 454 |
e.setAttributeNS(null, "r", "100"); |
| 455 |
e.setAttributeNS(null, "style", "fill:green;fill-opacity:.5"); |
| 456 |
root.appendChild(e); |
| 457 |
|
| 458 |
return document; |
| 459 |
} |
| 460 |
|
| 461 |
public void save(Document document) throws Exception { |
| 462 |
|
| 463 |
// Create a JPEGTranscoder and set its quality hint. |
| 464 |
JPEGTranscoder t = new JPEGTranscoder(); |
| 465 |
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, |
| 466 |
new Float(.8)); |
| 467 |
|
| 468 |
// Set the transcoder input and output. |
| 469 |
<strong>TranscoderInput input = new TranscoderInput(document);</strong> |
| 470 |
OutputStream ostream = new FileOutputStream("out.jpg"); |
| 471 |
TranscoderOutput output = new TranscoderOutput(ostream); |
| 472 |
|
| 473 |
// Perform the transcoding. |
| 474 |
t.transcode(input, output); |
| 475 |
ostream.flush(); |
| 476 |
ostream.close(); |
| 477 |
} |
| 478 |
|
| 479 |
public static void main(String[] args) throws Exception { |
| 480 |
// Runs the example. |
| 481 |
DOMRasterizer rasterizer = new DOMRasterizer(); |
| 482 |
Document document = rasterizer.createDocument(); |
| 483 |
rasterizer.save(document); |
| 484 |
System.exit(0); |
| 485 |
} |
| 486 |
} |
| 487 |
</source> |
| 488 |
<p> |
| 489 |
This code is divided into two distinct parts. |
| 490 |
</p> |
| 491 |
<dl> |
| 492 |
<dt>Creating an SVG DOM tree</dt> |
| 493 |
<dd> |
| 494 |
<p>See the <code>createDocument</code> method.</p> |
| 495 |
<p> |
| 496 |
Three steps are required at this time. The first step is to |
| 497 |
get the Batik SVG DOM implementation (via the |
| 498 |
<code>SVGDOMImplementation</code> class). Then, you can create a |
| 499 |
<code>org.w3c.dom.Document</code> (which also implements the |
| 500 |
<code>SVGDocument</code> interface) by invoking the |
| 501 |
<code>createDocument</code> method with the SVG namespace URI and |
| 502 |
the <code>svg</code> document element name. Finally, you can get |
| 503 |
the document element and start building your DOM tree. |
| 504 |
</p> |
| 505 |
</dd> |
| 506 |
<dt>Rasterizing the DOM</dt> |
| 507 |
<dd> |
| 508 |
<p>See the <code>save</code> method.</p> |
| 509 |
<p> |
| 510 |
Similar to the previous examples, you can transcode an SVG |
| 511 |
document to a raster image by creating a |
| 512 |
<code>TranscoderInput</code>, but passing to it the SVG Document |
| 513 |
this time. |
| 514 |
</p> |
| 515 |
</dd> |
| 516 |
</dl> |
| 517 |
<p> |
| 518 |
To try this example: |
| 519 |
</p> |
| 520 |
<ol> |
| 521 |
<li><p>Save the code as <code>DOMRasterizer.java</code>.</p></li> |
| 522 |
<li><p>Compile the program.</p></li> |
| 523 |
<li><p>Run it, by typingL</p> |
| 524 |
<source>java DOMRasterizer</source></li> |
| 525 |
<li><p>Inspect the output file <code>out.jpg</code>.</p></li> |
| 526 |
</ol> |
| 527 |
</section> |
| 528 |
</section> |
| 529 |
|
| 530 |
</body> |
| 531 |
</document> |