Imagen เป็นโมเดลการสร้างรูปภาพ โดยสามารถใช้เพื่อสร้าง อวตารที่กำหนดเองสำหรับโปรไฟล์ผู้ใช้ หรือเพื่อผสานรวมชิ้นงานภาพที่ปรับเปลี่ยนในแบบของคุณเข้ากับ โฟลว์หน้าจอที่มีอยู่เพื่อเพิ่มการมีส่วนร่วมของผู้ใช้
คุณเข้าถึงโมเดล Imagen จากแอป Android ได้โดยใช้ Firebase AI Logic SDK โมเดล Imagen พร้อมใช้งานโดยใช้ทั้ง ผู้ให้บริการ API ของ Firebase AI Logic: Gemini Developer API (แนะนำสำหรับนักพัฒนาซอฟต์แวร์ส่วนใหญ่ ) และ Vertex AI
ทดลองใช้พรอมต์
การสร้างพรอมต์ที่เหมาะสมมักต้องลองหลายครั้ง คุณสามารถทดลองใช้พรอมต์รูปภาพใน Vertex AI Studio ซึ่งเป็น IDE สำหรับการออกแบบและการสร้างต้นแบบพรอมต์ ดูเคล็ดลับเกี่ยวกับวิธีปรับปรุงพรอมต์ได้ที่คู่มือพรอมต์และแอตทริบิวต์รูปภาพ

ตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอป
ทําตามขั้นตอนในเอกสารประกอบของ Firebase เพื่อเพิ่ม Firebase ลงในโปรเจ็กต์ Android
เพิ่มการขึ้นต่อกันของ Gradle
เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในไฟล์ build.gradle
dependencies {
// Import the BoM for the Firebase platform
implementation(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 dependencies
implementation("com.google.firebase:firebase-ai")
}
สร้างรูปภาพ
หากต้องการสร้างรูปภาพในแอป Android ให้เริ่มต้นด้วยการสร้างอินสแตนซ์ของ
ImagenModel
พร้อมการกำหนดค่าที่ไม่บังคับ
คุณใช้พารามิเตอร์ generationConfig
เพื่อกำหนดพรอมต์เชิงลบ จำนวนรูปภาพ สัดส่วนภาพของรูปภาพเอาต์พุต รูปแบบรูปภาพ และเพิ่มลายน้ำได้ คุณใช้พารามิเตอร์ safetySettings
เพื่อกำหนดค่าตัวกรองความปลอดภัย
และตัวกรองบุคคลได้
Kotlin
val config = 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())
val model = 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
ImagenGenerationConfig config = new ImagenGenerationConfig.Builder()
.setNumberOfImages(2)
.setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9)
.setImageFormat(ImagenImageFormat.jpeg(100))
.setAddWatermark(false)
.build();
// For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI())
ImagenModelFutures model = 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())
);
เมื่อสร้างอินสแตนซ์ของ ImagenModel
แล้ว คุณจะสร้างรูปภาพได้โดยเรียกใช้
generateImages
ดังนี้
Kotlin
val imageResponse = model.generateImages(
prompt = "An astronaut riding a horse",
)
val image = imageResponse.images.first
val bitmapImage = image.asBitmap()
Java
CompletableFuture<GenerateContentResponse> futureResponse =
model.generateContent(
Content.newBuilder()
.addParts(
Part.newBuilder()
.setText("An astronaut riding a horse")
.build())
.build());
try {
GenerateContentResponse imageResponse = futureResponse.get();
List<GeneratedImage> images =
imageResponse
.getCandidates(0)
.getContent()
.getParts(0)
.getInlineData()
.getImagesList();
if (!images.isEmpty()) {
GeneratedImage image = images.get(0);
Bitmap bitmapImage = image.asBitmap();
// Use bitmapImage
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}