在應用程式中新增自訂圖片可以顯著改善和個人化使用者體驗,並提高使用者參與度。本文探討了 Firebase AI Logic 的兩項新的圖像生成功能:目前處於預覽階段的專用 Imagen 編輯功能,以及專為上下文或對話式圖像生成而設計的 Gemini 2.5 Flash Image(又名“Nano Banana”)的正式發布版本。
利用 Firebase AI 邏輯產生的圖像提升用戶參與度
圖像生成模型可用於建立自訂使用者個人資料頭像,或將個人化視覺資源直接整合到關鍵螢幕流程中。
例如,Imagen 提供了新的編輯功能(開發者預覽版)。現在您可以繪製一個遮罩,並利用影像修復技術在遮色片區域內產生像素。此外,還可以使用外繪來產生遮罩外部的像素。
Imagen 支援影像修復,允許僅生成影像的一部分。
或者,Gemini 2.5 Flash Image(又稱 Nano Banana)可以利用擴展的世界知識和 Gemini 模型的推理能力來產生與上下文相關的圖像,這非常適合創建與用戶當前應用程式內體驗一致的動態插圖。
使用 Gemini 2.5 Flash Image 建立與您的應用程式相關的動態插圖。
最後,透過對話式和迭代式圖像編輯功能,使用者可以用自然語言編輯照片。
使用 Gemini 2.5 Flash Image,透過自然語言編輯圖片。
在開始將 AI 整合到您的應用程式中時,瞭解 AI 安全性 非常重要。評估應用程式的安全風險、考慮調整以降低安全風險、執行適合您用例的安全測試、徵求用戶回饋和監控內容,這些都至關重要。
Imagen 或 Gemini:由您選擇
Gemini 2.5 Flash Image ("Nano Banana") 和 Imagen 的區別在於它們的主要關注點和高級功能。Gemini 2.5 Flash Image 作為 Gemini 系列中的圖像模型,在對話式圖像編輯方面表現出色,能夠在多次迭代中保持上下文和主題的一致性,並利用「世界知識和推理」來創建與上下文相關的視覺效果,或在長文本序列中嵌入準確的視覺效果。
Imagen 是 Google 專門用於圖像生成的圖像生成模型,旨在提供更大的創作控制,尤其擅長生成高度逼真的圖像、展現藝術細節、呈現特定風格,並提供明確的控制選項來指定生成圖像的寬高比或格式。
| Gemini 2.5 閃光燈影像 (納米香蕉🍌) | Imagen |
🌎 世界知識與推理,帶來更具上下文相關性的圖像 💬 以對話的方式編輯圖片,同時保持上下文 📖 在長文本序列中嵌入精確的視覺效果 | 📐 指定生成影像的寬高比或格式
🖌️支援基於蒙版的編輯,用於影像的內嵌和外嵌。
🎚️ 更好地控制生成影像的細節(品質、藝術細節和特定風格) |
讓我們來看看如何在你的應用程式中使用它們。
使用 Imagen 進行影像修復
幾個月前,我們發布了 Imagen 的新編輯功能。雖然 Imagen 現在已經可以用於影像生成,但編輯功能仍處於 開發者預覽版。
影像編輯功能包括影像修復和影像修復,以及基於遮罩的影像編輯功能。這項新功能允許使用者修改影像的特定區域,而無需重新產生整個影像。這意味著您可以保留圖像中最精彩的部分,只修改您想要更改的部分。
使用影像編輯功能對影像進行精確的局部修改,同時確保影像其餘部分的完整性
這些變更是在保持原始影像的核心元素和整體完整性的前提下進行的,並且只修改蒙版中的區域。
要使用 Imagen 實現影像修復,首先需要初始化 imagen-3.0-capability-001 一個支援編輯功能的特定 Imagen 模型:
// Copyright 2025 Google LLC.
// SPDX-License-Identifier: Apache-2.0
val editingModel =
Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel(
"imagen-3.0-capability-001",
generationConfig = ImagenGenerationConfig(
numberOfImages = 1,
aspectRatio = ImagenAspectRatio.SQUARE_1x1,
imageFormat = ImagenImageFormat.jpeg(compressionQuality = 75),
),
)在此基礎上,定義影像修復函數:
// Copyright 2025 Google LLC.
// SPDX-License-Identifier: Apache-2.0
val prompt = "remove the pancakes and make it an omelet instead"
suspend fun inpaintImageWithMask(sourceImage: Bitmap, maskImage: Bitmap, prompt: String, editSteps: Int = 50): Bitmap {
val imageResponse = editingModel.editImage(
referenceImages = listOf(
ImagenRawImage(sourceImage.toImagenInlineImage()),
ImagenRawMask(maskImage.toImagenInlineImage()),
),
prompt = prompt,
config = ImagenEditingConfig(
editMode = ImagenEditMode.INPAINT_INSERTION,
editSteps = editSteps,
),
)
return imageResponse.images.first().asBitmap()
}您需要提供來源影像、遮罩影像以及編輯提示和要執行的編輯步驟數。
您可以在 Android AI 範例目錄中的 Imagen Editing Sample 中看到它的實際應用!
Imagen 也支援 outpainting,使模型能夠產生遮罩外部的像素。您也可以使用 Imagen 的圖像自訂功能來變更圖片的樣式或更新圖片中的主題。請參閱 Android 開發者文件,以瞭解更多資訊。
使用 Gemini 2.5 Flash Image 產生對話式影像
使用 Gemini 2.5 Flash Image 編輯影像的一種方法是使用該模型的多回合聊天功能。
首先,初始化模型:
// Copyright 2025 Google LLC.
// SPDX-License-Identifier: Apache-2.0
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "gemini-2.5-flash-image",
// Configure the model to respond with text and images (required)
generationConfig = generationConfig {
responseModalities = listOf(ResponseModality.TEXT,
ResponseModality.IMAGE)
}
)
為了實現與上面描述的基於掩模的 Imagen 方法類似的結果,我們可以使用 chat API 與 Gemini 2.5 Flash Image 發起對話。
// Copyright 2025 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Initialize the chat
val chat = model.startChat()
// Load a bitmap
val source = ImageDecoder.createSource(context.contentResolver, uri)
val bitmap = ImageDecoder.decodeBitmap(source)
// Create the initial prompt instructing the model to edit the image
val prompt = content {
image(bitmap)
text("remove the pancakes and add an omelet")
}
// To generate an initial response, send a user message with the image and text prompt
var response = chat.sendMessage(prompt)
// Inspect the returned image
var generatedImageAsBitmap = response
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
// Follow up requests do not need to specify the image again
response = chat.sendMessage("Now, center the omelet in the pan")
generatedImageAsBitmap = response
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
您可以在 Android AI 範例目錄中的 Gemini Image Chat 範例 中看到它的實際應用,並在 Android 文件 中閱讀更多相關資訊。
結語
Imagen 和 Gemini 2.5 Flash Image 都提供了強大的功能,您可以根據具體使用情況選擇理想的圖像生成模型,以個性化您的應用程式並提高用戶參與度。
-
產品最新消息如果您是 Android 開發人員,想在應用程式中導入創新的 AI 功能,我們最近推出了強大的新版更新。
Thomas Ezan • 2 分鐘小故事 -
產品最新消息今天,我們將推出 Gemini 3 Flash,擴展 Gemini 3 模型系列。這款模型以低成本提供最先進的智慧功能,而且速度飛快。
Thomas Ezan • 2 分鐘小故事 -
產品最新消息這是 Android Studio Quail 的最終穩定版。Android Studio 的新功能可協助您有效率地建構優質 AI 應用程式。
Amman Asfaw • 5 分鐘小故事
每週透過電子郵件接收最新的 Android 開發洞察資訊。