Generate images with Imagen 3

Imagen 3 is an image generation model. It can be used to generate custom avatars for user profiles or to integrate personalized visual assets into existing screen flows to increase user engagement.

You can access Imagen models from your Android app using the Firebase AI Logic SDK. Imagen models are available using both Firebase AI Logic API providers: Gemini Developer API (recommended for most developers) and Vertex AI.

A diagram illustrating a Firebase AI Logic integration architecture
       to access the Gemini Developer API. An Android App utilizes the Firebase
       Android SDK to connect to Firebase. Firebase then interacts with the
       Gemini Developer API, which accesses Gemini Pro & Flash within the
       cloud.
Figure 1. Access Imagen 3 models using Firebase AI Logic.

Experiment with prompts

Creating the ideal prompts often takes multiple attempts. You can experiment with image prompts in Vertex AI Studio, an IDE for prompt design and prototyping. For tips on how to improve your prompts, review the prompt and image attribute guide.

A screenshot of the Google Cloud Vertex AI Studio interface,
      displaying four generated images of a T-Rex with a blue backpack in a
      prehistoric forest. The left sidebar shows navigation options like
      Dashboard, Model Garden, and Vision, while the right panel contains
      Parameters for image generation settings.
Figure 2. Vertex AI Studio can help you refine your image generation prompts.

Set up a Firebase project and connect your app

Follow the steps in the Firebase documentation to add Firebase to your Android project.

Add the Gradle dependency

Add the following dependencies to your build.gradle file:

dependencies {
  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:33.14.0"))

  // Add the dependency for the Firebase AI Logic library. When using the BoM,
  // you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
}

Generate an image

To generate an image in your Android app, start by instantiating an ImagenModel with an optional configuration.

You can use the generationConfig parameter to define a negative prompt, the number of images, the output image aspect ratio, the image format and add a watermark. You can use the safetySettings parameter to configure the safety and person filters.

Kotlin

val config = ImagenGenerationConfig {
    numberOfImages = 2,
    aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9,
    imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100),
    addWatermark = false
}

// Initialize the Gemini Developer API backend service
// For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI())
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel(
    modelName = "imagen-3.0-generate-002",
    generationConfig = config,
    safetySettings = ImagenSafetySettings(
       safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
       personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL
    )
)

Java

ImagenGenerationConfig config = new ImagenGenerationConfig.Builder()
    .setNumberOfImages(2)
    .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9)
    .setImageFormat(ImagenImageFormat.jpeg(100))
    .setAddWatermark(false)
    .build();

// For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI())
ImagenModelFutures model = ImagenModelFutures.from(
    FirebaseAI.ai(backend = GenerativeBackend.googleAI()).imagenModel(
       "imagen-3.0-generate-002",
       config,
       ImagenSafetySettings.builder()
          .setSafetyFilterLevel(ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE)
          .setPersonFilterLevel(ImagenPersonFilterLevel.BLOCK_ALL)
          .build())
);

Once your ImagenModel is instantiated, you can generate images by calling generateImages:

Kotlin

val imageResponse = model.generateImages(
  prompt = "An astronaut riding a horse",
)
val image = imageResponse.images.first
val bitmapImage = image.asBitmap()

Java

CompletableFuture<GenerateContentResponse> futureResponse =
    model.generateContent(
        Content.newBuilder()
            .addParts(
                Part.newBuilder()
                    .setText("An astronaut riding a horse")
                    .build())
            .build());

try {
  GenerateContentResponse imageResponse = futureResponse.get();
  List<GeneratedImage> images =
      imageResponse
          .getCandidates(0)
          .getContent()
          .getParts(0)
          .getInlineData()
          .getImagesList();

  if (!images.isEmpty()) {
    GeneratedImage image = images.get(0);
    Bitmap bitmapImage = image.asBitmap();
    // Use bitmapImage
  }
} catch (ExecutionException | InterruptedException e) {
  e.printStackTrace();
}