The goal of the transcoder API (package
org.apache.batik.transcoder) is to provide a generic
API for transcoding an input to an output. First, this document
explains the basic transcoder API that
Transcoder,
TranscoderInput
and
TranscoderOutput
define, and thus all transcoders have in common. Next, it describes
how to use the image transcoder API (package
org.apache.batik.transcoder.image), which lets you
rasterize an SVG document fragment to a raster image such as JPEG,
PNG or TIFF.
The org.apache.batik.transcoder package defines 5 major
classes:
The
Transcoder
interface defines the interface that all transcoders implement. You can
transcode a specific input using a specific output by invoking the
transcode 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
Document,
a
Reader,
an
InputStream,
or a
URI
as an input but only supports a byte stream for the output.
The
TranscoderInput
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
org.w3c.dom.Document, a Reader, an
InputStream, a org.xml.sax.XMLReader, or a
URI.
The TranscoderOutput 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 Document, a Writer, an OutputStream, a XMLFilter, or a URI.
The TranscodingHints 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 JPEGTranscoder provides a hint to control the encoding quality.
The ErrorHandler 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.
The org.apache.batik.transcoder.image 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 ImageTranscoder class and implementing the
writeImage method. Although, in next sections, the
examples will use the JPEG transcoder, the PNG transcoder works the
same way.
The following example, using the JPEGTranscoder, shows how to
transform an SVG document to a JPEG image.
The code creates a JPEGTranscoder 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 out.jpg. Finally, the
transcode method is invoked and the byte stream is
closed.
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.
To try this program:
Save the above program as SaveAsJPEG.java.
Compile the program.
Choose an SVG file to transcode, and perform the transcoding by running:
Inspect the output file out.jpg.
By adding the following line of code to the previous example, you will
specify the raster image size (in pixels). The transcoding hint
KEY_WIDTH lets you specify the raster image width. If the
raster image height is not provided (using the KEY_HEIGHT),
the transcoder will compute the raster image height by keeping the
aspect ratio of the SVG document.
The transcoder will have the same behavior if you specify the
KEY_HEIGHT without initializing the
KEY_WIDTH. In all cases (even if both keys are provided),
the transcoder will preserve the apsect ratio of the SVG document.
The image transcoder lets you specify an area of interest (that is, a
part of the SVG document). The key KEY_AOI allows you to
select the region of the SVG document to render. The value of this key must
be a
java.awt.Rectangle
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.
This code splits the same document anne.svg 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 KEY_AOI 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 KEY_WIDTH and KEY_HEIGHT, that
do not match the dimensions of the KEY_AOI 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.
To try this program:
Save the above program as SaveAsJPEGTiles.java.
Compile the program.
Copy the samples/anne.svg document from the Batik
distribution into the current directory.
Run the program by typing:
Inspect the output files tileTopRight.jpg,
tileTopRight.jpg, tileBottomRight.jpg
and tileBottomLeft.jpg.
The ImageTranscoder provides additional
TranscodingHints that let you customize the generated
images.
ImageTranscoder.KEY_MEDIAThis hint lets you choose the CSS medium to use. The author of the SVG document to transcode can control CSS media using the CSS media rule. For example:
ImageTranscoder.KEY_ALTERNATE_STYLESHEETThis hint lets you choose an alternate stylesheet the author of the SVG document to transcode might have provided using the xml-stylesheet processing instruction. For example:
ImageTranscoder.KEY_USER_STYLESHEET_URIThis hint lets you use a user stylesheet. A user stylesheet can override some styles of the SVG document to transcode. For example:
ImageTranscoder.KEY_PIXEL_TO_MMThis hint lets you use the pixel to millimeter conversion factor. This factor is used to determine how units are converted into pixels. For example:
or
ImageTranscoder.KEY_BACKGROUND_COLORThis hint lets you choose a background color. For example:
The following code creates and saves an SVG DOM tree to a raster image.
This code is divided into two distinct parts.
See the createDocument method.
Three steps are required at this time. The first step is to
get the Batik SVG DOM implementation (via the
SVGDOMImplementation class). Then, you can create a
org.w3c.dom.Document (which also implements the
SVGDocument interface) by invoking the
createDocument method with the SVG namespace URI and
the svg document element name. Finally, you can get
the document element and start building your DOM tree.
See the save method.
Similar to the previous examples, you can transcode an SVG
document to a raster image by creating a
TranscoderInput, but passing to it the SVG Document
this time.
To try this example:
Save the code as DOMRasterizer.java.
Compile the program.
Run it, by typingL
Inspect the output file out.jpg.