Sample code for 30+ languages & platforms
Java Requires Chilkat v11.4.0+

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Ai;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Ai ai = new Ai();

        ai.setProvider("openai");

        // Use your provider's API key.
        ai.setApiKey("MY_API_KEY");

        // Choose a model.
        ai.setModel("gpt-5.6-terra");

        JsonObject askParams = new JsonObject();
        askParams.updateString("image.output_format", "jpeg");
        askParams.updateString("image.size", "1024x1024");
        askParams.updateString("image.quality", "low");
        ai.setAskParams(askParams);

        ai.inputAddText("Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf");

        // Ask the AI for image output.
        try {
            ai.ask("image");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the image response data.
        BinData bdImageData = new BinData();
        try {
            ai.getOutputBd(bdImageData);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        bdImageData.writeFile("c:/aaworkarea/out.jpg");

        System.out.println("Success.");
    }
}