This sample is part of the AI Sample Catalog. To build and run this sample, you should clone the entire repository.
This sample demonstrates how to generate images from text prompts using the Imagen model. Users can input a text description, and the generative model will create an image based on that prompt, showcasing the power of text-to-image generation with Imagen.
The application uses the Firebase AI SDK (see How to run) for Android to interact with Imagen. The core logic is in the ImagenDataSource.kt file. An imagenModel is initialized with specific generation configurations (e.g., number of images, aspect ratio, image format). When a user provides a text prompt, it's passed to the generateImages method, which returns the generated image as a bitmap.
Here is the key snippet of code that calls the generative model from ImagenDataSource.kt:
@OptIn(PublicPreviewAPI::class)
suspend fun generateImage(prompt: String): Bitmap {
val imageResponse = imagenModel.generateImages(
prompt = prompt,
)
val image = imageResponse.images.first()
return image.asBitmap()
}Read more about Imagen in the Android Documentation.
