Hi guys,

I am trying to use BATIK to create JPG-images from SVG-DOM-trees which are
retrieved from a directory containing svg-files. The images contain mostly
basic geometric shapes (lines, polylines, circles, rectangles and text). The
following code works just fine, unless it comes to really large pictures -
e.g. 800x3500. As one might expect the engine runs out of memory and the
application crashes. 
My question: is there any way to solve this problem or at least find an
acceptable workaround that works also for even bigger images up to about
5000x5000? I am very much looking forward to your answers.

Best regards,
Max Hirschberger

public class ImageGenerator {

        public static void createJPEGS(String sourceDir, String targetDir) {

                try {
                        File source = new File(sourceDir);
                        /*
                         * Get all SVG-files from the specified directory
                         */
                        String[] svgFiles = source.list(new FilenameFilter() {

                                public boolean accept(File directory, String 
filename) {

                                        if (filename.endsWith(".svg"))
                                                return true;
                                        return false;

                                }
                        });
                        for (int i = 0; i < svgFiles.length; i++) {

                                int count = svgFiles[i].lastIndexOf(".");
                                String filename = svgFiles[i].substring(0, 
count);
                                String destination = targetDir + filename + 
".jpg";
                                String destinationThumbnail = targetDir + 
filename
                                                + "_thumbnail.jpg";

                                String sourceFile = sourceDir + svgFiles[i];

                                
                                String parser = 
XMLResourceDescriptor.getXMLParserClassName();
                                SAXSVGDocumentFactory f = new 
SAXSVGDocumentFactory(parser);
                                String svgUri = new 
File(sourceFile).toURI().toString();
                                Document doc = f.createDocument(svgUri);

                                int[] size = getSVGSize(doc);

                                /*
                                 * Add height and width to the svg so it is 
displayed in a
                                 * normal fashion
                                 */
                                XMLSupportMethods.addAttribute((Element) 
doc.getFirstChild(),
                                                "width", 
Integer.toString(size[0]));
                                XMLSupportMethods.addAttribute((Element) 
doc.getFirstChild(),
                                                "height", 
Integer.toString(size[1]));
                                XMLSupportMethods
                                                .storeDocumentToFile(doc, new 
File(sourceFile));
                                /*
                                 * Add area of Interest that determines which 
part of the SVG is
                                 * displayed in the JPG; important or tooltip 
creation
                                 */
                                int aoiSize = Math.min(size[0], size[1]);
                                Svg2Jpg(doc, new File(destination), 
size[0]+100, size[1]+100,
                                                new Rectangle(0, 0, 
size[0]+100, size[1]+100));
                                Svg2Jpg(doc, new File(destinationThumbnail), 
120, 150,
                                                new Rectangle(0, 0, aoiSize, 
aoiSize
                                                                - ((int) 0.2 * 
aoiSize)));

                        }
                } catch (TranscoderException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }
/**
 * Function taking a svg dom and converting it to a jpeg file
 * @param document SVG-DOM
 * @param destination where to save
 * @param width width of JPEG-Image
 * @param height height of JPEG-Image
 * @param areaOfInterest Determines which Part of the svg will be displayed.
 * @throws TranscoderException
 * @throws IOException
 */
        public static void Svg2Jpg(Document document, File destination, int 
width,
                        int height, Rectangle areaOfInterest) throws 
TranscoderException,
                        IOException {

                // Create Transcoder and set its quality and size hints
                JPEGTranscoder t = new JPEGTranscoder();
                t.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, new 
Float(height));
                t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new 
Float(width));
                t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
                t.addTranscodingHint(JPEGTranscoder.KEY_AOI, areaOfInterest);

                // Set the transcoder input and output.
                TranscoderInput input = new TranscoderInput(document);
                OutputStream ostream = new FileOutputStream(destination);
                TranscoderOutput output = new TranscoderOutput(ostream);

                // Perform the transcoding.
                t.transcode(input, output);
                ostream.flush();
                ostream.close();

        }
-- 
View this message in context: 
http://www.nabble.com/out-of-memory-while-creating-large-jpg-from-svg-tp15663329p15663329.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to