تولید تصاویر با Imagen، تولید تصاویر با Imagen، تولید تصاویر با Imagen
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
Imagen یک مدل تولید تصویر است. میتوان از آن برای تولید آواتارهای سفارشی برای نمایههای کاربر یا ادغام داراییهای بصری شخصیشده در جریانهای صفحه موجود برای افزایش تعامل کاربر استفاده کرد.
میتوانید با استفاده از Firebase AI Logic SDKبه مدلهای Imagen از برنامه Android خود دسترسی پیدا کنید. مدلهای Imagen با استفاده از هر دو ارائهدهنده API AI Logic Firebase در دسترس هستند: Gemini Developer API (توصیهشده برای اکثر توسعهدهندگان) و Vertex AI.
شکل 1. با استفاده از Firebase AI Logic به مدل های Imagen دسترسی پیدا کنید.
با دستورات آزمایش کنید
ایجاد درخواست های ایده آل اغلب به تلاش های متعدد نیاز دارد. میتوانید با درخواستهای تصویر در Vertex AI Studio ، یک IDE برای طراحی سریع و نمونهسازی آزمایش کنید. برای راهنمایی در مورد چگونگی بهبود درخواستهای خود، راهنمای ویژگی فرمان و تصویر را مرور کنید.
شکل 2. Vertex AI Studio می تواند به شما کمک کند تا درخواست های تولید تصویر خود را اصلاح کنید.
یک پروژه Firebase راه اندازی کنید و برنامه خود را متصل کنید
وابستگی های زیر را به فایل build.gradle خود اضافه کنید:
dependencies{// Import the BoM for the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.1.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")}
یک تصویر تولید کنید
برای ایجاد یک تصویر در برنامه Android خود، با نمونه سازی یک ImagenModel با یک پیکربندی اختیاری شروع کنید.
شما می توانید از پارامتر generationConfig برای تعریف یک اعلان منفی، تعداد تصاویر، نسبت تصویر خروجی، فرمت تصویر و اضافه کردن یک واترمارک استفاده کنید. می توانید از پارامتر safetySettings برای پیکربندی فیلترهای ایمنی و شخص استفاده کنید.
کاتلین
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))
جاوا
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()));
هنگامی که ImagenModel شما نمونه سازی شد، می توانید با فراخوانی generateImages تصاویر ایجاد کنید:
کاتلین
valimageResponse=model.generateImages(prompt="An astronaut riding a horse",)valimage=imageResponse.images.firstvalbitmapImage=image.asBitmap()
جاوا
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();}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-14 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-14 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Generate images with Imagen\n\nImagen 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-----------------------\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----------------------------------------------\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-------------------------\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.1.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-----------------\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\n### Kotlin\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\n### Java\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\n### Kotlin\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\n### Java\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 }"]]