StableDiffusionBridge lets you generate images locally from text prompts.
The bridge returns raw RGBA pixel data so your app can convert it into the most appropriate image type for the current platform.
Initialize the model#
val modelPath = StableDiffusionBridge.getModelPath("dreamshaper.safetensors")
val ok = StableDiffusionBridge.initModel(modelPath, threads = 4)
check(ok)Generate an image#
val rgba = StableDiffusionBridge.txt2img(
prompt = "A cinematic illustration of a llama explorer",
negativePrompt = "blurry, low quality, extra limbs",
width = 512,
height = 512,
steps = 20,
cfgScale = 7.0f,
seed = 1234L,
)Understanding the result#
The output is an RGBA ByteArray.
To display it, convert the bytes into your platform image type using the same width and height you requested.
Parameter guidance#
widthandheight: start with512x512unless you have benchmarked larger valuessteps: around 20 is a good mobile-friendly defaultcfgScale: around 7.0 to 8.0 is a common starting rangeseed: use a fixed value for reproducible results
Performance advice#
Image generation is significantly heavier than text generation. On mobile devices:
- prefer moderate output sizes
- avoid excessive step counts for the first iteration
- reuse one initialized model for many generations
Cleanup#
StableDiffusionBridge.release()