|
| 1 | +--- |
| 2 | +title: "Generating Media with Nano Banana 2 Lite and Gemini Omni Flash in Java" |
| 3 | +date: 2026-06-30T20:00:00+02:00 |
| 4 | +draft: false |
| 5 | +tags: ["Java", "Gemini", "AI", "Generative AI"] |
| 6 | +categories: ["Programming"] |
| 7 | +--- |
| 8 | + |
| 9 | +Google just announced the launch of some incredibly exciting new models: **Nano Banana 2 Lite** (the fastest, most cost-efficient Gemini Image model yet) and **Gemini Omni Flash** for high-quality video and conversational editing. You can read all about the announcement on the [Google Blog](https://blog.google/innovation-and-ai/models-and-research/gemini-models/gemini-omni-flash-nano-banana-2-lite/). |
| 10 | + |
| 11 | +As soon as I saw the news, I couldn't wait to get my hands dirty. I wanted to see how easy it would be to generate images and videos using the [Gemini Interactions API Java SDK](https://github.com/glaforge/gemini-interactions-api-sdk). |
| 12 | + |
| 13 | +Spoiler alert: it's incredibly simple! In this post, I'll walk you through how to invoke both of these models to generate an image and a video natively in Java. |
| 14 | + |
| 15 | +## Setting Up |
| 16 | + |
| 17 | +First, make sure you have the Gemini Interactions API SDK in your project. If you're using Maven, just add the dependency: |
| 18 | + |
| 19 | +```xml |
| 20 | +<dependency> |
| 21 | + <groupId>io.github.glaforge</groupId> |
| 22 | + <artifactId>gemini-interactions-api-sdk</artifactId> |
| 23 | + <version>0.11.0</version> |
| 24 | +</dependency> |
| 25 | +``` |
| 26 | + |
| 27 | +Also, ensure that your `GEMINI_API_KEY` environment variable is set. |
| 28 | + |
| 29 | +## Generating an Image with Nano Banana 2 Lite |
| 30 | + |
| 31 | +The model identifier for the new image model is `gemini-3.1-flash-lite-image`. Generating an image is as simple as creating a `ModelInteractionParams` request, specifying the `IMAGE` response modality, and extracting the binary data from the interaction. |
| 32 | + |
| 33 | +```java |
| 34 | +import io.github.glaforge.gemini.interactions.GeminiInteractionsClient; |
| 35 | +import io.github.glaforge.gemini.interactions.model.*; |
| 36 | +import io.github.glaforge.gemini.interactions.model.InteractionParams.ModelInteractionParams; |
| 37 | +import io.github.glaforge.gemini.interactions.model.Interaction.Modality; |
| 38 | +import java.nio.file.Files; |
| 39 | +import java.nio.file.Paths; |
| 40 | + |
| 41 | +public class MediaGenerator { |
| 42 | + public static void main(String[] args) throws Exception { |
| 43 | + // Initialize the client |
| 44 | + GeminiInteractionsClient client = GeminiInteractionsClient.builder() |
| 45 | + .apiKey(System.getenv("GEMINI_API_KEY")) |
| 46 | + .build(); |
| 47 | + |
| 48 | + // Prepare the image request |
| 49 | + ModelInteractionParams imageRequest = ModelInteractionParams.builder() |
| 50 | + .model("gemini-3.1-flash-lite-image") |
| 51 | + .input("A highly detailed realistic banana wearing sunglasses on a beach") |
| 52 | + .responseModalities(Modality.IMAGE) |
| 53 | + .build(); |
| 54 | + |
| 55 | + // Execute the interaction and save the image |
| 56 | + Interaction imageInteraction = client.create(imageRequest); |
| 57 | + |
| 58 | + if (imageInteraction.outputImage() != null) { |
| 59 | + byte[] imageData = imageInteraction.outputImage().data(); |
| 60 | + Files.write(Paths.get("nano-banana.png"), imageData); |
| 61 | + System.out.println("Image saved successfully!"); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Generating a Video with Gemini Omni Flash |
| 68 | + |
| 69 | +The video generation process is practically identical! The model identifier for Omni Flash is `gemini-omni-flash-preview`. We simply switch the model name and update the `responseModalities` to `Modality.VIDEO`. |
| 70 | + |
| 71 | +*Note: You may need to ensure you're on the absolute latest version of the SDK to support the `VIDEO` modality!* |
| 72 | + |
| 73 | +```java |
| 74 | +// Prepare the video request |
| 75 | +ModelInteractionParams videoRequest = ModelInteractionParams.builder() |
| 76 | + .model("gemini-omni-flash-preview") |
| 77 | + .input("A banana jumping into a pool of water") |
| 78 | + .responseModalities(Modality.VIDEO) |
| 79 | + .build(); |
| 80 | + |
| 81 | +// Execute the interaction and save the video |
| 82 | +Interaction videoInteraction = client.create(videoRequest); |
| 83 | + |
| 84 | +if (videoInteraction.outputVideo() != null) { |
| 85 | + byte[] videoData = videoInteraction.outputVideo().data(); |
| 86 | + Files.write(Paths.get("omni-banana.mp4"), videoData); |
| 87 | + System.out.println("Video saved successfully!"); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## The Results! |
| 92 | + |
| 93 | +With just a few lines of Java code, the SDK abstracts away all the HTTP calls and multipart response parsing, giving you the direct byte arrays for your generated media. |
| 94 | + |
| 95 | +*(If you are following along, don't forget to embed the generated `nano-banana.png` and `omni-banana.mp4` files here to show off the results!)* |
| 96 | + |
| 97 | +The Interactions API keeps getting more powerful, and the Java SDK is keeping right up with it. Let me know what you end up building with Nano Banana 2 Lite and Omni Flash! |
0 commit comments