Skip to main content

Cleaning up documents with the iOS Document Scanner

Document Cleanup lets the end user erase unwanted artifacts from a scanned page while preserving the underlying document content. Artifacts include handwriting, stamps, stains, fingers, shadows, and other distracting elements. The user paints over the regions to remove. The SDK then reconstructs a clean result image, with optional text preservation.

The Scanbot SDK ships a ready-to-use SwiftUI view, SBSDKDocumentCleanupCanvas, that wraps the custom document cleanup API and handles the painting and pinch-to-zoom gestures. It's driven by an SBSDKDocumentCleanupCanvasViewModel, which owns the cleanup state, the undo / redo history, and the stroke settings. The host application stays in full control of the surrounding chrome (top bar, toolbar, buttons, sliders, etc.).

Creating the cleanup canvas​

Set up the canvas as follows:

  1. Create an SBSDKImageRef from the page image.
  2. Configure the underlying engine with an SBSDKDocumentCleanupConfiguration.
  3. Use both to create the view model.
  4. Present the SwiftUI view as usual, e.g. inside a UIHostingController.
Creating the Document Cleanup Canvas
loading...

Usage of the Document Cleanup canvas​

The view model publishes the current image, the processing state, and the undo / redo availability. Observe it to enable or disable your own controls and to show a progress indicator while a cleanup operation is running. To apply a cleanup action, call undo(), redo(), or reset() on it.

Document Cleanup Custom UI View
loading...

Configuration options​

Configure the cleanup behavior of the underlying engine through DocumentCleanupConfiguration. In the Ready-to-Use UI, pass it as screens.cleanup.engineConfiguration.

PropertyTypeDefaultDescription
keepTextBooleantrueIf true, the SDK keeps detected text intact while the user erases around it. Text detection runs once up front and can take a moment, so construct the cleanup pipeline asynchronously to keep the UI responsive.
maxUndoRedoStackSizeInt10How many undo and redo steps the SDK keeps in memory. Use at least 1 so that reset can always restore the original image. When the limit is reached, the SDK merges the two oldest steps into one. The user can still return to the original image, but with fewer steps in between.
maxCleanupResolutionInt0Caps the brushed area the SDK processes in one cleanup step, as width × height in pixels. Larger areas are processed at a lower resolution and scaled back up, which saves memory but can soften the edges near the stroke. 0 means no limit. For example, 1200000 keeps peak memory at about 0.5 GB for a 12 MP image. Prefer cleaning smaller areas over relying on this limit.

Cleanup status​

A cleanup run reports a DocumentCleanupStatus alongside the resulting image:

ValueMeaning
OKCleanup completed at full resolution.
OK_BUT_REDUCED_QUALITYCleanup completed, but maxCleanupResolution forced an internal downscale of the cleanup region, so quality near the cleanup boundary may be reduced. If the result is not good enough, undo the last operation and apply cleanup on a smaller area.

View model properties and callbacks​

In addition to the SBSDKDocumentCleanupConfiguration, SBSDKDocumentCleanupCanvasViewModel exposes the following properties and callbacks to customize appearance and interaction:

  • strokeSize — diameter of the drawing brush, in view points.
  • strokeColor — color of the stroke preview drawn while the user paints.
  • isProcessing — true while a heavy cleanup operation is running; use it to drive a progress indicator and to disable your controls.
  • canUndo / canRedo — whether an undo or redo step is currently available.
  • undo() / redo() / reset() — apply the corresponding cleanup action.
  • currentImageRefSnapshot() — returns the current result image as an SBSDKImageRef, e.g. to store or share it.
  • onFailure — called when a cleanup operation fails.
  • onReducedQuality — called when a cleanup run completes with reduced quality because the affected area exceeds maxCleanupResolution.
  • onAreaTooLarge — called when a stroke is rejected because the masked area is too large to process.

Want to scan longer than one minute?

Generate a free trial license to test the Scanbot SDK thoroughly.

Get free trial license