|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Usage: |
| 19 | + * |
| 20 | + * <p>1a. If you are using Vertex AI, setup ADC to get credentials: |
| 21 | + * https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp |
| 22 | + * |
| 23 | + * <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables: |
| 24 | + * |
| 25 | + * <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT |
| 26 | + * |
| 27 | + * <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION |
| 28 | + * |
| 29 | + * <p>1b. If you are using Gemini Developer AI, set an API key environment variable. You can find a |
| 30 | + * list of available API keys here: https://aistudio.google.com/app/apikey |
| 31 | + * |
| 32 | + * <p>export GOOGLE_API_KEY=YOUR_API_KEY |
| 33 | + * |
| 34 | + * <p>2. Compile the java package and run the sample code. |
| 35 | + * |
| 36 | + * <p>mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.EditImageAsync" |
| 37 | + */ |
| 38 | +package com.google.genai.examples; |
| 39 | + |
| 40 | +import com.google.genai.Client; |
| 41 | +import com.google.genai.types.EditImageConfig; |
| 42 | +import com.google.genai.types.EditImageResponse; |
| 43 | +import com.google.genai.types.GenerateImagesConfig; |
| 44 | +import com.google.genai.types.GenerateImagesResponse; |
| 45 | +import com.google.genai.types.Image; |
| 46 | +import com.google.genai.types.MaskReferenceConfig; |
| 47 | +import com.google.genai.types.MaskReferenceImage; |
| 48 | +import com.google.genai.types.RawReferenceImage; |
| 49 | +import com.google.genai.types.ReferenceImage; |
| 50 | +import java.io.IOException; |
| 51 | +import java.util.ArrayList; |
| 52 | +import java.util.concurrent.CompletableFuture; |
| 53 | +import org.apache.http.HttpException; |
| 54 | + |
| 55 | +/** An example of using the Unified Gen AI Java SDK to edit an image asynchronously. */ |
| 56 | +public class EditImageAsync { |
| 57 | + public static void main(String[] args) throws IOException, HttpException { |
| 58 | + // Instantiates the client using Vertex AI, and sets the project and location in the builder. |
| 59 | + Client client = |
| 60 | + Client.builder() |
| 61 | + .vertexAI(true) |
| 62 | + .project(System.getenv("GOOGLE_CLOUD_PROJECT")) |
| 63 | + .location(System.getenv("GOOGLE_CLOUD_LOCATION")) |
| 64 | + .build(); |
| 65 | + |
| 66 | + GenerateImagesConfig generateImagesConfig = |
| 67 | + GenerateImagesConfig.builder().numberOfImages(1).outputMimeType("image/jpeg").build(); |
| 68 | + |
| 69 | + CompletableFuture<GenerateImagesResponse> generateImagesResponseFuture = |
| 70 | + client.async.models.generateImages( |
| 71 | + "imagen-3.0-generate-001", |
| 72 | + "An umbrella in the foreground, and a rainy night sky in the background", |
| 73 | + generateImagesConfig); |
| 74 | + |
| 75 | + generateImagesResponseFuture |
| 76 | + .thenAccept( |
| 77 | + generatedImagesResponse -> { |
| 78 | + Image generatedImage = |
| 79 | + generatedImagesResponse.generatedImages().get().get(0).image().get(); |
| 80 | + |
| 81 | + // Edit image with a mask. |
| 82 | + EditImageConfig editImageConfig = |
| 83 | + EditImageConfig.builder() |
| 84 | + .editMode("EDIT_MODE_INPAINT_INSERTION") |
| 85 | + .numberOfImages(1) |
| 86 | + .outputMimeType("image/jpeg") |
| 87 | + .build(); |
| 88 | + |
| 89 | + ArrayList<ReferenceImage> referenceImages = new ArrayList<>(); |
| 90 | + RawReferenceImage rawReferenceImage = |
| 91 | + RawReferenceImage.builder().referenceImage(generatedImage).referenceId(1).build(); |
| 92 | + referenceImages.add(rawReferenceImage); |
| 93 | + |
| 94 | + MaskReferenceImage maskReferenceImage = |
| 95 | + MaskReferenceImage.builder() |
| 96 | + .referenceId(2) |
| 97 | + .config( |
| 98 | + MaskReferenceConfig.builder() |
| 99 | + .maskMode("MASK_MODE_BACKGROUND") |
| 100 | + .maskDilation(0.0f) |
| 101 | + .build()) |
| 102 | + .build(); |
| 103 | + referenceImages.add(maskReferenceImage); |
| 104 | + |
| 105 | + CompletableFuture<EditImageResponse> editImageResponseFuture = |
| 106 | + client.async.models.editImage( |
| 107 | + "imagen-3.0-capability-001", |
| 108 | + "Sunlight and clear sky", |
| 109 | + referenceImages, |
| 110 | + editImageConfig); |
| 111 | + |
| 112 | + editImageResponseFuture |
| 113 | + .thenAccept( |
| 114 | + editImageResponse -> { |
| 115 | + Image editedImage = |
| 116 | + editImageResponse.generatedImages().get().get(0).image().get(); |
| 117 | + // Do something with editedImage. |
| 118 | + }) |
| 119 | + .join(); |
| 120 | + }) |
| 121 | + .join(); |
| 122 | + } |
| 123 | +} |
0 commit comments