免费POC, 零成本试错
AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


我要投稿

一文详解 | 从了解到集成,轻松为您的应用选择理想的图像生成模型

发布日期:2025-11-26 20:27:18 浏览次数: 1518
作者:谷歌开发者

微信搜一搜,关注“谷歌开发者”

推荐语

Firebase AI Logic新推出的Imagen和Gemini 2.5 Flash两大图像生成模型,为应用开发者提供了强大的视觉内容创作工具。

核心内容:
1. Imagen的独特编辑功能:图像内绘制与扩展能力
2. Gemini 2.5 Flash的上下文相关图像生成优势
3. AI集成应用时的安全考量与最佳实践

杨芳贤
53AI创始人/腾讯云(TVP)最具价值专家

作者 / 高级开发者关系工程师 Thomas Ezan 和开发者关系工程师 Mozart Louis


为您的应用添加自定义图像,能够显著改善和个性化用户体验,有效提高用户参与度。本文将探讨使用 Firebase AI Logic 生成图像的两种新功能: 其一是 Imagen 专属编辑功能预览版;其二是 Gemini 2.5 Flash Image (又名 Nano Banana) 正式版,后者的专长就是在情境或对话中生成图像。



借助 Firebase AI Logic 生成图像以提高用户参与度


图像生成模型可用于创建自定义用户个人资料头像,或将个性化视觉素材直接集成到应用关键流程中。


例如,Imagen 提供了尚处于开发者预览版的全新编辑功能。现在,您可以绘制遮罩并利用图像内绘制 (inpainting) 在遮罩区域内生成像素。此外,还提供了图像扩展 (outpainting) 功能,可用于生成遮罩区域外的像素。

△ Imagen 支持图像内绘制功能,可以仅生成图像的一部分

另一方面,Gemini 2.5 Flash Image (又名 Nano Banana) 可以使用 Gemini 模型渊博的世界知识和推理能力来生成与上下文相关的图像,这非常适合用于创建与用户当前应用内体验契合的动态插图。

△ 使用 Gemini 2.5 Flash Image 创建与您的应用上下文相关的动态插图 

最后,借助对话和迭代编辑图像的功能,用户可以使用自然语言编辑图像。

△ 通过 Gemini 2.5 Flash Image,使用自然语言编辑图像

在着手将 AI 集成到您的应用中时,了解 AI 安全性非常重要。尤其关键的是,您需要评估应用的安全风险、斟酌调整以降低安全风险、执行适合您用例的安全测试,以及征求用户反馈并监控内容。


  • AI 安全性

    https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen



Imagen 或 Gemini 任您选择


Gemini 2.5 Flash Image (Nano Banana) 和 Imagen 之间的区别在于各自独特的侧重点和高级功能。Gemini 2.5 Flash Image 作为广泛的 Gemini 系列中的图像模型,擅长对话式图像编辑,能在多次迭代中保持上下文和主体一致性,并利用 "世界知识和推理" 来创建与上下文相关的视觉内容,或在长文本序列中嵌入准确的视觉内容。


  • Gemini 2.5 Flash Image

    https://ai.google.dev/gemini-api/docs/image-generation

  • Imagen

    https://ai.google.dev/gemini-api/docs/imagen


Imagen 是 Google 的专业图像生成模型,专为更好地发挥创意和掌控作品而设计,擅长于高度真实的输出、艺术细节、特定风格,并提供明确的控制选项,来指定生成图像的宽高比或格式。

Gemini 2.5 Flash Image 

(Nano Banana 🍌)

Imagen

🌎 具备世界知识和推理能力,生成与上下文更相关的图像


💬 在保持与上下文关联性的同时,通过对话编辑图像


📖 在长文本序列中嵌入准确的视觉内容

📐 指定生成图像的宽高比或格式


🖌️ 支持基于遮罩的编辑,实现图像内绘制和图像扩展


🎨 更好地控制生成图像的细节 (质量、艺术细节和特定风格)

一起来了解如何在您的应用中使用上述功能。



使用 Imagen 进行图像内绘制 


几个月前,我们发布了 Imagen 新的编辑功能。虽然 Imagen 的图像生成功能已可正式用于生产环境,但编辑功能仍处于开发者预览版阶段。


Imagen 编辑功能包括图像内绘制图像扩展,二者均为基于遮罩的图像编辑功能。这项新功能允许用户修改图像的特定区域,而无需重新生成整个图像。这意味着您可以保留图像中您最满意的部分,只更改您想要调整的内容。

△ 使用 Imagen 编辑功能对图像进行精确的针对性更改,并保证图像其余部分的完整性

做出这些更改时,原始图像的核心元素和图像整体完整性不受影响,您可以仅调整遮罩区域。


要使用 Imagen 的图像内绘制功能,请先初始化 imagen-3.0-capability-001,这是支持编辑功能的特定 Imagen 模型:

// Copyright 2025 Google LLC.// SPDX-License-Identifier: Apache-2.0val 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.0val 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 Editing Sample

    https://github.com/android/ai-samples/tree/main/ai-catalog/samples/imagen-editing


Imagen 还支持图像扩展,该功能使您能让模型在遮罩外的区域生成像素。您还可以使用 Imagen 的图像自定义功能来更改图像风格或更新图像中的一个主体。有关详细信息,请参阅 Android 开发者文档


  • Android 开发者文档

    https://developer.android.com/ai/imagen#editing



使用 Gemini 2.5 Flash Image 通过对话生成图像


要使用 Gemini 2.5 Flash Image 编辑图像,一种方法是通过该模型的多轮聊天功能进行编辑。


首先,初始化模型:

// Copyright 2025 Google LLC.// SPDX-License-Identifier: Apache-2.0val 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 chatval chat = model.startChat()// Load a bitmapval source = ImageDecoder.createSource(context.contentResolver, uri)val bitmap = ImageDecoder.decodeBitmap(source)// Create the initial prompt instructing the model to edit the imageval 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 promptvar response = chat.sendMessage(prompt)// Inspect the returned imagevar generatedImageAsBitmap = response    .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image// Follow up requests do not need to specify the image againresponse = chat.sendMessage("Now, center the omelet in the pan")generatedImageAsBitmap = response    .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image

您可以在 Android AI 示例库的 Gemini Image Chat Sample 查看该功能的实际运作,也可参阅 Android 技术文档以了解更多相关信息。


  • Gemini Image Chat Sample

    https://github.com/android/ai-samples/tree/main/ai-catalog/samples/gemini-image-chat

  • Android 技术文档

    https://developer.android.com/ai/gemini/developer-api#generate-images



结论


Imagen 和 Gemini 2.5 Flash Image 都提供了强大的功能,允许您根据具体用例选择理想的图像生成模型,从而让您的应用更加个性化,并提高用户参与度。


欢迎您持续关注 "谷歌开发者" 微信公众号,及时了解更多开发技术和产品更新等资讯动态。






53AI,企业落地大模型首选服务商

产品:场景落地咨询+大模型应用平台+行业解决方案

承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

添加专属顾问

回到顶部

加载中...

扫码咨询