SVG (Scalable Vector Graphics) is an XML-based vector image format for defining two-dimensional graphics with animation support. These images are mainly used on web because of the high pixel density. On the other hand, the rich features also enhances the size of SVG images as compared to the PNG image having same dimension. Such differences let developers think about to convert images from one format to another. This article discusses how to convert SVG vectors to PNG images in Java.

Convert SVG to PNG in Java (Grayscale, Rotate, Flip, Bright, Watermark)

Java API to Convert Images

GroupDocs.Conversion showcases document & image conversion API for Java developers. It supports a large list of images formats for conversion from one format to another. Additionally, the conversion results can also be customized using available advanced options. I will use this API, GroupDocs.Conversion for Java to convert the SVG vector graphics into PNG image format.

Download or Configure

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pom.xml of your maven-based Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>22.3</version> 
</dependency>

Convert SVG to PNG Image in Java

Without wasting any time, let’s begin with the basic conversion with the default configuration. The following steps show how to convert the SVG vector graphics file into PNG image in Java.

  • Load the PNG graphic using the Converter class.
  • Prepare the image conversion options.
  • Set the conversion file type as PNG.
  • Call the convert() method to get the loaded SVG file converted into PNG image.

The following Java source code converts the SVG into a PNG image.

SVG to PNG Image using Advanced Options in Java

Many customization options can be applied to PNG output while converting from SVG to get the desired result. The following are some of the properties that can be adjusted while SVG to PNG conversion:

Customization Option Method
Brightness setBrightness(int value)
Contrast setContrast(int value)
Flip Mode (Horizontal / Vertical) setFlipMode(ImageFlipModes value)
Gamma setGamma(float value)
Grayscale setGrayscale(boolean value)
Height setHeight(int value)
Rotation Angles setRotateAngle(int value)
Watermark setWatermark(WatermarkTextOptions value)
Width setWidth(int value)

The following steps show how to apply variations while converting an SVG graphic to a PNG image in Java.

  • Load the SVG graphic using the Converter class.
  • Prepare the image conversion options.
  • Set the conversion file type as PNG.
  • Set other relevant properties using the respective setter methods and adjust their values accordingly.
  • Convert to PNG image using the convert() method with prepared options.

You may apply any of these effects or in combination to get the required PNG output. The following source code applies customization to the PNG image while SVG to PNG conversion in Java.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

Conclusion

To sum up, we learned to convert SVG files into PNG format within the Java applications. We furhter learned, how we can rotate, flip, or watermark PNG files when converted from SVG format. Furthermore, we discussed how to adjust brightness, gamma, and grayscale options.

You may try building your own image conversion Java application. You may learn more about the APIs from the documentation for the automation of document and image conversions.

The easiest way is to experience the examples from GitHub. Contact us for any query via the forum.

See Also