From a813f61a0e600e352310f6f1bc96606cf27a34ec Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 15 Sep 2011 18:35:25 +0000 Subject: Allow the generation of jpg git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1171217 13f79535-47bb-0310-9956-ffa450edef68 --- .../CompositeDiagramGeneratorUsingBatik/pom.xml | 20 +++- .../org/apache/tuscany/sca/diagram/main/Main.java | 116 +++++++++++++++++++-- .../diagram/layout/DiagramGeneratorTestCase.java | 2 +- 3 files changed, 126 insertions(+), 12 deletions(-) (limited to 'collaboration/GSoC-2011-Nirmal') diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml index e38c9badb6..3a3b4b4ec6 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/pom.xml @@ -30,6 +30,8 @@ commons-cli 1.2 + + org.apache.xmlgraphics - batik-ext + batik-dom 1.7 + + org.apache.xmlgraphics batik-parser @@ -90,6 +95,12 @@ 1.7 + + org.apache.xmlgraphics + batik-codec + 1.7 + + xml-apis xml-apis-ext @@ -127,6 +138,7 @@ + org.apache.maven.plugins maven-jar-plugin diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java index 8224e47955..2ce360a9c3 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/diagram/main/Main.java @@ -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) { @@ -141,9 +162,9 @@ public class Main { HTMLWrapper html = new HTMLWrapper(svg, comp.getName(), htmlFile); html.buildHTML(); -// System.err.println("--------------HTML Output for " + comp.getName() + "--------------\n"); -// System.out.println(content); -// System.err.println("--------------------------------------------------------------\n"); + // System.err.println("--------------HTML Output for " + comp.getName() + "--------------\n"); + // System.out.println(content); + // System.err.println("--------------------------------------------------------------\n"); } @@ -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(); + } + } diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java index aee42dbfc0..ee0feb86bb 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/test/java/org/apache/tuscany/sca/diagram/layout/DiagramGeneratorTestCase.java @@ -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()); } } } -- cgit v1.2.3