Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Imagen adalah model pembuatan gambar. Fitur ini dapat digunakan untuk membuat avatar kustom untuk profil pengguna atau mengintegrasikan aset visual yang dipersonalisasi ke dalam alur layar yang ada untuk meningkatkan interaksi pengguna.
Anda dapat mengakses model Imagen dari aplikasi Android menggunakan Firebase AI Logic SDK. Model Imagen tersedia menggunakan
penyedia API Logika AI Firebase: Gemini Developer API (direkomendasikan untuk sebagian besar
developer) dan Vertex AI.
Gambar 1.
Mengakses model Imagen menggunakan Firebase AI Logic.
Bereksperimen dengan perintah
Membuat perintah yang ideal sering kali memerlukan beberapa upaya. Anda dapat bereksperimen dengan perintah gambar di Vertex AI Studio, IDE untuk desain dan pembuatan prototipe perintah. Untuk tips tentang cara meningkatkan perintah, tinjau
panduan atribut gambar dan perintah.
Gambar 2.
Vertex AI Studio dapat membantu Anda meningkatkan kualitas perintah
pembuatan gambar.
Menyiapkan project Firebase dan menghubungkan aplikasi
Tambahkan dependensi berikut ke file build.gradle Anda:
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")}
Membuat gambar
Untuk membuat gambar di aplikasi Android, mulai dengan membuat instance
ImagenModel dengan konfigurasi opsional.
Anda dapat menggunakan parameter generationConfig untuk menentukan perintah negatif,
jumlah gambar, rasio aspek gambar output, format gambar, dan menambahkan
watermark. Anda dapat menggunakan parameter safetySettings untuk mengonfigurasi filter keamanan
dan orang.
Kotlin
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()));
Setelah ImagenModel dibuat instance-nya, Anda dapat membuat gambar dengan memanggil
generateImages:
Kotlin
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();}
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-30 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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 }"]]