Imagen 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](https://firebase.google.com/docs/vertex-ai/models) from your Android app using the
[Firebase AI Logic SDK.](https://firebase.google.com/docs/vertex-ai/generate-images-imagen?platform=android) Imagen models are available using both
Firebase AI Logic [API providers](https://developer.android.com/ai/gemini#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.](https://developer.android.com/static/ai/assets/images/firebase-ai-logic-imagen.svg) **Figure 1.** Access Imagen models using Firebase AI Logic. **Note:** Firebase AI Logic doesn't yet support all the features available for the server-side integrations of Imagen models. Learn more about the supported capabilities in the [Firebase documentation](https://firebase.google.com/docs/vertex-ai/generate-images-imagen?platform=android#capabilities-features).

## Experiment with prompts

Creating the ideal prompts often takes multiple attempts. You can experiment
with image prompts in [Google AI Studio](https://aistudio.google.com/gen-media), an IDE for prompt
design and prototyping. For tips on how to improve your prompts, review the
[prompt and image attribute guide](https://cloud.google.com/vertex-ai/generative-ai/docs/image/img-gen-prompt-guide).
![A screenshot of the Google AI Studio interface,
displaying four generated images of a T-Rex with a blue backpack in a
prehistoric forest.](https://developer.android.com/static/ai/assets/images/t-rex-imagen.png) **Figure 2.** Google 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](https://firebase.google.com/docs/android/setup).

## 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:34.3.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`](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=android) 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`](https://firebase.google.com/docs/vertex-ai/safety-settings?platform=android) parameter to configure the safety
and person filters.
**Note:** Refer to the Firebase documentation for up-to-date information about [available Imagen models](https://firebase.google.com/docs/vertex-ai/models).  

### 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-4.0-generate-001",
        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-4.0-generate-001",
           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 = "A hyper realistic picture of a t-rex with a blue bagpack in a prehistoric forest",
    )
    val image = imageResponse.images.first
    val bitmapImage = image.asBitmap()

### Java

    CompletableFuture<GenerateContentResponse> futureResponse =
        model.generateContent(
            Content.newBuilder()
                .addParts(
                    Part.newBuilder()
                        .setText("A hyper realistic picture of a t-rex with a blue bagpack in a prehistoric forest")
                        .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();
    }