Building Kotlin Barcode Reader on Windows

Kotlin, developed by JetBrain, is a statically typed programming language that compiles to Java code and JavaScript code. Recently, Google announced that Kotlin is a first-class language for writing Android apps. The big Android market will bring a promising future for Kotlin. If you never used Kotlin before and don’t want to fall behind, get started with the new programming language now. In this article, I want to share the experience of writing my first Kotlin app – a command line barcode reader.

Dev Tool and Barcode Library

  • IntelliJ IDEA Community is the recommended tool for developing Kotlin project.
  • Dynamsoft Barcode Reader SDK provides dynamsoft.barcode.jar for writing barcode reader app in Java. Since Kotlin is designed with Java interoperability, we can directly use the Java library in Kotlin project.

Kotlin: Hello World

Read official Kotlin tutorial, create an app.kt file to write a Kolin app quickly:

fun main(args: Array<String>) {
    println("Hello World")

Kotlin’s documentation provides a lot of new knowledge, and it’s hard to digest in a short time. Luckily, Java is easily convertible to Kotlin. Comparing to reading Kotlin documentation, I prefer to learn Kotlin by converting Java code. Here is the classic ‘Hello World’ program in Java:

public class HelloWorld {
    public static void main(String\[\] args) {
        System.out.println("Hello World");

Let’s paste it to app.kt to see what will happen in IntelliJ IDEA. The IDE will automatically convert it to Kotlin code:

object HelloWorld {

    @JvmStatic fun main(args: Array<String>) {
        println("Hello World")


Press ‘Ctrl + Shift + F10’ to run the app:

kotlin build error

Something is wrong with the error message:

!!! JUnit version 3.8 or later expected

java.lang.ClassNotFoundException: junit.framework.ComparisonFailure.

The file name causes the issue! Changing app.kt to HelloWorld.kt will fix the problem.

Kotlin Barcode Reader

Press ‘Ctrl + Shift + Alt + S’ to open project structure. Click Libraries to add com.dynamsoft.barcode.jar file.

 Kotlin barcode library

Dynamsoft Barcode Reader SDK already contains a Java sample. So just copy from Dynamsoft\Barcode Reader 5.1\Samples\Desktop\Java to your Kotlin project.

On the menu bar, select Code > Convert Java File to Kotlin File:

java to kotlin

            // Read barcode
            val result = br.readFile(pszImageFile!!, lFormat, iMaxCount)

            if (result.errorCode != BarcodeReader.DBR\_OK && result.errorCode != BarcodeReader.DBRERR\_LICENSE\_EXPIRED && result.errorCode != BarcodeReader.DBRERR\_LICENSE\_INVALID) {

            // Output barcode result

            var pszTemp: String

            if (result.barcodes == null || result.barcodes.size == 0) {
                pszTemp = String.format(
                        "No barcode found. Total time spent: %.3f seconds.", (ullTimeEnd - ullTimeBegin).toFloat() / 1000)
            } else {
                pszTemp = String.format("Total barcode(s) found: %d. Total time spent: %.3f seconds.", result.barcodes.size, (ullTimeEnd - ullTimeBegin).toFloat() / 1000)

                iIndex = 0
                while (iIndex < result.barcodes.size) {
                    val barcode = result.barcodes\[iIndex\]

                    pszTemp = String.format("  Barcode %d:", iIndex + 1)
                    pszTemp = String.format("    Page: %d", barcode.pageNumber)
                    pszTemp = String.format("    Type: %s", barcode.formatString)
                    pszTemp = "    Value: " + barcode.displayValue

                    pszTemp = String.format("    Region: {Left: %d, Top: %d, Width: %d, Height: %d}",
                            barcode.boundingBox.x, barcode.boundingBox.y, barcode.boundingBox.width, barcode.boundingBox.height)


There is an error - Operator ‘!=’ cannot be applied to ‘Long’ and ‘Int’.

Kotlin long int

To fix it, add ‘L’ after 0:

if (lFormat != 0L)

Now we can successfully run the Kotlin barcode app.

Source Code

Search Blog Posts