|
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:
Transcoder -
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 org.w3c.dom.Document , a
Reader , an InputStream , or a
URI as an input but only supports a byte stream for the
output.
TranscoderInput -
Defines the input of a transcoder. There are various ways to create an
input and the most commons 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 .
TranscoderOutput -
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
org.w3c.dom.Document , a Writer , an
OutputStream , a org.xml.sax.XMLFilter , or a
URI .
TranscodingHints -
The TranscodingHints class contains different hints which 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.
ErrorHandler -
This class provides a way to get the errors and/or warnings that might
occur while transcoding. A default implementation is provided but
you can, for example, implement your own handler that display a dialog
instead of stack trace.
|
| | | | How to Use the Image Transcoder API | | | | |
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 and implementing the
writeImage method. Although, in next sections, the
examples will use the JPEG transcoder, the PNG transcoder works the
same way.
| | | | Defining the Size of the Image | | | | |
| | | | Selecting an Area of Interest | | | | |
The image transcoder lets you specify an area of interest (ie. a part
of the SVG document). The key KEY_AOI enables to select
the region of the SVG document to render. The value of this key must
be a java.awt.Rectangle specified in pixels and described
in the SVG document's space. The following example shows how you can
split an SVG document into 4 tiles.
| | | |
SaveAsJPEGTiles.java
====================
import java.io.*;
import java.awt.*;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
public class SaveAsJPEGTiles {
JPEGTranscoder trans = new JPEGTranscoder();
public SaveAsJPEGTiles() {
trans.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(.8));
}
public void tile(String inputFilename,
String outputFilename,
Rectangle aoi) throws Exception {
trans.addTranscodingHint(JPEGTranscoder.KEY_WIDTH,
new Float(aoi.width));
trans.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT,
new Float(aoi.height));
trans.addTranscodingHint(JPEGTranscoder.KEY_AOI, aoi);
String svgURI = new File(inputFilename).toURL().toString();
TranscoderInput input = new TranscoderInput(svgURI);
OutputStream ostream = new FileOutputStream(outputFilename);
TranscoderOutput output = new TranscoderOutput(ostream);
trans.transcode(input, output);
ostream.flush();
ostream.close();
}
public static void main(String [] args) throws Exception {
SaveAsJPEGTiles p = new SaveAsJPEGTiles();
String in = "samples/anne.svg";
int documentWidth = 450;
int documentHeight = 500;
int dw2 = documentWidth / 2;
int dh2 = documentHeight / 2;
p.tile(in, "tileTopLeft.jpg", new Rectangle(0, 0, dw2, dh2));
p.tile(in, "tileTopRight.jpg", new Rectangle(dw2, 0, dw2, dh2));
p.tile(in, "tileBottomLeft.jpg", new Rectangle(0, dh2, dw2, dh2));
p.tile(in, "tileBottomRight.jpg", new Rectangle(dw2, dh2, dw2, dh2));
System.exit(0);
}
}
| | | | |
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 combine the
KEY_WIDTH , KEY_HEIGHT keys with the
KEY_AOI . 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 zoom in or out depending on the specified raster
image size.
Try this:
- Compile and run the program: SaveAsJPEGTiles.java. You will need the "anne.svg" document.
% java SaveAsJPEGTiles
- Take a look at: tileTopRight.jpg, tileTopRight.jpg, tileBottomRight.jpg
and tileBottomLeft.jpg
|
The ImageTranscoder provides additional
TranscodingHints that lets you customize the generated
images.
ImageTranscoder.KEY_MEDIA -
This 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. Example:
trans.addTranscodingHint(ImageTranscoder.KEY_MEDIA, "print");
ImageTranscoder.KEY_ALTERNATE_STYLESHEET -
This hint lets you choose an alternate stylesheet the author of the SVG
document to transcode might have provided using the xml-stylesheet processing
instruction. Example:
trans.addTranscodingHint(ImageTranscoder.KEY_ALTERNATE_STYLESHEET, alternateStylesheetName);
ImageTranscoder.KEY_USER_STYLESHEET_URI -
This hint lets you use a user stylesheet. User stylesheet can override some
styles of the SVG document to transcode. Example:
trans.addTranscodingHint(ImageTranscoder.KEY_USER_STYLESHEET_URI, "http://...");
ImageTranscoder.KEY_PIXEL_TO_MM -
This hint lets you use the pixel to millimeter conversion factor. This factor
is used to determine how units are converted into pixels. Example:
// 96dpi
trans.addTranscodingHint(ImageTranscoder.KEY_PIXEL_TO_MM, new Float(0.2645833f));
or
// 72dpi
trans.addTranscodingHint(ImageTranscoder.KEY_PIXEL_TO_MM, new Float(0.3528f));
ImageTranscoder.KEY_BACKGROUND_COLOR -
This hint lets you choose a background color. Example:
trans.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);
|
| | | | Generating an Image from an SVG DOM Tree | | | | |
The following code creates and saves an SVG DOM tree.
| | | |
DOMRasterizer.java
==================
import java.io.*;
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;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DOMImplementation;
public class DOMRasterizer {
public Document createDocument() {
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document document =
impl.createDocument(svgNS, "svg", null);
Element root = document.getDocumentElement();
root.setAttributeNS(null, "width", "450");
root.setAttributeNS(null, "height", "500");
Element e;
e = document.createElementNS(svgNS, "rect");
e.setAttributeNS(null, "x", "10");
e.setAttributeNS(null, "y", "10");
e.setAttributeNS(null, "width", "200");
e.setAttributeNS(null, "height", "300");
e.setAttributeNS(null, "style", "fill:red;stroke:black;stroke-width:4");
root.appendChild(e);
e = document.createElementNS(svgNS, "circle");
e.setAttributeNS(null, "cx", "225");
e.setAttributeNS(null, "cy", "250");
e.setAttributeNS(null, "r", "100");
e.setAttributeNS(null, "style", "fill:green;fill-opacity:.5");
root.appendChild(e);
return document;
}
public void save(Document document) throws Exception {
JPEGTranscoder t = new JPEGTranscoder();
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(.8));
TranscoderInput input = new TranscoderInput(document);
OutputStream ostream = new FileOutputStream("out.jpg");
TranscoderOutput output = new TranscoderOutput(ostream);
t.transcode(input, output);
ostream.flush();
ostream.close();
}
public static void main(String [] args) throws Exception {
DOMRasterizer rasterizer = new DOMRasterizer();
Document document = rasterizer.createDocument();
rasterizer.save(document);
System.exit(0);
}
}
| | | | |
This code is divided into two distinct parts.
Creating an SVG DOM tree -
See the createDocument method Three steps are
required at this time. The first one consists on getting the Batik SVG
DOM implementation (via the SVGDOMImplementation class). Then, you can
create a org.w3c.dom.Document (which is an SVG Document by
the way) by invoking the createDocument method with the
svg namespace URI and the "svg" document element. At last, you can get
the document element and start building your DOM tree.
Rasterizing your DOM -
See the save method Similar to the previous
examples, you can transcode an SVG document to a raster image by
creating a TranscoderInput with this time, the SVG Document.
Try this:
- Compile and run the program: DOMRasterizer.java.
% java DOMRasterizer
- Take a look at: out.jpg
|
|
|
|