# Class ImageGenerator

java.lang.Object
com.codename1.ai.ImageGenerator

public abstract class ImageGenerator extends Object

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
  Constructor
  Description
  `ImageGenerator()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract AsyncResource<Image>`
  `generate(GenerateImageRequest req)`
  Generates an image without blocking the EDT.
  `static ImageGenerator`
  `onDevice()`
  On-device generator.
  `static ImageGenerator`
  `openai(String apiKey)`
  Creates an OpenAI image generator.
  `static ImageGenerator`
  `replicate(String apiKey)`
  Replicate runs a wide catalog of third-party image models (SDXL, Flux, etc.) behind a uniform REST API.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ImageGenerator

    public ImageGenerator()

- ## Method Details

  - ### openai

    public static ImageGenerator openai(String apiKey)
    Creates an OpenAI image generator.
    Parameters:
      `apiKey` - OpenAI API key
    Returns:
      configured generator

  - ### replicate

    public static ImageGenerator replicate(String apiKey)
    Replicate runs a wide catalog of third-party image models (SDXL, Flux, etc.) behind a uniform REST API. Pass the API token from `https://replicate.com/account`.
    Parameters:
      `apiKey` - Replicate API token
    Returns:
      configured generator

  - ### onDevice

    public static ImageGenerator onDevice()
    On-device generator. Requires the optional cn1lib `cn1-ai-stablediffusion`; without it this returns an `AsyncResource` that completes with `UnsupportedOperationException`.
    Returns:
      optional on-device generator or an unsupported stub

  - ### generate

    public abstract AsyncResource<Image> generate(GenerateImageRequest req)
    Generates an image without blocking the EDT.
    Parameters:
      `req` - provider-neutral generation request
    Returns:
      asynchronous generated image