Importing SVG

ColinP
Posts: 939
Joined: Mon Aug 03, 2020 7:46 pm

Importing SVG

Post by ColinP »

This is probably not the best place to ask but here goes...

I can currently let users load PNG, JPEG, GIF and BMP files into a VM module but it would be nice to add direct support for SVG.

After doing about an hour's worth of research it seems that using a transcoder from Apache Batik is the way to go but I'm wandering how much is involved.

At one level I've found the code shown below which looks really simple but I have no idea how easy it is to set up the build environment for the upper batch of imports.

I know there are a few Java experts here and hopefully you've had experience of installing ASF stuff so any pointers would be handy. Is it straightforward to install the libraries or a major pain? The Apache documentation isn't exactly newbie friendly.

Code: Select all

import lombok.extern.slf4j.Slf4j;
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.PNGTranscoder;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Slf4j
@Component
public class SvgToRasterizeImageConverter {

    public BufferedImage transcodeSVGToBufferedImage(File file, int width, int height) {
        // Create a PNG transcoder.
        Transcoder t = new PNGTranscoder();

        // Set the transcoding hints.
        t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) width);
        t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) height);
        try (FileInputStream inputStream = new FileInputStream(file)) {
            // Create the transcoder input.
            TranscoderInput input = new TranscoderInput(inputStream);

            // Create the transcoder output.
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            TranscoderOutput output = new TranscoderOutput(outputStream);

            // Save the image.
            t.transcode(input, output);

            // Flush and close the stream.
            outputStream.flush();
            outputStream.close();

            // Convert the byte stream into an image.
            byte[] imgData = outputStream.toByteArray();
            return ImageIO.read(new ByteArrayInputStream(imgData));

        } catch (IOException | TranscoderException e) {
            log.error("Conversion error", e);
        }
        return null;
    }

}
Centripidity
Posts: 140
Joined: Sun Jan 22, 2023 5:18 am
Location: Melbourne
Contact:

Re: Importing SVG

Post by Centripidity »

Colin,

Sorry I can't be of much help here, especially sorry given all the help you've given me over these past few months. However, I will watch this thread with interest. I have recently tried to incorporate a large Java library (although not one from Apache) into a module and haven't yet been able to solve an issue that I think relates to the CLASSPATH environment variable. It appears that just adding the compiled Java libraries into the module is not, in my particular case at least, enough to make them able to find one another.

Peter
borkman
Posts: 51
Joined: Tue May 09, 2023 7:26 pm

Re: Importing SVG

Post by borkman »

I've had luck including a small json library (json-simple) via the designer and then importing the classes I needed. json-simple doesn't have any other dependencies, but if it did I'd think downloading and including those as well *should* work. You could try to track them all down or just watch the errors and download/include them one by one until you get them all.

BTW, Hi, I'm new. :D I hope to have some of modules out soon*.


*soon - an indeterminate amount of time based partially on the next sale so I can get a Designer license cheaper and partially on me splitting time between my hobbies to finish some of my ideas
UrbanCyborg
Posts: 588
Joined: Mon Nov 15, 2021 9:23 pm

Re: Importing SVG

Post by UrbanCyborg »

Don't know if this is helpful or not, but recently I had occasion to install some of the Apache Commons Math libraries. First problem was that the library and class names weren't the same as the package paths. For instance, anything prefixed org.apache.commons.math4.0-beta had to be manually renamed org.apache.commons.math4, even in the .jar files. Another quirk, although this may be a VM thing, is that I had to import two versions, one with no suffix, and the other with an appended asterisk. I also had to do that in the CLASSPATH environment variable, and as I recall, that was order-sensitive.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
ColinP
Posts: 939
Joined: Mon Aug 03, 2020 7:46 pm

Re: Importing SVG

Post by ColinP »

Thanks for the useful feedback from everyone and a warm welcome to borkman.

The trouble that Reid had is the kind of thing that's put me off diving striaight in, especially as I have lots of other things to sort that are more important at the moment.

I think the lombok and springframework imports and the @Slf4j and @Component annotations can all be dropped without impact, so I should be left with just the Apache stuff.

One thing I came across when digging a little deeper was the environment variable APACHE_HOME which wasn't mentioned in many places but might help. Other than that I guess CLASSPATH is the main thing but what Peter said doesn't raise hopes.

Anyway I'm going to be brave and download the binary jars and poke about for an hour. If it takes longer than that I'll put it at the bottom of my todo list (although direct SVG support would be neat thing to have in the toolbox).

I'll report back...
Centripidity
Posts: 140
Joined: Sun Jan 22, 2023 5:18 am
Location: Melbourne
Contact:

Re: Importing SVG

Post by Centripidity »

ColinP wrote: Mon May 22, 2023 12:03 pm Other than that I guess CLASSPATH is the main thing but what Peter said doesn't raise hopes.
That is more likely a problem with my lack of understanding of the Java environment than necessarily a major issue. I have to get my head around how a CLASSPATH on my local development machine translates to the compiled VM module and how to make sure the connections work on a user's pc without a local copy of that CLASSPATH variable.
ColinP
Posts: 939
Joined: Mon Aug 03, 2020 7:46 pm

Re: Importing SVG

Post by ColinP »

That's odd as CLASSPATH is undefined on my system at the mo' so I suspect it has no impact on the VM runtime.
Centripidity
Posts: 140
Joined: Sun Jan 22, 2023 5:18 am
Location: Melbourne
Contact:

Re: Importing SVG

Post by Centripidity »

ColinP wrote: Mon May 22, 2023 12:18 pm That's odd as CLASSPATH is undefined on my system at the mo' so I suspect it has no impact on the VM runtime.
I suppose what I'm saying is that you shouldn't take my difficulties with CLASSPATH as an indicator that there is necessarily a major problem incorporating large external libraries. At this stage I really don't know what I'm doing and I haven't put the work in yet to really get my head around it. All I know is that the problem I am having has been solved for users of other development environments by manipulating CLASSPATH but I haven't yet worked out how to get that solution to apply to VM.
ColinP
Posts: 939
Joined: Mon Aug 03, 2020 7:46 pm

Re: Importing SVG

Post by ColinP »

Gotcha Peter.

Anyway. I actually had a lightbulb moment when pointing CLASSPATH at the jars didn't work. I remembered that there's an Additional JAR Files entry in VMD's Property pane. I entered the library path not really expecting it to work but low and behold it actually compiled straight away!

:D

I'm going to check to see if the code actually works as advertised now.....
Centripidity
Posts: 140
Joined: Sun Jan 22, 2023 5:18 am
Location: Melbourne
Contact:

Re: Importing SVG

Post by Centripidity »

ColinP wrote: Mon May 22, 2023 12:50 pm I remembered that there's an Additional JAR Files entry in VMD's Property pane. I entered the library path not really expecting it to work but low and behold it actually compiled straight away!
Yeah, in my case I can get my module to compile but it throws a runtime exception when I call a function in one of the libraries that it claims it can't find.
Post Reply

Return to “Module Designer”