Welcome to the second lesson of our course on building an image generation service with Java. In our previous lesson, we created the PromptManager class to handle the formatting of prompts for our image generation process. Now, we'll build upon that foundation by creating the ImageManager class, which will be responsible for handling the images once they're generated.
The ImageManager serves as a crucial component in our Java web application architecture. While the PromptManager prepares the instructions for image generation, the ImageManager takes care of what happens after an image is created. Its primary responsibilities include:
- Storing generated images along with their associated prompts
- Converting images to a web-friendly format (
Base64) - Providing access to the collection of stored images
This component is essential because web applications can't directly work with raw image data. By converting images to Base64 strings, we can easily embed them in HTML or send them as JSON responses in our API. Additionally, keeping track of which prompt generated which image allows us to maintain a history of generations and potentially reuse successful prompts.
Let's dive into building this important piece of our image generation service in Java.
To begin, we'll create a new file called ImageManager.java in our src/main/java/com/codesignal/models directory. This file will contain our ImageManager class. First, let's set up the basic structure and understand the imports we'll need:
Let's break down these imports:
java.util.Base64: Provides methods for encoding and decodingBase64, which we'll use to convert our images to a string format that can be easily transmitted overHTTP.java.util.Map: Lets us store image metadata in key-value pairs.java.util.HashMap: Provides a concreteMapimplementation for storing each image record.java.awt.image.BufferedImage: Represents an image with an accessible buffer of image data.ByteArrayInputStream: Reads from a byte array in memory.ByteArrayOutputStream: Writes to a byte array in memory.
One of the key functions of our ImageManager class is to convert image data into a format that can be easily used in web applications. Base64 encoding is perfect for this purpose, as it transforms binary data into a string of ASCII characters that can be included directly in HTML or JSON.
Let's implement the imageToBase64 method:
This method takes a byte[] as input, which represents the raw image data. In the next lesson, these bytes will come from a Gemini API generateContent response — specifically, the binary data extracted from an image inline_data part returned by gemini-3.1-flash-image. Let's walk through what's happening:
- We use
ImageIO.read()with aByteArrayInputStreamto read theimageBytesinto a .
Now that we can convert images to Base64, let's implement the method for adding images to our collection:
This method performs several important tasks:
- It calls our
imageToBase64method to convert the image data to aBase64string. - It creates a
Map<String, Object>containing:- An
idbased on the current size of theimageslist - The
promptthat was used to generate the image - The
Base64-encoded image data
- An
- It appends this map to our
imageslist. - It returns the
imageBase64string, which can be immediately used by the caller if needed.
Let's implement a method to retrieve all stored images:
This method simply returns the entire List of image maps, allowing other parts of our application to access all stored images and their associated metadata.
Now that we've implemented our ImageManager class, let's create a simple test class to verify that it works correctly. We'll create this in our src/main/java/com/codesignal directory as Main.java:
In this test class, we:
- Import our
ImageManagerclass. - Create an instance of the
ImageManager. - Generate a simple in-memory test image and convert it to bytes.
- Call the
addImagemethod with our test data, handling any exceptions.
In this lesson, we've built the ImageManager class, a crucial component of our image generation service. This class handles the storage and processing of generated images, converting them to a web-friendly format and maintaining a collection of all images along with their associated prompts.
Let's review what we've learned:
- We created a class structure with a simple in-memory storage mechanism using
Javacollections. - We implemented a method to convert image data to
Base64format, making it suitable for web applications. - We built methods to add images to our collection and retrieve all stored images.
- We created a simple test class to verify the functionality of our
ImageManager.
The ImageManager complements the PromptManager we built in the previous lesson. While the PromptManager prepares the instructions for image generation, the ImageManager handles the results of that generation process. Together, these components form the foundation of our image generation service in Java.
In the upcoming practice exercises, you'll have the opportunity to work with the ImageManager class, testing its functionality with different types of image data and exploring how it integrates with the rest of our application. You'll also get to experiment with error handling and see how the class behaves in various scenarios.
In our next lesson, we'll build upon this foundation by implementing the ImageGeneratorService, which will connect to the using the flow with to actually generate images based on our prompts. This service will use both the and classes we've created so far.
