Dev Center
Table of contents

Getting Started with C Language

In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using C language.

Requirements

  • Operating System:
    • Windows 7, 8, 10, 11, 2003, 2008, 2008 R2, 2012, 2016, 2019, 2022
    • Linux x64: Ubuntu 14.04.4+ LTS, Debian 8+, etc
    • Linux arm 32bit
    • Linux arm 64bit
    • MacOS 64bit: 10.12+ (not included in the trial package, contact us to get the SDK)
  • Developing Tool
    • Visual Studio 2008 or above
    • GCC 5.4+

Note: Dynamsoft Barcode Reader provides both online and offline license options. The online license option might not work in an environment that doesn’t have network connection. In such case, you can get an offline trial license key via Customer Portal or by contacting us.

Installation

If you haven’t downloaded the SDK yet, download the C/C++ Package now from Dynamsoft website and unpack the package into the directory of your choice.

For this tutorial, we unpack it to [INSTALLATION FOLDER], change it to your unpacking path for the following content.

Build Your First Application

Let’s start by creating a console application which demonstrates how to use the minimum code to read barcodes from an image file.

You can download the entire source code here.

Create a New Project

For Windows

  1. Open Visual Studio. Go to File > New > Project, create a new Empty Project and set Project name as DBRCSample.

  2. Add a new source file named DBRCSample.c into the project.

For Linux/ARM/Mac

  1. Create a new source file named DBRCSample.c and place it into the folder [INSTALLATION FOLDER]/Samples.

Include the Library

  1. Add headers and libs in DBRCSample.c.

     #include <stdio.h>
     #include "[INSTALLATION FOLDER]/Include/DynamsoftBarcodeReader.h"
     #if defined(_WIN64) || defined(_WIN32)
         #ifdef _WIN64
             #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x64/DBRx64.lib")
         #else
             #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x86/DBRx86.lib")
         #endif
     #endif
    

Initialize a Barcode Reader Instance

  1. Initialize the license key.

     int errorCode = 0;
     char szErrorMsg[512];
     errorCode = DBR_InitLicense("<insert DBR license key here>", szErrorMsg, 512);
     if (errorCode != DBR_OK)
     {
         // Add your code for license error processing;
         printf("%s\n", szErrorMsg);
     }
    

    Please replace <insert DBR license key here> with a valid DBR licensekey. There are two ways to obtain one:

    • Search DBR_InitLicense and find the license from [INSTALLATION FOLDER]/Samples/BarcodeReaderDemo/BarcodeReaderDemo.cpp.
    • Request a trial license from Customer Portal.
  2. Create an instance of Dynamsoft Barcode Reader.

     void* dbr = DBR_GetInstance();
     if(dbr != NULL)
     {
         // Add your code here to call decoding method, process barcode results and so on
         // ...
         DBR_RecycleInstance(dbr);
     }
    

Configure the Barcode Scanning Behavior

  1. Set barcode format and count to read.

     char szErrorMsg[512];
     PublicRuntimeSettings settings;
     DBR_GetRuntimeSettings(dbr, &settings);
     settings.barcodeFormatIds = BF_PDF417; 
     settings.barcodeFormatIds_2 = BF2_DOTCODE; 
     settings.expectedBarcodesCount = 2;
     DBR_UpdateRuntimeSettings(dbr, &settings, szErrorMsg, 512);
    

    The barcode formats to enable is highly application-specific. We recommend that you only enable the barcode formats your application requires. Check out Barcode Format Enumeration for full supported barcode formats.

    If you know exactly the barcode count you want to read, specify expectedBarcodesCount to speed up the process and improve the accuracy.

    The Barcode Reader SDK comes with a large array of runtime settings to optimize the performance of the library. To learn about all the runtime settings, please visit the RuntimeSettings API page. To learn more about the cases and situations in which the settings can help, please visit the Explore Features page.

Decode and Output Results

  1. Decode barcodes from an image file.

     int errorCode = -1;
     errorCode = DBR_DecodeFile(dbr, "[INSTALLATION FOLDER]/Images/AllSupportedBarcodeTypes.png", "");
     if(errorCode != DBR_OK)
         printf("%s\n", DBR_GetErrorString(errorCode));
    

    For the error handling mechanism, the SDK returns Error Code for each function and provides a function DBR_GetErrorString to get the readable message. You should add codes for error handling based on your needs. Check out Error Code for full supported error codes.

  2. Get and output barcode results.

     TextResultArray* barcodeResults = NULL;
     DBR_GetAllTextResults(dbr, &barcodeResults);
     if (barcodeResults != NULL && barcodeResults->resultsCount > 0)
     {
         printf("%d total barcode(s) found. \n", barcodeResults->resultsCount);
         for (int index = 0; index < barcodeResults->resultsCount; index++)
         {
             printf("Result %d\n", index + 1);
             printf("    Barcode Format: %s\n", barcodeResults->results[index] ->barcodeFormatString);
             printf("    Barcode Text: %s \n", barcodeResults->results[index] ->barcodeText);
         }
     }
    

    The SDK returns multiple barcode information, including barcode count, barcode format, barcode text, location, barcode raw data, etc. Check out TextResult for full supported result data.

Release Allocated Memory

  1. Release the allocated memory for the barcode results and instance.

     if(barcodeResults != NULL)           
         DBR_FreeTextResults(&barcodeResults);
    
  2. Release the allocated memory for the instance.

     if(dbr != NULL)           
         DBR_RecycleInstance(dbr);
    

Note:
Please change all [INSTALLATION FOLDER] in above code snippet to your unpacking path.

Build and Run the Project

For Windows

  1. In Visual Studio, set the solution to build as Release|x64.

  2. Build the project to generate program DBRCSample.exe.

  3. Copy ALL *.dll files under [INSTALLATION FOLDER]\Lib\Windows\x64 to the same folder as the DBRCSample.exe.

  4. Run the program DBRCSample.exe.

The SDK supports both x86 and x64, please set the platform based on your needs.

For Linux/ARM/Mac

  1. Open a terminal and change to the target directory where DBRCSample.c located in. Build the sample:

     gcc -o DBRCSample DBRCSample.c -lDynamsoftBarcodeReader -L ../Lib/Linux -Wl,-rpath=../Lib/Linux
    

    Please replace Linux to ARM32 or ARM64 based on your platform.

  2. Run the program DBRCSample.

     ./DBRCSample
    

You can download the entire source code here.

Next Steps

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 10.x
    • Version 10.2.0
    • Version 10.0.20
    • Version 10.0.10
    • Version 10.0.0
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.30
    • Version 9.6.20
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.0
    • Version 9.0.0
  • Version 8.x
    • Version 8.8.0
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.0
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
Change +