Write Text on JPG Images in Java

Writing text on images or photos in JPG format enhances branding and allows adding context, watermarks, or copyright information. Whether you’re creating memes, social media posts, or marketing materials, the ability to write text on JPG images is incredibly useful. In this blog post, we will learn how to add text to JPG images using Java. We’ll guide you through each step, showing you how to write on photos or other images. Let’s dive in!

This article covers the following topics:

  1. Java API to write text on JPG images
  2. Write text on JPG images in Java
  3. Add captions to photos in Java
  4. Write text on JPG images online
  5. Free resources

Java API to Write Text on JPG Images

We will use Aspose.Drawing for Java to write text on JPG images. Aspose.Drawing for Java is a robust 2D graphics library that enables developers to create and manipulate vector graphics, images, and drawings within Java applications. It provides a set of APIs for rendering graphics, drawing shapes, and handling fonts, allowing for high-quality graphical content creation and manipulation. 

Please download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.com/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-drawing</artifactId>
        <version>24.4</version>
    </dependency>
</dependencies>

Write Text on JPG Images in Java

We can easily write any kind of text on JPG images by following the steps below:

  1. Load a JPG image using the Bitmap class.
  2. Create a new Graphics object from the Bitmap object using the fromImage() method.
  3. Initialize a SolidBrush class object with the specified text color.
  4. Define a Font class object with the desired font family, style, and size for the text.
  5. Initialize a Rectangle class object.
  6. After that, call the drawString() method with the text to show, Font, Brush, and Rectangle class objects as arguments.
  7. Finally, save the output image using the save() method.

The following code sample shows how to write text on JPG in Java.

Write Text on JPG Images in Java

Write Text on JPG Images in Java

Add Caption to Photos - Add Text to Photo

We can also add a caption to a photo by following the steps below:

  1. Load a photo image using the Bitmap class.
  2. Create a new bitmap with the size of the loaded image, with the addition of a rectangle size for the caption.
  3. Create a new Graphics object from the Bitmap object using the fromImage() method.
  4. Draw the loaded image on the newly created image using the drawImage() method.
  5. Draw a filled rectangle for the caption box.
  6. Specify the text string format using the StringFormat class.
  7. Define text, its color, and font.
  8. After that, call the drawString() method with the text to show, Font, Brush, and Rectangle class objects as arguments.
  9. Finally, save the output image using the save() method.

The following code sample shows how to add a caption to a photo in Java.

Add caption to photos in Java

Add caption to photos in Java

Get a Free License

You can obtain a free temporary license to try Aspose.Drawing for Java without any evaluation limitations.

Write Text on JPG Images Online

You can also try a free online tool to write text on JPG images. With its user-friendly interface, you can easily add text to images without the need to install additional software, create an account, or subscribe to any service.

Add Text to Image – Free Learning Resources

In addition to writing text on JPG images, explore the resources below to learn more about adding text to images, displaying vector graphics on raster images, and discovering other features of the library:

Conclusion

In this blog post, we learned how to write text on JPG images in Java. Whether you are adding simple captions to the images, creating dynamic banners, or applying watermarks for branding, Aspose.Drawing for Java provides the tools and flexibility to achieve professional results. By following the steps outlined in this article, you can easily add text to images and customize fonts, colors, and positions to suit your specific needs. Furthermore, we have introduced a free online tool for adding text to images on the go. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also