Svelte

Accura IDScan Plugin — Svelte Integration Guide

This guide walks you through integrating the Accura IDScan Plugin into a Svelte project built with Vite.

Step 1: Initialize Project

If you do not have an existing Svelte project, scaffold one using Vite:

npm create vite@latest my-id-app -- --template svelte
cd my-id-app
npm install

Step 2: Install Plugin

Install the Accura IDScan Plugin package from the npm registry:

npm install accuraidscanplugin

Step 3: Implementation

Create a dedicated scanner component at src/lib/IDScanner.svelte. The following snippet shows only the plugin import and initialization logic:

<script>
  import { onMount, onDestroy } from 'svelte';

  let plugin = null; // Holds the active plugin instance for lifecycle management

  onMount(async () => {
    // Dynamically import the plugin inside onMount to guarantee browser-only execution.
    // Svelte's onMount lifecycle hook runs exclusively on the client side,
    // making it safe to access browser APIs such as the camera.
    const { default: IDCardPlugin } = await import('accuraidscanplugin');

    // Instantiate the plugin with:
    //   1. The capture callback invoked when the scan session is complete
    //   2. A configuration object specifying the target card and UI appearance
    plugin = new IDCardPlugin(
      handleCapture,   // Fired automatically upon successful ID card scan
      {
        countryCode: "UGA",    // Country code of the ID Card
        cardCode: "UGNIDF",    // Card code of front ID Card
        topTextSize: "",       // Top overlay text size (default if empty)
        topTextColor: "",      // Top overlay text color (default if empty)
        topTextWeight: "",     // Top overlay font weight (default if empty)
        bottomTextSize: "",    // Bottom overlay text size (default if empty)
        bottomTextColor: "",   // Bottom overlay text color (default if empty)
        bottomTextWeight: "",  // Bottom overlay font weight (default if empty)
      }
    );

    // Activate the camera and begin the ID card detection session.
    await plugin.start();
  });

  // Release camera resources and destroy the plugin when the component is removed from the DOM.
  onDestroy(() => {
    if (plugin) {
      plugin.destroy();
    }
  });
</script>

Step 4: Response Handling

When the plugin completes an ID card scan, it invokes the handleCapture callback with a payload object. This object may contain one or both of the following properties:

Property
Type
Description

front

string

Base64 Data URL of the front side of the ID card

back

string

Base64 Data URL of the back side of the ID card (if applicable)

What is Base64? Base64 is a binary-to-text encoding scheme that converts raw binary image data into a sequence of printable ASCII characters. Each scanned card image is delivered as a Data URL string (e.g., data:image/jpeg;base64,/9j/...), combining a MIME type prefix with the encoded image payload. Before transmitting to a server, this string must be decoded back into binary form (a Blob) to construct a valid multipart HTTP request.

The following demonstrates the base64-to-Blob conversion and API submission:


Step 5: Demo Implementation

The following is the complete, production-ready component. Copy and paste it directly into src/lib/IDScanner.svelte. The original logic is preserved exactly as-is.


Step 6: Usage

Import and render the component in App.svelte:


Step 7: Running the Project

Last updated