A problem with functionality

With Batik SVG, and Implementation

Page 1 of 1

5 Replies - 506 Views - Last Post: 14 June 2010 - 06:20 PM Rate Topic: -----

#1 Matamane  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

A problem with functionality

Posted 14 June 2010 - 05:18 PM

I've been doing a lot of experimentation with Batik SVG, and my most recent escapade into this realm has yielded this.

import java.awt.Image;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.swing.JSVGCanvas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.svg.SVGDocument;

public class DOMRasterizer {

    public SVGDocument createDocument(Image i) {

        // Create a new document.
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
        SVGDocument document = (SVGDocument) impl.createDocument(svgNS, "svg", null);
        SVGGraphics2D g = new SVGGraphics2D(document);
        g.drawImage(i, null, null);
        Element root = document.getDocumentElement();
        g.getRoot(root);
        return document;
    }

    public void save(Document document) throws Exception {

        // Create a JPEGTranscoder and set its quality hint.
        JPEGTranscoder t = new JPEGTranscoder();
        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
                             new Float(.9));

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

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

    public File load(String newfile) throws FileNotFoundException
    {
        try
        {
            File file = new File(newfile);
            FileInputStream fistream = new FileInputStream(file);
            DataInputStream distream = new DataInputStream(fistream);
            distream.readInt();
            distream.close();
            return file;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws Exception
    {
        DOMRasterizer rasterizer = new DOMRasterizer();
        File file = rasterizer.load("imageNO.jpg");
        Image i = ImageIO.read(file);
        Document document = rasterizer.createDocument(i);
        rasterizer.save(document);
    }
}




What this does is it takes an file, retrieves an image from it, and creates an SVG Document using a SVG graphic which draws the retrieved image. It functions perfectly fine, except for one issue. When I check out the saved jpg, and I try zooming in on it, there is still a lot of blur, as if it had not been changed to an SVG. Is the problem with the tools I am using, that there is a problem with how I am saving it, or that I am not creating a SVG version of that image when I am creating the SVG document?

Any help would be great

Is This A Good Question/Topic? 0
  • +

Replies To: A problem with functionality

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: A problem with functionality

Posted 14 June 2010 - 06:09 PM

Quote

When I check out the saved jpg

That right there is probably your problem. SVG is a different image format, just like JPEG, GIF, TIFF, or PNG. So you should be creating an SVG File, not a JPEG File.
Was This Post Helpful? 1
  • +
  • -

#3 Matamane  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: A problem with functionality

Posted 14 June 2010 - 06:11 PM

View Postmacosxnerd101, on 14 June 2010 - 05:09 PM, said:

Quote

When I check out the saved jpg

That right there is probably your problem. SVG is a different image format, just like JPEG, GIF, TIFF, or PNG. So you should be creating an SVG File, not a JPEG File.



Would there be a way to save a scalable vector graphic image as a jpeg in that case?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: A problem with functionality

Posted 14 June 2010 - 06:13 PM

No. Scalable Vector Graphics should be saved as image.svg.
Was This Post Helpful? 0
  • +
  • -

#5 Matamane  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: A problem with functionality

Posted 14 June 2010 - 06:16 PM

View Postmacosxnerd101, on 14 June 2010 - 05:13 PM, said:

No. Scalable Vector Graphics should be saved as image.svg.


Then what's the point of org.apache.batik.transcoder
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10564
  • View blog
  • Posts: 39,091
  • Joined: 27-December 08

Re: A problem with functionality

Posted 14 June 2010 - 06:20 PM

It looks like to provide support for converting images between various formats, including SVG. When you convert a JPEG to a PNG, the PNG isn't a JPEG. As SVG is an image format, when you convert SVG to JPEG, JPEG isn't an SVG.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1