Introduction to the Image Generator Service

Welcome to the third lesson of our course on building an image generation service in Java! In our previous lessons, we created the PromptManager to format user inputs into detailed prompts and the ImageManager to handle storing and processing generated images. Now, we're ready to build the core component that brings everything together: the ImageGeneratorService.

The ImageGeneratorService is the central piece of our application that will:

  1. Connect to Google's Gemini API to generate images.
  2. Use our PromptManager to format user inputs into effective prompts.
  3. Pass the selected aspect ratio through to the prompt template.
  4. Store generated images using our ImageManager.
  5. Provide access to all previously generated images.

This service acts as the bridge between our application's components and the external AI service that actually creates the images. By encapsulating all the image generation logic in a dedicated service class, we maintain a clean separation of concerns in our application architecture.

In this lesson, we'll implement this service step by step, from setting up the client to handling responses and errors. By the end, you'll have a fully functional image generation service that you can later integrate into a Java web application.

Setting Up the Gemini API Client

Before we can generate images, we need to set up a way to communicate with Google's Gemini API. We'll use the Gemini Java SDK to call the generateContent flow with the gemini-3.1-flash-image model.

To do this, you'll need the following dependencies in your project:

These libraries let us call the API through the SDK and work with typed response objects instead of building raw HTTP requests by hand.

Next, let's create our ImageGeneratorService class and set up the service configuration in the constructor. In Java, you would typically place this class in a package such as com.codesignal.services and save it in a file named ImageGeneratorService.java:

Implementing the Image Generation Logic with the SDK

Now that we have our service configuration set up, let's implement the core method of our service: generateImage(). This method will take a userInput string and an aspectRatio string, format them into a detailed prompt using our PromptManager, call the Gemini API through the SDK, and store the resulting image using our ImageManager.

Here is the implementation:

Error Handling and Service Integration

Generating images through an external API can fail for various reasons: network issues, API limits, invalid prompts, or server errors. To make our service robust, we include checks for missing image data and throw clear exceptions when needed.

There are also Gemini-specific failure cases to be aware of. The generateContent response may return successfully but not include an image inlineData part — for example, if the model returns only text or if the content was filtered. Our code handles this explicitly by checking whether imageBytes is still null after iterating through all parts, and throwing a RuntimeException with a clear message in that case.

Now, let's add one more method to our service to retrieve all previously generated images:

This simple method delegates to our ImageManager's getImages() method, returning the complete list of stored images along with their associated prompts and IDs.

With these two methods, our ImageGeneratorService provides a complete interface for generating and retrieving images. The service integrates our previously built components (PromptManager and ) and connects them to the external using the .

ImageGeneratorService Complete Implementation
Testing the Complete Service

Now that we've implemented our ImageGeneratorService, let's create a test class to verify that it works correctly. You can do this with a simple main method in a Java class, such as Main.java:

In this test code, we:

  1. Import our ImageGeneratorService class.
  2. Define a sample userInput and aspectRatio for testing.
  3. Create an instance of our ImageGeneratorService.
  4. Call the generateImage() method with our sample input.
  5. Print the result.
  6. Retrieve and print all stored images.

When running this code with a valid API key and the correct dependencies, you would see output similar to:

Summary and Practice Preview

In this lesson, we've built the ImageGeneratorService, the core component of our image generation application. This service connects our previously built components (PromptManager and ImageManager) to Google's Gemini API using the Gemini Java SDK, calling the official generateContent flow with the gemini-3.1-flash-image model to generate high-quality images from text prompts.

Let's review what we've learned:

  1. We set up the Gemini Java SDK to communicate with Google's generateContent API in Java.
  2. We implemented the generateImage() method, calling the generateContent flow with the gemini-3.1-flash-image model and configuring image output via responseModalities.
  3. We passed the selected aspectRatio through the prompt formatting step.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal