SVG files are Scalable Vector Graphics mainly used on the web. Technically, SVG is an XML-based vector image format for defining 2-dimensional graphics with animation support. SVG Images have high pixel density, so these hardly reduce their quality on zoom-in. These features make the SVG images larger in size as compared to JPG images. Due to differences in features, there comes the need to convert images from one format to another. This article discusses how to convert SVG vectors to JPG images in Java.

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

Java API to Convert Images

GroupDocs.Conversion equips Java developers with its API to convert various images into different formats. The conversion results can be further customized using multiple advanced options. I will use this Java API, GroupDocs.Conversion for Java to convert the SVG vector files into JPG 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 pox.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 JPG Image in Java

Let’s start with the basic conversion with the default settings. The following steps show how to convert the SVG vector graphic into JPG image format in Java.

  • Load the SVG graphic using the Converter class.
  • Prepare the image conversion options.
  • Set the conversion file type as JPG.
  • Convert to JPG image using the appropriate convert() method with prepared options.

The following Java source code converts the SVG to a JPG image.

SVG to JPG Image using Advanced Options in Java

While converting the vector graphics, many other options can be applied to JPG output to customize the image according to the requirement. The following are some of the properties that can be adjusted while SVG to JPG conversion:

  • Brightness
  • Contrast
  • Gamma
  • Grayscale
  • Flip Mode (Horizontal / Vertical)
  • Rotation Angles
  • Height
  • Width
  • Watermark

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

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

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

Get a Free API License

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

Conclusion

To conclude, we learned to convert SVG vector graphics into JPG format within Java applications. We also discussed how we can rotate, flip, or watermark SVG files when converted to JPG. Furthermore, we adjusted brightness, gamma, and grayscale options.

Try building your own image conversion Java application, you may learn more about the low code and high code Java 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