I'm still struggling to figure out a way to improve the quality of my SVG 
images in Batik.

It seems this maybe an Antialiasing issue? I can't seem to find a transcoder 
hint that will improve this.

Thanks,
Mark

From: Mark Riley [mailto:mark.ri...@seriousintegrated.com]
Sent: Friday, August 23, 2013 2:41 PM
To: batik-users@xmlgraphics.apache.org
Subject: SVG BufferedImage Quality

I have a question about how to improve the quality of the images I'm generating 
for buffered images.

[cid:image001.png@01CEA339.F3DD19E0]

The left image is the image I generated with Batik while the right is the 
original. How do I generate a Buffered Image of the same quality? My code 
currently looks like this:

Float width;
        Float height;

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[image.getStream().available()];
            int n = 0;
            while ((n = image.getStream().read(buf)) >= 0) {
                baos.write(buf, 0, n);
            }
            byte[] content = baos.toByteArray();


            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            Document doc = f.createDocument(image.getFilename(), new 
ByteArrayInputStream(content));
            Element element = doc.getDocumentElement();
            if (element.hasAttributeNS(null, "viewBox")) {
                String[] parts = element.getAttribute("viewBox").split("\\ ");
                width = new Float(parts[2]);
                height = new Float(parts[3]);
            } else if (element.hasAttributeNS(null, "height") && 
element.hasAttributeNS(null, "width")) {
                height = new Float(element.getAttributeNS(null, "height"));
                width = new Float(element.getAttributeNS(null, "width"));
            } else {
                width = new Float(100);
                height = new Float(100);
            }

            ShipTranscoder transcoder = new ShipTranscoder();
            TranscodingHints hints = new TranscodingHints();
            DOMImplementation impl = 
SVGDOMImplementation.getDOMImplementation();
            hints.put(ImageTranscoder.KEY_WIDTH, width);
            hints.put(ImageTranscoder.KEY_HEIGHT, height);
            hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, impl);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, 
SVGConstants.SVG_NAMESPACE_URI);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, 
SVGConstants.SVG_NAMESPACE_URI);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, 
SVGConstants.SVG_SVG_TAG);
            hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false);
            transcoder.setTranscodingHints(hints);
            transcoder.transcode(new TranscoderInput(new 
ByteArrayInputStream(content)), null);
            image.setImage(transcoder.getImage());

        } catch (Throwable e) {
            Exceptions.printStackTrace(e);
            String error = "Invalid input stream for SVG file type.";
            setError(SHIPPROPKEY.IMAGE_SOURCEFILE, error);
            LOGGER.log(Level.SEVERE, "        {0}\n", error);
            throw new MashException(getClass().getSimpleName()
}

class ShipTranscoder extends ImageTranscoder {

        private BufferedImage image = null;

        @Override
        public BufferedImage createImage(int w, int h) {
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            return image;
        }

        @Override
        public void writeImage(BufferedImage img, TranscoderOutput out) {
        }

        public BufferedImage getImage() {
            return image;
        }
    }

Thanks,
Mark

<<inline: image001.png>>

Reply via email to