Skip to content
Open
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
4 changes: 4 additions & 0 deletions genai/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<artifactId>google-genai</artifactId>
<version>1.15.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
<dependency>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package genai.imagegeneration;

// [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]

import com.google.genai.Client;
import com.google.genai.types.ControlReferenceConfig;
import com.google.genai.types.ControlReferenceImage;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.GeneratedImage;
import com.google.genai.types.Image;
import java.util.List;
import java.util.Optional;

public class ImageGenCannyCtrlTypeWithTextAndImage {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String modelId = "imagen-3.0-capability-001";
String outputGcsUri = "gs://your-bucket/your-prefix";
cannyEdgeCustomization(modelId, outputGcsUri);
}

/**
* Generates an image with a Canny edge image and text prompt.
*
* <p>This function demonstrates controlled customization. It uses a "Canny edge" image, which is
* a black-and-white line drawing that outlines the shapes of objects. The model is instructed to
* follow this structural outline to generate an image based the text prompt.
*
* @param modelId The GenAI model to use for generating the image.
* @param outputGcsUri A GCS URI where the generated image will be saved. Example:
* "gs://your-bucket/your-prefix"
* @return An Optional containing the GCS URI of the generated image if successful.
*/
public static Optional<String> cannyEdgeCustomization(String modelId, String outputGcsUri) {
// Client Initialization. Once created, it can be reused for multiple requests.
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
// Create a reference image out of an existing canny edge image signal
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_canny.png
ControlReferenceImage controlReferenceImage =
ControlReferenceImage.builder()
.referenceId(1)
.referenceImage(
Image.builder()
.gcsUri("gs://cloud-samples-data/generative-ai/image/car_canny.png")
.build())
.config(ControlReferenceConfig.builder().controlType("CONTROL_TYPE_CANNY").build())
.build();

// The `[1]` in the prompt refers to the `referenceId` assigned to
// the control reference image.
EditImageResponse imageResponse =
client.models.editImage(
modelId,
"a watercolor painting of a red car[1] driving on a road",
List.of(controlReferenceImage),
EditImageConfig.builder()
.editMode("EDIT_MODE_CONTROLLED_EDITING")
.numberOfImages(1)
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
.personGeneration("ALLOW_ADULT")
.outputGcsUri(outputGcsUri)
.build());

Image generatedImage =
imageResponse
.generatedImages()
.flatMap(generatedImages -> generatedImages.stream().findFirst())
.flatMap(GeneratedImage::image)
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));

generatedImage.gcsUri().ifPresent(System.out::println);
// Example response:
// gs://your-bucket/your-prefix
return generatedImage.gcsUri();
}
}
}
// [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package genai.imagegeneration;

// [START googlegenaisdk_imggen_raw_reference_with_txt_img]

import com.google.genai.Client;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.GeneratedImage;
import com.google.genai.types.Image;
import com.google.genai.types.RawReferenceImage;
import java.util.List;
import java.util.Optional;

public class ImageGenRawReferenceWithTextAndImage {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String modelId = "imagen-3.0-capability-001";
String outputGcsUri = "gs://your-bucket/your-prefix";
styleTransferCustomization(modelId, outputGcsUri);
}

/**
* Generates an image in a new style based on a raw reference image and text prompt.
*
* <p>This function demonstrates style transfer customization. It uses a raw reference image of a
* "teacup". The model is instructed to recreate the reference image in a new style based on the
* text prompt.
*
* @param modelId The GenAI model to use for generating the image.
* @param outputGcsUri A GCS URI where the generated image will be saved. Example:
* "gs://your-bucket/your-prefix"
* @return An Optional containing the GCS URI of the generated image if successful.
*/
public static Optional<String> styleTransferCustomization(String modelId, String outputGcsUri) {
// Client Initialization. Once created, it can be reused for multiple requests.
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
// Create a raw reference image of teacup stored in Google Cloud Storage
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/teacup-1.png
RawReferenceImage rawReferenceImage =
RawReferenceImage.builder()
.referenceId(1)
.referenceImage(
Image.builder()
.gcsUri("gs://cloud-samples-data/generative-ai/image/teacup-1.png")
.build())
.build();

// The `[1]` in the prompt refers to the `referenceId` assigned to the raw reference image.
EditImageResponse imageResponse =
client.models.editImage(
modelId,
"transform the subject in the image so that "
+ "the teacup[1] is made entirely out of chocolate",
List.of(rawReferenceImage),
EditImageConfig.builder()
.editMode("EDIT_MODE_DEFAULT")
.numberOfImages(1)
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
.personGeneration("ALLOW_ADULT")
.outputGcsUri(outputGcsUri)
.build());

Image generatedImage =
imageResponse
.generatedImages()
.flatMap(generatedImages -> generatedImages.stream().findFirst())
.flatMap(GeneratedImage::image)
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));

generatedImage.gcsUri().ifPresent(System.out::println);
// Example response:
// gs://your-bucket/your-prefix
return generatedImage.gcsUri();
}
}
}
// [END googlegenaisdk_imggen_raw_reference_with_txt_img]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package genai.imagegeneration;

// [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]

import com.google.genai.Client;
import com.google.genai.types.ControlReferenceConfig;
import com.google.genai.types.ControlReferenceImage;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.GeneratedImage;
import com.google.genai.types.Image;
import java.util.List;
import java.util.Optional;

public class ImageGenScribbleCtrlTypeWithTextAndImage {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String modelId = "imagen-3.0-capability-001";
String outputGcsUri = "gs://your-bucket/your-prefix";
scribbleCustomization(modelId, outputGcsUri);
}

/**
* Generates an image with a Scribble image and text prompt.
*
* <p>This function demonstrates controlled customization. It uses a "Scribble" image, which is a
* rough drawing with a black background and white lines. The model is instructed to follow this
* structural outline to generate an image based the text prompt.
*
* @param modelId The GenAI model to use for generating the image.
* @param outputGcsUri A GCS URI where the generated image will be saved. Example:
* "gs://your-bucket/your-prefix"
* @return An Optional containing the GCS URI of the generated image if successful.
*/
public static Optional<String> scribbleCustomization(String modelId, String outputGcsUri) {
// Client Initialization. Once created, it can be reused for multiple requests.
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
// Create a reference image out of an existing scribble image signal
// using
// https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_scribble.png
ControlReferenceImage controlReferenceImage =
ControlReferenceImage.builder()
.referenceId(1)
.referenceImage(
Image.builder()
.gcsUri("gs://cloud-samples-data/generative-ai/image/car_scribble.png")
.build())
.config(ControlReferenceConfig.builder().controlType("CONTROL_TYPE_SCRIBBLE").build())
.build();

// The `[1]` in the prompt refers to the `referenceId` assigned to the
// control reference image.
EditImageResponse imageResponse =
client.models.editImage(
modelId,
"an oil painting showing the side of a red car[1]",
List.of(controlReferenceImage),
EditImageConfig.builder()
.editMode("EDIT_MODE_CONTROLLED_EDITING")
.numberOfImages(1)
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
.personGeneration("ALLOW_ADULT")
.outputGcsUri(outputGcsUri)
.build());

Image generatedImage =
imageResponse
.generatedImages()
.flatMap(generatedImages -> generatedImages.stream().findFirst())
.flatMap(GeneratedImage::image)
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));

generatedImage.gcsUri().ifPresent(System.out::println);
// Example response:
// gs://your-bucket/your-prefix

return generatedImage.gcsUri();
}
}
}
// [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package genai.imagegeneration;

// [START googlegenaisdk_imggen_style_reference_with_txt_img]

import com.google.genai.Client;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.GeneratedImage;
import com.google.genai.types.Image;
import com.google.genai.types.StyleReferenceConfig;
import com.google.genai.types.StyleReferenceImage;
import java.util.List;
import java.util.Optional;

public class ImageGenStyleReferenceWithTextAndImage {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String modelId = "imagen-3.0-capability-001";
String outputGcsUri = "gs://your-bucket/your-prefix";
styleCustomization(modelId, outputGcsUri);
}

/**
* Generates an image with a style reference image and text prompt
*
* <p>This function demonstrates style customization. It uses a style reference image of a "neon
* sign". The model is instructed to transfer the style of the reference image to a new image
* based on the text prompt.
*
* @param modelId The GenAI model to use for generating the image.
* @param outputGcsUri A GCS URI where the generated image will be saved. Example:
* "gs://your-bucket/your-prefix"
* @return An Optional containing the GCS URI of the generated image if successful.
*/
public static Optional<String> styleCustomization(String modelId, String outputGcsUri) {
// Client Initialization. Once created, it can be reused for multiple requests.
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
// Create a style reference image of a neon sign stored in Google Cloud Storage
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png
StyleReferenceImage styleReferenceImage =
StyleReferenceImage.builder()
.referenceId(1)
.referenceImage(
Image.builder()
.gcsUri("gs://cloud-samples-data/generative-ai/image/neon.png")
.build())
.config(StyleReferenceConfig.builder().styleDescription("neon sign").build())
.build();

// The `[1]` in the prompt refers to the `referenceId` assigned to the style reference image.
EditImageResponse imageResponse =
client.models.editImage(
modelId,
"generate an image of a neon sign [1] with the words: have a great day",
List.of(styleReferenceImage),
EditImageConfig.builder()
.editMode("EDIT_MODE_DEFAULT")
.numberOfImages(1)
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
.personGeneration("ALLOW_ADULT")
.outputGcsUri(outputGcsUri)
.build());

Image generatedImage =
imageResponse
.generatedImages()
.flatMap(generatedImages -> generatedImages.stream().findFirst())
.flatMap(GeneratedImage::image)
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));

generatedImage.gcsUri().ifPresent(System.out::println);
// Example response:
// gs://your-bucket/your-prefix

return generatedImage.gcsUri();
}
}
}
// [END googlegenaisdk_imggen_style_reference_with_txt_img]
Loading