Skip to main content
Speed up SparkScan integration with Agent Skills

Install the Scandit plugin and use the /sparkscan-kmp skill so that your AI coding agent can integrate, debug, and customize SparkScan on Kotlin Multiplatform following Scandit's recommended patterns. More info →

Run the following command in your project directory. It detects the supported coding agents you have installed and adds the Scandit plugin to each. Re-run it to update.

npx plugins add scandit/skills

Prefer to set it up yourself? Manual installation steps for each agent →

Configuring SparkScan

SparkScan works without any configuration. This page shows the code for the changes you can make to it. For what each option means for your users, see How SparkScan works.

Most options are on SparkScanViewSettings. Pass your settings as viewSettings to the Compose SparkScanView, or to the imperative SparkScanView constructor. The Compose SparkScanView reads viewSettings only when it creates the view. To apply other settings, pass a new SparkScanViewSettings object and create the view again, for example by wrapping the composable in key(viewSettings).

Using a hardware button to scan​

Let users start scanning with the hardware button instead of the trigger button on the screen. On iOS, the hardware button is the volume button.

Before you start:

  1. Set SparkScanViewSettings.hardwareTriggerEnabled to true before you create the SparkScanView. It's off by default. On Android, changes after that don't apply to the view.

    import com.kmp.datacapture.barcode.spark.SparkScanViewSettings

    val viewSettings = SparkScanViewSettings.sparkScanViewSettings().apply {
    hardwareTriggerEnabled = true
    }
  2. Optional: to choose the button on Android, set its key code in SparkScanViewSettings.hardwareTriggerKeyCode. By default, the button is the dedicated hardware button on XCover devices and the volume-down button on other devices.

To check that it works: press the hardware button. The scanner starts, as it does when users tap the trigger button.

Showing an error for a scanned barcode​

Show an error when your app doesn't accept a barcode, for example one that's already on a list or that comes from the wrong lot. Users see your message, and scanning pauses until the timeout ends or they tap the trigger button. For how this works and how to choose the timeout, see Error state.

  1. Implement SparkScanFeedbackDelegate. In getFeedbackForBarcode(), return a SparkScanBarcodeSuccessFeedback or a SparkScanBarcodeErrorFeedback with a message and a timeout. This method runs on a background thread, so update the UI on the main thread.

    import com.kmp.datacapture.barcode.data.Barcode
    import com.kmp.datacapture.barcode.spark.SparkScanBarcodeErrorFeedback
    import com.kmp.datacapture.barcode.spark.SparkScanBarcodeFeedback
    import com.kmp.datacapture.barcode.spark.SparkScanBarcodeSuccessFeedback
    import com.kmp.datacapture.barcode.spark.SparkScanFeedbackDelegate

    class MyFeedbackDelegate : SparkScanFeedbackDelegate {
    override fun getFeedbackForBarcode(barcode: Barcode): SparkScanBarcodeFeedback? =
    if (isValidBarcode(barcode)) {
    SparkScanBarcodeSuccessFeedback()
    } else {
    SparkScanBarcodeErrorFeedback(
    errorMessage = "This code should not have been scanned",
    resumeCapturingDelay = 60_000L, // in milliseconds
    )
    }

    private fun isValidBarcode(barcode: Barcode): Boolean {
    // Your validation logic, for example a lookup in your item list
    return true
    }
    }
  2. Set the delegate on SparkScanView.feedbackDelegate.

    Use the imperative SparkScanView for this: the Compose SparkScanView has no parameter for a feedback delegate.

    sparkScanView.feedbackDelegate = MyFeedbackDelegate()

You can return different errors for different conditions, for example one for a wrong barcode and another for a duplicate.

Each error has its own message and timeout, and can also set the color of the screen flash (visualFeedbackColor), the brush that highlights the barcode (brush), and the sound and vibration (feedback). Set them with the constructor, where every parameter after the timeout is optional.

To turn off the screen flash for every scan, set SparkScanViewSettings.visualFeedbackEnabled to false.

To check that it works: scan a barcode that your app rejects. The error message appears, and the scanner pauses for the timeout or until you tap the trigger button.

Rejecting barcodes​

Reject a barcode without pausing the scanner when a wrong barcode shouldn't stop users. They get the error feedback and can keep scanning right away.

  1. In the feedback delegate from Showing an error for a scanned barcode, return a SparkScanBarcodeErrorFeedback whose resumeCapturingDelay is 0L.

    SparkScanBarcodeErrorFeedback(
    errorMessage = "This code is already on the list",
    resumeCapturingDelay = 0L,
    )

To check that it works: scan a barcode that your app rejects. You get the error feedback, and you can scan the next barcode right away.

Customizing the UI​

Change which buttons and controls the SparkScan view shows, and control scanning from your own UI. The full list of SparkScanView properties is in the SparkScanView API reference.

With the Compose SparkScanView, choose which controls are visible with its parameters, such as torchControlVisible and zoomSwitchControlVisible. For example, to hide the torch and zoom controls:

SparkScanView(
settings = settings,
torchControlVisible = false,
zoomSwitchControlVisible = false,
)

The Compose SparkScanView has no startScanning(), pauseScanning() or uiListener. To use your own trigger button or to handle the buttons for other scanning modes, as the following sections describe, use the imperative SparkScanView and set its properties.

You can change most of the SparkScan UI to fit your app:

  • The colors of icons, buttons and the toolbar
  • The trigger button and its icon
  • The text of every toast message
  • The size of the mini preview
  • Which icons are visible

Using your own trigger button​

The default trigger button fits most apps. When yours needs a different control, for example a button that's already part of your UI, hide the default trigger button with sparkScanView.triggerButtonVisible and control scanning yourself:

  • To start scanning, call sparkScanView.startScanning() from your own button. To update your button when the scanner's state changes, use the SparkScan view UI listener.
  • To stop scanning, show the mini preview's close button with sparkScanView.previewCloseControlVisible, or call sparkScanView.pauseScanning() from your own button.

You can also rely only on a hardware button, if you turned that on with sparkScanViewSettings.hardwareTriggerEnabled.

Two examples of a customized SparkScan UI:

SparkScan in a self-scanning app with a custom floating button and an expanded preview
A self-scanning app with its own floating button, and the preview expanded by default.
SparkScan in a tablet point-of-sale app that uses its existing scan button
A mobile point-of-sale app that starts the scanner from its existing scan button.

Showing the settings toolbar​

The settings toolbar is hidden by default. When you show one of its buttons, such as the selection mode or scanning behavior button, the toolbar appears at the bottom of the mini preview.

In the Compose SparkScanView, the settings toolbar shows by default, because some of its buttons are visible by default.

Showing buttons for other scanning modes​

Some workflows need another scanning mode next to SparkScan, such as MatrixScan Find, MatrixScan Count or Smart Label Capture. SparkScan has prebuilt toolbar buttons that switch to those modes from the SparkScan UI.

First, show the buttons:

// Show the Smart Label Capture, MatrixScan Count and MatrixScan Find buttons
sparkScanView.labelCaptureButtonVisible = true
sparkScanView.barcodeCountButtonVisible = true
sparkScanView.barcodeFindButtonVisible = true
The settings toolbar without buttons for other scanning modes
The default toolbar, without buttons for other scanning modes.
The settings toolbar with buttons for other scanning modes
The toolbar with the MatrixScan Find and MatrixScan Count buttons.

Then set your UI listener on the SparkScanView with sparkScanView.uiListener. It's called when users tap the MatrixScan Find, MatrixScan Count or Smart Label Capture button in the toolbar.

Changing the scanning workflow​

Change the workflow when the default one doesn't fit how your users scan. Three settings set the scanner's initial state, and you can combine them.

  1. Set the selection mode with SparkScanSettings.selectionMode.

  2. Set the scanning behavior with SparkScanViewSettings.scanningBehavior and the preview behavior with SparkScanViewSettings.previewBehavior.

  3. Optional: to stop users from scanning continuously by touching & holding the trigger button, set SparkScanViewSettings.holdToScanEnabled to false.

To check that it works: tap the trigger button. The scanner starts in the selection mode, scanning behavior, and preview behavior you set.

For the values and defaults, see the SparkScan settings reference. For what each mode does, see How SparkScan works.

Next step​

SparkScan settings reference