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
123 changes: 123 additions & 0 deletions examples/src/main/java/com/google/genai/examples/EditImageAsync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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
*
* https://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.
*/

/**
* Usage:
*
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
*
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
*
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
*
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
*
* <p>1b. If you are using Gemini Developer AI, set an API key environment variable. You can find a
* list of available API keys here: https://aistudio.google.com/app/apikey
*
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
*
* <p>2. Compile the java package and run the sample code.
*
* <p>mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.EditImageAsync"
*/
package com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.GenerateImagesConfig;
import com.google.genai.types.GenerateImagesResponse;
import com.google.genai.types.Image;
import com.google.genai.types.MaskReferenceConfig;
import com.google.genai.types.MaskReferenceImage;
import com.google.genai.types.RawReferenceImage;
import com.google.genai.types.ReferenceImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import org.apache.http.HttpException;

/** An example of using the Unified Gen AI Java SDK to edit an image asynchronously. */
public class EditImageAsync {
public static void main(String[] args) throws IOException, HttpException {
// Instantiates the client using Vertex AI, and sets the project and location in the builder.
Client client =
Client.builder()
.vertexAI(true)
.project(System.getenv("GOOGLE_CLOUD_PROJECT"))
.location(System.getenv("GOOGLE_CLOUD_LOCATION"))
.build();

GenerateImagesConfig generateImagesConfig =
GenerateImagesConfig.builder().numberOfImages(1).outputMimeType("image/jpeg").build();

CompletableFuture<GenerateImagesResponse> generateImagesResponseFuture =
client.async.models.generateImages(
"imagen-3.0-generate-001",
"An umbrella in the foreground, and a rainy night sky in the background",
generateImagesConfig);

generateImagesResponseFuture
.thenAccept(
generatedImagesResponse -> {
Image generatedImage =
generatedImagesResponse.generatedImages().get().get(0).image().get();

// Edit image with a mask.
EditImageConfig editImageConfig =
EditImageConfig.builder()
.editMode("EDIT_MODE_INPAINT_INSERTION")
.numberOfImages(1)
.outputMimeType("image/jpeg")
.build();

ArrayList<ReferenceImage> referenceImages = new ArrayList<>();
RawReferenceImage rawReferenceImage =
RawReferenceImage.builder().referenceImage(generatedImage).referenceId(1).build();
referenceImages.add(rawReferenceImage);

MaskReferenceImage maskReferenceImage =
MaskReferenceImage.builder()
.referenceId(2)
.config(
MaskReferenceConfig.builder()
.maskMode("MASK_MODE_BACKGROUND")
.maskDilation(0.0f)
.build())
.build();
referenceImages.add(maskReferenceImage);

CompletableFuture<EditImageResponse> editImageResponseFuture =
client.async.models.editImage(
"imagen-3.0-capability-001",
"Sunlight and clear sky",
referenceImages,
editImageConfig);

editImageResponseFuture
.thenAccept(
editImageResponse -> {
Image editedImage =
editImageResponse.generatedImages().get().get(0).image().get();
// Do something with editedImage.
})
.join();
})
.join();
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/google/genai/AsyncModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.google.genai;

import com.google.genai.types.Content;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.EmbedContentConfig;
import com.google.genai.types.EmbedContentResponse;
import com.google.genai.types.GenerateContentConfig;
Expand All @@ -28,6 +30,7 @@
import com.google.genai.types.GenerateVideosConfig;
import com.google.genai.types.GenerateVideosOperation;
import com.google.genai.types.Image;
import com.google.genai.types.ReferenceImage;
import com.google.genai.types.UpscaleImageConfig;
import com.google.genai.types.UpscaleImageResponse;
import java.util.List;
Expand Down Expand Up @@ -153,6 +156,30 @@ public CompletableFuture<GenerateImagesResponse> generateImages(
return CompletableFuture.supplyAsync(() -> models.generateImages(model, prompt, config));
}

/**
* Asynchronously edits an image given a GenAI model, a prompt, and a list of reference images.
*
* @param model the name of the GenAI model to use for editing capabilities
* @param prompt the prompt to edit the image
* @param referenceImages a {@link List<com.google.genai.types.ReferenceImage>} to send to use for
* editing. The 5 types of reference images are: {@link
* com.google.genai.types.RawReferenceImage}, {@link
* com.google.genai.types.MaskReferenceImage}, {@link
* com.google.genai.types.ControlReferenceImage}, {@link
* com.google.genai.types.StyleReferenceImage}, {@link
* com.google.genai.types.SubjectReferenceImage},
* @param config a {@link com.google.genai.types.EditImageConfig} instance that specifies the
* optional configurations
* @return a {@link com.google.genai.types.EditImageResponse} instance that contains the edited
* image.
*/
public CompletableFuture<EditImageResponse> editImage(
String model, String prompt, List<ReferenceImage> referenceImages, EditImageConfig config) {

return CompletableFuture.supplyAsync(
() -> models.editImage(model, prompt, referenceImages, config));
}

/**
* Asynchronously upscales an image given a GenAI model and an image and an upscale factor.
*
Expand Down
Loading