Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
// [START googlegenaisdk_imggen_with_txt]

import com.google.genai.Client;
import com.google.genai.types.GenerateImagesConfig;
import com.google.genai.types.GenerateImagesResponse;
import com.google.genai.types.GeneratedImage;
import com.google.genai.types.Blob;
import com.google.genai.types.Candidate;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Image;
import com.google.genai.types.Part;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -33,7 +36,7 @@ public class ImageGenWithText {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String modelId = "imagen-4.0-generate-001";
String modelId = "gemini-2.5-flash-image";
String outputFile = "resources/output/dog_newspaper.png";
generateImage(modelId, outputFile);
}
Expand All @@ -43,32 +46,32 @@ public static Image generateImage(String modelId, String outputFile) throws IOEx
// Client Initialization. Once created, it can be reused for multiple requests.
try (Client client = Client.builder().location("global").vertexAI(true).build()) {

GenerateImagesResponse response =
client.models.generateImages(
modelId,
"A dog reading a newspaper",
GenerateImagesConfig.builder().imageSize("2K").outputMimeType("image/png").build());
GenerateContentConfig config =
GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build();

Image generatedImage =
GenerateContentResponse response =
client.models.generateContent(modelId, "A dog reading a newspaper", config);

byte[] imageBytes =
response
.generatedImages()
.flatMap(generatedImages -> generatedImages.stream().findFirst())
.flatMap(GeneratedImage::image)
.candidates()
.flatMap(candidates -> candidates.stream().findFirst())
.flatMap(Candidate::content)
.flatMap(Content::parts)
.flatMap(
parts ->
parts.stream()
.filter(part -> part.inlineData().flatMap(Blob::data).isPresent())
.findFirst())
.flatMap(part -> part.inlineData().flatMap(Blob::data))
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));

// Read image data and write it to the output file
if (generatedImage.imageBytes().isPresent()) {
BufferedImage image =
ImageIO.read(new ByteArrayInputStream(generatedImage.imageBytes().get()));
ImageIO.write(image, "png", new File(outputFile));
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
ImageIO.write(image, "png", new File(outputFile));

System.out.printf(
"Created output image using %s bytes\n", generatedImage.imageBytes().get().length);
}
System.out.printf("Created output image using %d bytes\n", imageBytes.length);

// Example response:
// Created output image using 1633112 bytes
return generatedImage;
return Image.builder().imageBytes(imageBytes).build();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@
@RunWith(JUnit4.class)
public class ImageGenerationIT {

private static final String IMAGEN_3_MODEL = "imagen-3.0-capability-001";
private static final String BUCKET_NAME =
System.getenv().getOrDefault("BUCKET_NAME", "java-docs-samples-testing")
.replaceFirst("^gs://", "");
private static final String PREFIX = "genai-img-generation-" + UUID.randomUUID();
private static final String OUTPUT_GCS_URI = String.format("gs://%s/%s", BUCKET_NAME, PREFIX);
private static final String IMAGEN_4_MODEL = "imagen-4.0-generate-001";
private static final String IMAGEN_MODEL = "gemini-2.5-flash-image";
private static final String VIRTUAL_TRY_ON_MODEL = "virtual-try-on-001";

private ByteArrayOutputStream bout;
Expand Down Expand Up @@ -96,7 +95,7 @@ public void tearDown() {
public void testImageGenCannyCtrlTypeWithTextAndImage() {
Optional<String> response =
ImageGenCannyCtrlTypeWithTextAndImage.cannyEdgeCustomization(
IMAGEN_3_MODEL, OUTPUT_GCS_URI);
IMAGEN_MODEL, OUTPUT_GCS_URI);
assertThat(response).isPresent();
assertThat(response.get()).isNotEmpty();
}
Expand All @@ -106,7 +105,7 @@ public void testImageGenCannyCtrlTypeWithTextAndImage() {
public void testImageGenRawReferenceWithTextAndImage() {
Optional<String> response =
ImageGenRawReferenceWithTextAndImage.styleTransferCustomization(
IMAGEN_3_MODEL, OUTPUT_GCS_URI);
IMAGEN_MODEL, OUTPUT_GCS_URI);
assertThat(response).isPresent();
assertThat(response.get()).isNotEmpty();
}
Expand All @@ -116,7 +115,7 @@ public void testImageGenRawReferenceWithTextAndImage() {
public void testImageGenScribbleCtrlTypeWithTextAndImage() {
Optional<String> response =
ImageGenScribbleCtrlTypeWithTextAndImage.scribbleCustomization(
IMAGEN_3_MODEL, OUTPUT_GCS_URI);
IMAGEN_MODEL, OUTPUT_GCS_URI);
assertThat(response).isPresent();
assertThat(response.get()).isNotEmpty();
}
Expand All @@ -126,7 +125,7 @@ public void testImageGenScribbleCtrlTypeWithTextAndImage() {
public void testImageGenStyleReferenceWithTextAndImage() {
Optional<String> response =
ImageGenStyleReferenceWithTextAndImage.styleCustomization(
IMAGEN_3_MODEL, OUTPUT_GCS_URI);
IMAGEN_MODEL, OUTPUT_GCS_URI);
assertThat(response).isPresent();
assertThat(response.get()).isNotEmpty();
}
Expand All @@ -136,7 +135,7 @@ public void testImageGenStyleReferenceWithTextAndImage() {
public void testImageGenSubjectReferenceWithTextAndImage() {
Optional<String> response =
ImageGenSubjectReferenceWithTextAndImage.subjectCustomization(
IMAGEN_3_MODEL, OUTPUT_GCS_URI);
IMAGEN_MODEL, OUTPUT_GCS_URI);
assertThat(response).isPresent();
assertThat(response.get()).isNotEmpty();
}
Expand All @@ -156,7 +155,7 @@ public void testImageGenVirtualTryOnWithTextAndImage() throws IOException {
@Ignore("Imagen models are deprecated and unavailable; migrated to Gemini in follow-up PR")
public void testImageGenWithText() throws IOException {
Image image =
ImageGenWithText.generateImage(IMAGEN_4_MODEL, "resources/output/dog_newspaper.png");
ImageGenWithText.generateImage(IMAGEN_MODEL, "resources/output/dog_newspaper.png");

assertThat(image).isNotNull();
assertThat(image.imageBytes()).isPresent();
Expand Down