Prerequisites
- A working Android quickstart
- Advanced filters enabled for the project
- A decoded
Bitmapor readable local video path
Replace with an image
NosmaiBackgroundSegmentationConfig config =
new NosmaiBackgroundSegmentationConfig(
NosmaiBackgroundSegmentationConfig.Mode.IMAGE
);
config.replacementImage = backgroundBitmap;
NosmaiEffects.setBackgroundSegmentation(config);
For a color, use Mode.COLOR and set an Android ARGB integer in replacementColor. For video, use Mode.VIDEO and set a readable file path or content URI in replacementVideoPath.
Change or clear it
NosmaiEffects.clearBackgroundSegmentation();
Do not recycle the supplied bitmap while the background configuration is active.
Quality guidance
Use even front lighting and an asset close to the preview aspect ratio. Test hair, glasses, quick motion, low light, and both camera facings on the lowest supported device.
Verify it worked
The source background should be replaced without restarting Camera2. Pause and resume the app once to confirm the configuration and camera lifecycle recover.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Background stays unchanged | Pipeline is not ready or advanced filters are unavailable | Check readiness and project capabilities |
| Video fails | Path or content URI is not readable | Resolve permission and pass a local readable value |
| Preview slows | Segmentation and camera resolution exceed the device budget | Use 30 FPS, start at 720p, and test a Release build |