Allow the generation of jpg

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1171217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2011-09-15 18:35:25 +00:00
parent c6c38f12af
commit a813f61a0e
3 changed files with 128 additions and 14 deletions

View file

@ -30,6 +30,8 @@
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<!--
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-anim</artifactId>
@ -48,17 +50,20 @@
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-ext</artifactId>
<version>1.7</version>
</dependency>
-->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-dom</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-ext</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
@ -90,6 +95,12 @@
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis-ext</artifactId>
@ -127,6 +138,7 @@
</configuration>
</plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
@ -147,7 +159,7 @@
</execution>
</executions>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>

View file

@ -20,7 +20,11 @@
package org.apache.tuscany.sca.diagram.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
@ -30,6 +34,12 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
@ -52,9 +62,12 @@ public class Main {
Option opt2 = new Option("h", "html", false, "Generate HTML documents");
options.addOption(opt2);
Option opt3 = new Option("o", "output", true, "Output directory");
Option opt3 = new Option("j", "jpeg", false, "Generate JPEG diagrams");
options.addOption(opt3);
Option opt4 = new Option("o", "output", true, "Output directory");
options.addOption(opt4);
return options;
}
@ -86,10 +99,12 @@ public class Main {
boolean isHtml = false;
boolean isSvg = false;
boolean isJpeg = false;
isSvg = cli.hasOption('s');
isHtml = cli.hasOption('h');
isJpeg = cli.hasOption('j');
if (!isSvg && !isHtml) {
if (!isSvg && !isHtml && !isJpeg) {
isSvg = true;
isHtml = true;
}
@ -103,10 +118,11 @@ public class Main {
}
File dir = new File(outFileDir);
String[] compositeFiles = cli.getArgs();
generate(dir, isSvg, isHtml, compositeFiles);
generate(dir, isSvg, isHtml, false, compositeFiles);
}
public static void generate(File dir, boolean isSvg, boolean isHtml, String... compositeFiles) throws Exception {
public static void generate(File dir, boolean isSvg, boolean isHtml, boolean isJpeg, String... compositeFiles)
throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
for (String str : compositeFiles) {
@ -121,6 +137,11 @@ public class Main {
DiagramGenerator dg = new DiagramGenerator(comp, isHtml);
Document svg = dg.buildSVGDocument();
if (isJpeg) {
String jpgFileName = comp.getName() + comp.getFileNameSuffix() + ".jpg";
svgToJPEG(svg, new File(dir, jpgFileName));
}
String svgContent = extractSvg(svg);
if (isSvg) {
@ -174,4 +195,85 @@ public class Main {
return svgString;
}
public static void svgToJPEG(File svg, File jpeg) throws IOException, TranscoderException {
// Create the transcoder input.
TranscoderInput input = new TranscoderInput(svg.toURI().toString());
// Create the transcoder output.
OutputStream ostream = new FileOutputStream(jpeg);
TranscoderOutput output = new TranscoderOutput(ostream);
// Create a JPEG transcoder
Transcoder t = new JPEGTranscoder();
// Set the transcoding hints.
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));
// Save the image.
t.transcode(input, output);
// Flush and close the stream.
ostream.flush();
ostream.close();
}
public static void svgToJPEG(Document svg, File jpeg) throws IOException, TranscoderException {
// Create the transcoder input.
TranscoderInput input = new TranscoderInput(svg);
// Create the transcoder output.
OutputStream ostream = new FileOutputStream(jpeg);
TranscoderOutput output = new TranscoderOutput(ostream);
// Create a JPEG transcoder
Transcoder t = new JPEGTranscoder();
// Set the transcoding hints.
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));
// Save the image.
t.transcode(input, output);
// Flush and close the stream.
ostream.flush();
ostream.close();
}
public static void svgToJPEG(InputStream svg, OutputStream jpeg) throws IOException, TranscoderException {
// Create the transcoder input.
TranscoderInput input = new TranscoderInput(svg);
TranscoderOutput output = new TranscoderOutput(jpeg);
// Create a JPEG transcoder
Transcoder t = new JPEGTranscoder();
// Set the transcoding hints.
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));
// Save the image.
t.transcode(input, output);
// Flush and close the stream.
svg.close();
jpeg.flush();
jpeg.close();
}
public static void svgToPNG(InputStream svg, OutputStream png) throws IOException, TranscoderException {
// Create the transcoder input.
TranscoderInput input = new TranscoderInput(svg);
TranscoderOutput output = new TranscoderOutput(png);
// Create a JPEG transcoder
Transcoder t = new PNGTranscoder();
// Save the image.
t.transcode(input, output);
// Flush and close the stream.
svg.close();
png.flush();
png.close();
}
}

View file

@ -25,7 +25,7 @@ public class DiagramGeneratorTestCase {
for (File xml : new File("input").listFiles()) {
if (xml.getName().endsWith(".xml")) {
System.out.println(xml);
Main.generate(new File("target"), true, false, xml.toString());
Main.generate(new File("target"), true, false, false, xml.toString());
}
}
}