Class ImageGenerator
java.lang.Object
com.codename1.ai.ImageGenerator
Cloud-first image generation. The openai and replicate factory
methods cover the two dominant managed endpoints. On-device
generation via Core ML / ONNX Stable Diffusion is provided
separately by the optional cn1-ai-stablediffusion cn1lib; see
onDevice().
ImageGenerator.openai(KeyStore.get("openai_key"))
.generate(new GenerateImageRequest("A cat in a sombrero").setSize("1024x1024"))
.ready(img -> imageComponent.setIcon(img));
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract AsyncResource<Image> Generates an image without blocking the EDT.static ImageGeneratoronDevice()On-device generator.static ImageGeneratorCreates an OpenAI image generator talking to the public endpoint.static ImageGeneratoropenAiCompatible(String baseUrl, String apiKey) Creates a generator for any endpoint that speaks the OpenAI image generation API -- most usefully your own server, holding the provider key and proxying the call, so the device carries nothing more than a short-lived token of yours.static ImageGeneratorReplicate runs a wide catalog of third-party image models (SDXL, Flux, etc.) behind a uniform REST API.
-
Constructor Details
-
ImageGenerator
public ImageGenerator()
-
-
Method Details
-
openai
Creates an OpenAI image generator talking to the public endpoint.
Shipping this means the key travels with the app, and a key the running app can read is one its user can extract. For a released build, put the key on your server and point
openAiCompatible(String, String)at it instead.- Parameters:
apiKey- OpenAI API key- Returns:
- configured generator
-
openAiCompatible
Creates a generator for any endpoint that speaks the OpenAI image generation API -- most usefully your own server, holding the provider key and proxying the call, so the device carries nothing more than a short-lived token of yours.- Parameters:
baseUrl- endpoint root, for examplehttps://api.example.com/ai/v1; required, because falling back to the public endpoint would send your proxy's token and your user's prompt to a third partyapiKey- credential for that endpoint, or empty when it needs none- Returns:
- configured generator
- Throws:
IllegalArgumentException- whenbaseUrlis null or empty
-
replicate
Replicate runs a wide catalog of third-party image models (SDXL, Flux, etc.) behind a uniform REST API. Pass the API token fromhttps://replicate.com/account.- Parameters:
apiKey- Replicate API token- Returns:
- configured generator
-
onDevice
On-device generator. Requires the optional cn1libcn1-ai-stablediffusion; without it this returns anAsyncResourcethat completes withUnsupportedOperationException.- Returns:
- optional on-device generator or an unsupported stub
-
generate
Generates an image without blocking the EDT.- Parameters:
req- provider-neutral generation request- Returns:
- asynchronous generated image
-