Created
March 20, 2012 09:33
-
-
Save roopeshvaddepally/2133392 to your computer and use it in GitHub Desktop.
Batik Service. Take svg, image_type as input. and convert the svg to image of given type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ly.visual; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.Date; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.batik.dom.svg.SAXSVGDocumentFactory; | |
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.batik.util.XMLResourceDescriptor; | |
import org.w3c.dom.Document; | |
@WebServlet("/BatikService") | |
public class BatikService extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
public BatikService() { | |
super(); | |
} | |
/* | |
* Example svg: | |
* <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> | |
* <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" /> | |
* </svg> | |
* | |
* encode the svg to url format with http://www.motobit.com/util/url-encoder.asp | |
* | |
* http://localhost:8080/BatikService1/BatikService?image_type=png&svg=%3Csvg+xml... | |
* where the ellipsis are the escaped svg | |
* | |
* Post request has max post size limit set to 2MB (http://tomcat.apache.org/tomcat-5.5-doc/config/http.html) | |
* Fix it by changing server.xml to something like below which accepts unlimited size posts(totally dangerous): | |
* <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" maxPostSize="0"/> | |
*/ | |
protected void doGet(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
Date d1 = new Date(); | |
String svg = request.getParameter("svg"); | |
String imageType = request.getParameter("image_type"); | |
InputStream inputStream = new ByteArrayInputStream(svg.getBytes()); | |
OutputStream outputStream = response.getOutputStream(); | |
Document doc = getDocument(inputStream); | |
TranscoderInput input = new TranscoderInput(doc); | |
TranscoderOutput output = new TranscoderOutput(outputStream); | |
try { | |
Transcoder transcoder = getTranscoder(imageType, 0.7f); | |
transcoder.transcode(input, output); | |
} catch (TranscoderException e) { | |
e.printStackTrace(); | |
} | |
Date d2 = new Date(); | |
System.out.println(d2.getTime() - d1.getTime()); | |
response.setContentType("image/" + imageType); | |
} | |
protected void doPost(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
doGet(request, response); | |
} | |
private Document getDocument(InputStream inputStream) throws IOException { | |
String parser = XMLResourceDescriptor.getXMLParserClassName(); | |
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); | |
Document doc = f.createDocument("http://www.w3.org/2000/svg", | |
inputStream); | |
return doc; | |
} | |
private Transcoder getTranscoder(String transcoderType, float keyQuality) { | |
Transcoder transcoder = null; | |
if (transcoderType.equals("jpeg")) { | |
transcoder = getJPEGTranscoder(keyQuality); | |
} else if (transcoderType.equals("png")) { | |
transcoder = getPNGTranscoder(); | |
} | |
return transcoder; | |
} | |
private JPEGTranscoder getJPEGTranscoder(float keyQuality) { | |
JPEGTranscoder jpeg = new JPEGTranscoder(); | |
jpeg.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float( | |
keyQuality)); | |
return jpeg; | |
} | |
private PNGTranscoder getPNGTranscoder() { | |
return new PNGTranscoder(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment