1. Kotlin
  2. Template-Based Barcode Scanning

Kotlin

Template-Based Barcode Scanning

Template-Based Barcode Scanning is a powerful mode in the Vision SDK that enables scanning specific barcodes from a document based on a predefined template. This is ideal when a document contains multiple barcodes, but you only want to extract a particular subset — for example, barcodes associated with tracking numbers, invoice codes, or specific labels.

🎯 Use Case

Imagine a shipping label that includes:

  • A QR code for tracking
  • A Code128 for internal processing
  • A UPC for product identification

With template-based scanning, you can configure the SDK to scan only the tracking QR code and ignore the others.

🧩 Creating a Template

To create a custom template, you need to first make sure that your Activity extends from ComponentActivity. We have extension functions added in ComponentActivity that you can use to start the Activity to create a template. We also have extension callback for when a template is created.

Starting the Template Creation Activity

        class YourActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setTemplateCreatedCallback { newCreatedTemplateId ->
            if (newCreatedTemplateId == null || newCreatedTemplateId.isEmpty()) {
                // Template creation was cancelled
                return@setTemplateCreatedCallback
            }

            // Template was successfully created and saved
        }

        createTemplateButton.setOnClickListener {
            startCreateTemplateScreen()
        }
    }
}

      

💾 Managing Template

The Vision SDK automatically saves created templates into its secure storage. You can manage these templates using the following methods:

  • Retrieve all saved template IDs:

            val allTemplates: List<BarcodeTemplate> = TemplateManager().getAllBarcodeTemplates()
    
          
  • Delete a specific template:

            val templateToDelete: BarcodeTemplate = getTemplateToDelete()
    TemplateManager().deleteBarcodeTemplate(templateToDelete)
    
          
  • Update a specific template:

            val templateToUpdate: BarcodeTemplate = getTemplateToUpdate()
    val updatedTemplate = templateToUpdate.copy(name = updatedName)
    val templateManager =  TemplateManager()
    templateManager.deleteBarcodeTemplate(templateToUpdate)
    templateManager.saveBarcodeTemplate(updatedTemplate)
    
          

📸 Scanning with a Template

After creating and saving a template, you can configure the scanner to use it for scanning specific barcodes:

        val templateManager =  TemplateManager()
val templateToApply: BarcodeTemplate = getTemplateToApply(templateManager)
visionCameraView.applyBarcodeTemplate(templateToApply)

      

✅ Benefits

  • Selective Scanning: Focus on specific barcodes within a document.
  • Efficiency: Reduce processing time by ignoring irrelevant barcodes.
  • Customization: Tailor scanning behavior to match your document layouts.

Sample Code

        class YourActivity : ComponentActivity() {

    private val visionCameraView: VisionCameraView by lazy { findViewById(R.id.vision_camera_view) }

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_your)

        // Setting Template creation callback
        setTemplateCreatedCallback { newCreatedTemplateId ->
            if (newCreatedTemplateId == null || newCreatedTemplateId.isEmpty()) {
                // Template creation was cancelled
                return@setTemplateCreatedCallback
            }

            // Template was successfully created and saved
        }

        // Start Template creation screen on button press
        createTemplateButton.setOnClickListener {
            startCreateTemplateScreen()
        }

        // Start camera
        visionCameraView.startCamera()

        // Get template you want to apply
        val templateManager =  TemplateManager()
        val templateToApply: BarcodeTemplate? = getTemplateToApply(templateManager)

        // Apply the template
        if (templateToApply != null) {
            visionCameraView.applyBarcodeTemplate(templateToApply)
        }
    }

    private fun getTemplateToApply(templateManager: TemplateManager): BarcodeTemplate? {
        return templateManager.getAllBarcodeTemplates().firstOrNull()
    }
}