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 Vertex AI in Firebase SDK.

Access Imagen 3 models using Vertex AI in Firebase
Figure 1. Access Imagen 3 models using Vertex AI in Firebase.

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.

Vertex AI Studio can help you refine your image generation prompts
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 app file:

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:33.9.0"))

    implementation("com.google.firebase:firebase-vertexai")
}

Imagen 3 in Vertex AI in Firebase requires Firebase BOM 33.10.0 or higher.

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 imagenModel = Firebase.vertexAI.imagenModel(
  modelName = "imagen-3.0-generate-001",
  generationConfig = ImagenGenerationConfig(
    negativePrompt = "frogs",
    numberOfImages = 2,
    aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9,
    imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100),
    addWatermark = false
  ),
  safetySettings = ImagenSafetySettings(
    safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
    personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL
  )
)

Java

GenerativeModel imagenModel = FirebaseVertexAI.getInstance().imagenModel(
    "imagen-3.0-generate-001",
    ImagenGenerationConfig.builder()
        .setNegativePrompt("frogs")
        .setNumberOfImages(2)
        .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9)
        .setImageFormat(ImagenImageFormat.jpeg(100))
        .setAddWatermark(true)
        .build(),
    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 = imagenModel.generateImages(
  prompt = "An astronaut riding a horse",
)
val image = imageResponse.images.first
val bitmapImage = image.asBitmap()

Java

CompletableFuture<GenerateContentResponse> futureResponse =
    imagenModel.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();
}

Next steps