KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > SVGConverter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: SVGConverter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf;
21
22 import org.apache.batik.transcoder.TranscoderException;
23 import org.apache.batik.transcoder.TranscoderInput;
24 import org.apache.batik.transcoder.TranscoderOutput;
25 import org.apache.batik.transcoder.image.JPEGTranscoder;
26 import org.apache.commons.io.output.ByteArrayOutputStream;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.fop.image.XMLImage;
30
31 /**
32  * Helper class for converting SVG to bitmap images.
33  */

34 public final class SVGConverter {
35
36     /** logger instance */
37     private static Log log = LogFactory.getLog(SVGConverter.class);
38
39     /**
40      * Constructor is private, because it's just a utility class.
41      */

42     private SVGConverter() {
43     }
44     
45     /**
46      * Converts a SVG image to a JPEG bitmap.
47      * @param image the SVG image
48      * @return a byte array containing the JPEG image
49      */

50     public static byte[] convertToJPEG(XMLImage image) {
51         JPEGTranscoder transcoder = new JPEGTranscoder();
52         /* TODO Disabled to avoid side-effect due to the mixing of source and target resolutions
53          * This should be reenabled when it has been determined how exactly to handle this
54         transcoder.addTranscodingHint(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,
55                 new Float(25.4f / 300)); //300dpi should be enough for now.
56         */

57         transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float JavaDoc(0.9f));
58         TranscoderInput input = new TranscoderInput(image.getDocument());
59         ByteArrayOutputStream baout = new ByteArrayOutputStream(16384);
60         TranscoderOutput output = new TranscoderOutput(baout);
61         try {
62             transcoder.transcode(input, output);
63             return baout.toByteArray();
64         } catch (TranscoderException e) {
65             log.error(e);
66             return null;
67         }
68     }
69     
70 }
71
Popular Tags