WWW.DISSERTATION.XLIBX.INFO
FREE ELECTRONIC LIBRARY - Dissertations, online materials
 
<< HOME
CONTACTS



«Image Transcoder Tutorial A brief introduction to the image transcoder API by Thierry Kormann Table of contents Introduction The Transcoder API How ...»

Image Transcoder Tutorial

A brief introduction to the image transcoder API

by Thierry Kormann

Table of contents

Introduction

The Transcoder API

How to Use the Image Transcoder API

Creating an Image

3.1

Defining the Size of the Image

3.2

Selecting an Area of Interest

3.3

Other Transcoding Hints

3.4

Generating an Image from an SVG DOM Tree

3.5

PDF created by Apache FOP

http://xmlgraphics.apache.org/fop/

Image Transcoder Tutorial

1. Introduction 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 Transcoder API

• How to Use the Image Transcoder API

• Creating an Image

• Defining the Size of the Image

• Selecting an Area of Interest

• Other Transcoding Hints

• Generating an Image from an SVG DOM Tree

2. The Transcoder API

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 Page 2 PDF created by Apache FOP http://xmlgraphics.apache.org/fop/ Image Transcoder Tutorial 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.

3. 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.

3.1. Creating an Image The following example, using the JPEGTranscoder shows how to trasnform an SVG document to a JPEG image.

SaveAsJpeg.java =============== import java.io.*;

import org.apache.batik.transcoder.image.JPEGTranscoder;

import org.apache.batik.transcoder.TranscoderInput;

import org.apache.batik.transcoder.TranscoderOutput;

public class SaveAsJPEG { public static void main(String [] args) throws Exception { // create a JPEG transcoder JPEGTranscoder t = new JPEGTranscoder();

// set the transcoding hints t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));

// create the transcoder input String svgURI = new File(args[0]).toURL().toString();

TranscoderInput input = new TranscoderInput(svgURI);

–  –  –

// create the transcoder output OutputStream ostream = new FileOutputStream("out.jpg");

TranscoderOutput output = new TranscoderOutput(ostream);

// save the image t.transcode(input, output);

// flush and close the stream then exit ostream.flush();

ostream.close();

System.exit(0);

} } The code creates a JPEGTranscoder and sets the transcoding hints. The first hint indicates the XML parser to use. The second hint sets the encoding quality. Then, an input and an output are created. The input is created using the first command line argument which should represent a URI. The output is a byte stream representing 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 style sheet, the preferred language of the document or the background color.

Try this:





1. Compile and run the program: SaveAsJPEG.java. You will need an SVG document.

% java SaveAsJPEG filename.svg

2. Take a look at: out.jpg

3.2. Defining the Size of the Image

By adding the following line of code to the previous example, you will specify the raster image size (in pixels). The new transcoding hint KEY_WIDTH lets you specify the raster image width. As the raster image height is not provided (using the KEY_HEIGHT), the transcoder will compute the raster image dimension 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 KEY_HEIGHT without initializing the KEY_WIDTH. In all cases (though both keys are provided), the transcoder will preserve the apsect ratio of the SVG document.

3.3. 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:

1. Compile and run the program: SaveAsJPEGTiles.java. You will need the "anne.svg" document.

% java SaveAsJPEGTiles

2. Take a look at: tileTopRight.jpg, tileTopRight.jpg, tileBottomRight.jpg and tileBottomLeft.jpg

3.4. Other Transcoding Hints

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:

Page 6 PDF created by Apache FOP http://xmlgraphics.apache.org/fop/ Image Transcoder Tutorial // 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);

3.5. 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:

1. Compile and run the program: DOMRasterizer.java.



Similar works:

«Du’a for Reading the Book ead the following Du’a (supplication) before studying a R religious book or Islamic lesson,, you will remember whatever you study: ََ َ ‫اَ ُ ا‬ ُ َ َ ََ َ ‫وا‬ ِ َََ َ ََ َ ‫َ ر َ َ َـ‬ ‫ذاا َل واِ َ ام‬ ِ Translation Yā Allah ! Open the portal of knowledge and wisdom for us, and have mercy on us! O the One Who is the Most Honourable and Magnificent! (Al-Musta raf, vol. 1, pp. 40) Note: Recite...»

«Courtesy of John Devaney at www.fullpointsfooty.net 1961 SANFL Grand Final: West Adelaide vs. Norwood The Turkish Bath Grand Final Link Bar 1 Norwood's 'Big Bill' Wedding wins a clean tap against Neil Kerley during the course of the 1961 SANFL grand final. Courtesy of John Devaney at www.fullpointsfooty.net Apart from the spectacular fall from grace of 1960 premier North Adelaide, the 1961 SANFL minor round spawned few surprises, and gave rise to the following ladder: 1961 SANFL Premiership...»

«Global Energy & Infrastructure Alert July 2015 FERC Assesses Penalties for Alleged No-Risk UTC Trading Against City Power and Its Founder Earlier this month, the Federal Energy Regulatory Commission (FERC) issued an Order Assessing Civil Penalties against City Power Marketing, LLC (City Power) and its founder and sole owner, K. Stephen Tsingas (collectively, Respondents) for violating section 222 of the Federal Power Act (FPA) and section 1c.2 of the Commission’s regulations, which prohibit...»

«Level 2 Certificate in Professional Patisserie and Confectionery (7120-02) www.cityandguilds.com Qualification handbook for centres July 2011 Version 1.0 600/1986/4 About City & Guilds City & Guilds is the UK’s leading provider of vocational qualifications, offering over 500 awards across a wide range of industries, and progressing from entry level to the highest levels of professional achievement. With over 8500 centres in 100 countries, City & Guilds is recognised by employers worldwide for...»

«Water Quality Trends for Minnesota Rivers and Streams at Milestone Sites Five of seven pollutants better, two getting worse June 2014 Author The MPCA is reducing printing and mailing costs by using the Internet to distribute reports and David Christopherson information to wider audience. Visit our website for more information. MPCA reports are printed on 100% postconsumer recycled content paper manufactured without chlorine or chlorine derivatives. Minnesota Pollution Control Agency 520...»

«Families with Children’s Activity 1 Rewriting a Gospel One way to give children insight into Scripture is to have them rewrite it. Creative writing can lead children to understand more deeply the meaning of the Scripture and help them to remember it. Check the Lectionary readings that will be used at Mass over the next month or season (Advent, Christmas, Lent, Easter). Select these readings as a focus for the activity. Children can work on different readings. There are a variety of ways that...»

«Fund Name Al-Ahli Saudi Mid-Cap Equity Fund The Fund is regulated by the Investment Funds Regulations issued by the Capital Market Authority The Fund has been approved by the Capital Market Authority On 26/6/1429 corresponding to 30/6/2008 Fund Manager NCB Capital Company Fund Manager Address NCB Capital Company Commercial Registration number 1010231474 Al Mather Street, P.O. Box 22216, Riyadh 11495, Kingdom of Saudi Arabia. Website address: www.ncbc.com Custodian NCB Capital Company Auditors...»

«What Does Classifying More Than 10,000 Image Categories Tell Us? Jia Deng1,3, Alexander C. Berg2, Kai Li1, and Li Fei-Fei3 1 Princeton University 2 Columbia University 3 Stanford University Abstract. Image classification is a critical task for both humans and computers. One of the challenges lies in the large scale of the semantic space. In particular, humans can recognize tens of thousands of object classes and scenes. No computer vision algorithm today has been tested at this scale. This...»

«STATE OF FLORIDA DEPARTMENT OF TRANSPORTATION DRAINAGE HANDBOOK STORMWATER MANAGEMENT FACILITY OFFICE OF DESIGN, DRAINAGE SECTION January 2004 TALLAHASSEE, FLORIDA TABLE OF CONTENTS Chapter 1 Introduction 1.1 Background 1.2 Purpose 1.3 Regulatory Framework 1.4 General Discussion of This Handbook 1.4.1 Units 1.4.2 Examples 1.4.3 Possible Plan Notes 1.4.4 Distribution 1.4.5 Revisions 1.5 Definitions of Terms and Acronyms Chapter 2 Selecting a Pond Site 2.1 Estimating Right of Way Requirements...»

«1 Teens, Technology, and Trouble Our students are always online and always on their phones, and some of them are misusing technology in pretty bad ways. And it’s affecting what we’re trying to accomplish at our school. We’ve set numerous rules, and outlined a number of sanctions—some of which are severe. We’ve implemented blocks and filters. But I feel these strategies are piecemeal. There has got to be a better approach. There has got to be something else we can do. —educator from...»

«Larhyss Journal, ISSN 1112-3680, n°20, Décembre 2014, pp. 209-224 © 2014 Tous droits réservés STRATEGIE DE PROSPECTION HYDROGEOLOGIQUE PAR PREVISION DE L’ÉPAISSEUR DE SOCLE A FORER KOUASSI A.M.1, KADIO K.K.R.2, AHOUSSI K.E.3, BIEMI J.3 1 Institut National Polytechnique Félix Houphouët-Boigny (INP-HB) ; Département des Sciences de la Terre et des Ressources Minières (STeRMi) ; Laboratoire du Génie Civil, des Géosciences et des Sciences Géographiques ; BP 1093 Yamoussoukro, Côte...»

«CASE 0:04-cv-02720-DSD-SRN Document 41 Filed 08/31/05 Page 1 of 13 UNITED STATES DISTRICT COURT DISTRICT OF MINNESOTA Civil No. 04-2720(DSD/SRN) Christofar Atak, Plaintiff, v. ORDER Gregory Siem, in his individual capacity and as an officer of the Rochester Police Department, John Doe and Richard Roe, in their supervisory capacities as ranking Rochester police officers, and the City of Rochester, Defendants. Robert Bennett, Esq., Eric Hageman, Esq., Andrew Noel, Esq. and Flynn, Gaskins &...»





 
<<  HOME   |    CONTACTS
2016 www.dissertation.xlibx.info - Dissertations, online materials

Materials of this site are available for review, all rights belong to their respective owners.
If you do not agree with the fact that your material is placed on this site, please, email us, we will within 1-2 business days delete him.