Firebase AI Logic SDK を使用して、Android アプリから Imagen モデルにアクセスできます。Imagen モデルは、Firebase AI Logic の API プロバイダである Gemini Developer API(ほとんどのデベロッパーに推奨)と Vertex AI の両方を使用して使用できます。
図 1.
Firebase AI Logic を使用して Imagen モデルにアクセスする。
プロンプトで試す
理想的なプロンプトを作成するには、多くの場合、何度か試す必要があります。画像プロンプトは、プロンプトの設計とプロトタイピング用の IDE である Vertex AI Studio でテストできます。プロンプトを改善する方法については、プロンプトと画像属性のガイドをご覧ください。
dependencies{// Import the BoM for the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.2.0"))// Add the dependency for the Firebase AI Logic library. When using the BoM,// you don't specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-ai")}
valconfig=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())valmodel=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
ImagenGenerationConfigconfig=newImagenGenerationConfig.Builder().setNumberOfImages(2).setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9).setImageFormat(ImagenImageFormat.jpeg(100)).setAddWatermark(false).build();// For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI())ImagenModelFuturesmodel=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()));
valimageResponse=model.generateImages(prompt="An astronaut riding a horse",)valimage=imageResponse.images.firstvalbitmapImage=image.asBitmap()
Java
CompletableFuture<GenerateContentResponse>futureResponse=model.generateContent(Content.newBuilder().addParts(Part.newBuilder().setText("An astronaut riding a horse").build()).build());try{GenerateContentResponseimageResponse=futureResponse.get();List<GeneratedImage>images=imageResponse.getCandidates(0).getContent().getParts(0).getInlineData().getImagesList();if(!images.isEmpty()){GeneratedImageimage=images.get(0);BitmapbitmapImage=image.asBitmap();// Use bitmapImage}}catch(ExecutionException|InterruptedExceptione){e.printStackTrace();}
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-30 UTC。"],[],[],null,["Imagen is an image generation model. It can be used to generate\ncustom avatars for user profiles or to integrate personalized visual assets into\nexisting screen flows to increase user engagement.\n\nYou can access [Imagen models](https://firebase.google.com/docs/vertex-ai/models) from your Android app using the\n[Firebase AI Logic SDK.](https://firebase.google.com/docs/vertex-ai/generate-images-imagen?platform=android) Imagen models are available using both\nFirebase AI Logic [API providers](/ai/gemini#api-providers): Gemini Developer API (recommended for most\ndevelopers) and Vertex AI.\n**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).\n\nExperiment with prompts\n\nCreating the ideal prompts often takes multiple attempts. You can experiment\nwith image prompts in [Vertex AI Studio](https://console.cloud.google.com/vertex-ai/generative/vision), an IDE for prompt\ndesign and prototyping. For tips on how to improve your prompts, review the\n[prompt and image attribute guide](https://cloud.google.com/vertex-ai/generative-ai/docs/image/img-gen-prompt-guide).\n**Figure 2.** Vertex AI Studio can help you refine your image generation prompts.\n\nSet up a Firebase project and connect your app\n\nFollow the steps in the Firebase documentation to\n[add Firebase to your Android project](https://firebase.google.com/docs/android/setup).\n\nAdd the Gradle dependency\n\nAdd the following dependencies to your `build.gradle` file: \n\n dependencies {\n // Import the BoM for the Firebase platform\n implementation(platform(\"com.google.firebase:firebase-bom:34.2.0\"))\n\n // Add the dependency for the Firebase AI Logic library. When using the BoM,\n // you don't specify versions in Firebase library dependencies\n implementation(\"com.google.firebase:firebase-ai\")\n }\n\nGenerate an image\n\nTo generate an image in your Android app, start by instantiating an\n`ImagenModel` with an optional configuration.\n\nYou can use the [`generationConfig`](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=android) parameter to define a negative prompt, the\nnumber of images, the output image aspect ratio, the image format and add a\nwatermark. You can use the [`safetySettings`](https://firebase.google.com/docs/vertex-ai/safety-settings?platform=android) parameter to configure the safety\nand person filters.\n**Note:** Refer to the Firebase documentation for up-to-date information about [available Imagen models](https://firebase.google.com/docs/vertex-ai/models). \n\nKotlin \n\n val config = ImagenGenerationConfig {\n numberOfImages = 2,\n aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9,\n imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100),\n addWatermark = false\n }\n\n // Initialize the Gemini Developer API backend service\n // For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI())\n val model = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel(\n modelName = \"imagen-3.0-generate-002\",\n generationConfig = config,\n safetySettings = ImagenSafetySettings(\n safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,\n personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL\n )\n )\n\nJava \n\n ImagenGenerationConfig config = new ImagenGenerationConfig.Builder()\n .setNumberOfImages(2)\n .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9)\n .setImageFormat(ImagenImageFormat.jpeg(100))\n .setAddWatermark(false)\n .build();\n\n // For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI())\n ImagenModelFutures model = ImagenModelFutures.from(\n FirebaseAI.ai(backend = GenerativeBackend.googleAI()).imagenModel(\n \"imagen-3.0-generate-002\",\n config,\n ImagenSafetySettings.builder()\n .setSafetyFilterLevel(ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE)\n .setPersonFilterLevel(ImagenPersonFilterLevel.BLOCK_ALL)\n .build())\n );\n\nOnce your `ImagenModel` is instantiated, you can generate images by calling\n`generateImages`: \n\nKotlin \n\n val imageResponse = model.generateImages(\n prompt = \"An astronaut riding a horse\",\n )\n val image = imageResponse.images.first\n val bitmapImage = image.asBitmap()\n\nJava \n\n CompletableFuture\u003cGenerateContentResponse\u003e futureResponse =\n model.generateContent(\n Content.newBuilder()\n .addParts(\n Part.newBuilder()\n .setText(\"An astronaut riding a horse\")\n .build())\n .build());\n\n try {\n GenerateContentResponse imageResponse = futureResponse.get();\n List\u003cGeneratedImage\u003e images =\n imageResponse\n .getCandidates(0)\n .getContent()\n .getParts(0)\n .getInlineData()\n .getImagesList();\n\n if (!images.isEmpty()) {\n GeneratedImage image = images.get(0);\n Bitmap bitmapImage = image.asBitmap();\n // Use bitmapImage\n }\n } catch (ExecutionException | InterruptedException e) {\n e.printStackTrace();\n }"]]