Skip to main content

Java quick start for the Linux Barcode Scanner SDK

Build a lightweight Java app on Linux that uses the Scanbot SDK. You'll configure Gradle to fetch the platform-specific JAR and run a minimal example.

Create the project and install the Scanbot SDK​

Open a terminal and execute the following commands to create a project folder:

1. Init Java project with Gradle​

mkdir barcode-quickstart-java
cd barcode-quickstart-java
gradle init --type java-application

Gradle will generate the basic project structure with a sample App.java.

2. Add the Scanbot SDK to build.gradle​

Open build.gradle and replace its content with the following. Replace <SCANBOTSDK_VERSION> with the version you want to install.

import java.net.URL
import java.io.FileOutputStream

// Replace with the actual version (e.g., "9.0.0")
val scanbotSdkVersion = "<SCANBOTSDK_VERSION>"

// Detect host architecture to pick the correct JAR
val arch = System.getProperty("os.arch")
val scanbotArch = if (arch.contains("arm") || arch.contains("aarch64")) "aarch64" else "x86_64"

// Helper to download a file into build/libs and use it as a dependency
fun urlFile(url: String, name: String) = run {
val file = layout.buildDirectory.dir("libs").get().file(name).asFile
file.parentFile.mkdirs()
if (!file.exists()) {
URL(url).openStream().use { input ->
FileOutputStream(file).use { output ->
input.copyTo(output)
}
}
}
files(file.absolutePath)
}

dependencies {
implementation(
urlFile(
"https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv$scanbotSdkVersion/scanbotsdk-$scanbotSdkVersion-linux-$scanbotArch.jar",
"scanbotsdk.jar"
)
)
}

Initialize the Scanbot SDK​

Replace the default App.java with:

package org.example;

import io.scanbot.sdk.ScanbotSDK;
import io.scanbot.sdk.licensing.LicenseInfo;

public class App {
public static void main(String[] args) throws Exception {
final String LICENSE_KEY = "SCANBOTSDK-LICENSE"; // TODO: replace with your key
final String WRITABLE_PATH = ".";

ScanbotSDK.initialize(LICENSE_KEY, WRITABLE_PATH);

LicenseInfo info = ScanbotSDK.getLicenseInfo();
System.out.printf("License status=%s", info.getStatus());
}
}

Create and run the Barcode Scanner​

After successful initialization, create a scanner configuration and run it on an ImageRef.

private static void runBarcodeScanner(ImageRef image) throws Exception {
BarcodeScannerConfiguration config = new BarcodeScannerConfiguration();
// Configure other parameters as needed.

try (BarcodeScanner scanner = new BarcodeScanner(config);
BarcodeScannerResult result = scanner.run(image)) {

List<BarcodeItem> barcodes = result.getBarcodes();

if (barcodes.isEmpty()) {
System.out.println("No barcodes found.");
return;
}

for (int i = 0; i < barcodes.size(); i++) {
BarcodeItem b = barcodes.get(i);
System.out.printf("Barcode #%d:%n", i + 1);
System.out.printf(" Format: %s%n", b.getFormat().name());
System.out.printf(" Text: %s%n", b.getText());
}
}
}

Complete example​

import java.util.List;

import io.scanbot.sdk.ScanbotSDK;
import io.scanbot.sdk.licensing.LicenseInfo;
import io.scanbot.sdk.barcode.*;
import io.scanbot.sdk.image.*;

public class App {
public static void main(String[] args) throws Exception {
final String LICENSE_KEY = "SCANBOTSDK-LICENSE"; // TODO: replace with your key
final String WRITABLE_PATH = System.getProperty("user.dir");

ScanbotSDK.initialize(LICENSE_KEY, WRITABLE_PATH);

LicenseInfo info = ScanbotSDK.getLicenseInfo();
System.out.printf("License status=%s", info.getStatus());

if (args.length < 1) {
System.err.println("Usage: ./gradlew run --args='/path/to/image.jpg'");
System.exit(1);
}

String imagePath = args[0];
try (ImageRef image = ImageRef.fromPath(imagePath, new PathImageLoadOptions())) {
runBarcodeScanner(image);
}
}

private static void runBarcodeScanner(ImageRef image) throws Exception {
BarcodeScannerConfiguration config = new BarcodeScannerConfiguration();
// Configure other parameters as needed.

try (BarcodeScanner scanner = new BarcodeScanner(config);
BarcodeScannerResult result = scanner.run(image)) {

List<BarcodeItem> barcodes = result.getBarcodes();

if (barcodes.isEmpty()) {
System.out.println("No barcodes found.");
return;
}

for (int i = 0; i < barcodes.size(); i++) {
BarcodeItem b = barcodes.get(i);
System.out.printf("Barcode #%d:%n", i + 1);
System.out.printf(" Format: %s%n", b.getFormat().name());
System.out.printf(" Text: %s%n", b.getText());
}
}
}
}

Run​

./gradlew run --args='/path/to/image.jpg'

🚀 That's it! You've built a minimal barcode scanner in Java on Linux.

💡 In this quick start guide, we use the SDK's default scanner settings. However, you can freely customize the configuration, supported barcode types, and performance options.

Get in touch​

If you need further information or are interested in licensing the Scanbot SDK, please get in touch with our solution experts.