MENU
Table of contents

Thanks for downloading Dynamsoft Barcode Reader Package!

Your download will start shortly. If your download does not begin, click here to retry.

User Guide for C Language

System Requirements

  • Operating systems:
    • Windows: 7, 8, 10, 2003, 2008, 2008 R2, 2012;
    • Linux x64: Ubuntu 14.04.4+ LTS, Debian 8+, etc;
    • Linux arm 32bit;
    • Linux arm 64bit (contact us to get the SDK);
    • macOS 64bit: 10.15+ (contact us to get the SDK).

 

Installation

You can download Dynamsoft Barcode Reader SDK from the Dynamsoft website and run the setup program. The trial installer includes a free trial license valid for 30 days.

After installation, you can find samples for supported platforms in the Samples folder under the installation folder.

 

Getting Started: HelloWorld

  1. Start Visual Studio and create a new Win32 Console Application in C. Let’s name it BarcodeReadDemo_C.
  2. Add Dynamsoft Barcode Reader headers and libs in BarcodeReadDemo_C.c.
     #include <stdio.h>
     #include "<relative path>/DynamsoftCommon.h"
     #include "<relative path>/DynamsoftBarcodeReader.h"
        
     #ifdef _WIN64
     #pragma comment(lib, "<relative path>/DBRx64.lib")
     #else
     #pragma comment(lib, "<relative path>/DBRx86.lib")
     #endif
    

    Please replace <relative path> in the code with the relative path to the BarcodeReadDemo_C.c file. Typically, The DynamsoftBarcodeReader.h file can be found in DBR-C_CPP-{version number}\DynamsoftBarcodeReader\Include\, and the LIB files can be found in DBR-C_CPP-{version number}\DynamsoftBarcodeReader\Lib\.

  3. Update the main function in BarcodeReadDemo_C.c.
    int main()
    {
        // Define variables
        void *hBarcode = NULL;
        int iRet = -1;
        int iIndex = 0;
        int iLicMsg = -1;
        TextResultArray* pResult = NULL;
        hBarcode = DBR_CreateInstance();
    
        // Initialize license prior to any decoding
        iLicMsg = DBR_InitLicense(hBarcode, "<your license key here>");
    
        //If error occurs to the license initialization
         if (iLicMsg != DBR_OK) 
        {
            printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
            return iLicMsg;
        }
    
        // Start decoding. Leave the template name empty ("") will use the settings from PublicRuntimeSettings
        DBR_DecodeFile(hBarcode, "<your image file full path>", "");
    
        // Get the text result
        iRet = DBR_GetAllTextResults(hBarcode, &pResult);
    
        // If error occurs
        if (iRet != DBR_OK) 
        {
            printf("Failed to read barcode: %d\r\n%s\r\n", iRet, DBR_GetErrorString(iRet));
            return iRet;
        }
    
        // If succeeds
        printf("%d total barcode(s) found. \n", pResult->resultsCount);
        for (iIndex = 0; iIndex < pResult->resultsCount; iIndex++)
        {
            printf("Result %d\n", iIndex + 1);
            printf("Barcode Format: %s\n", pResult->results[iIndex]->barcodeFormatString);
            printf("Text reads: %s \n", pResult->results[iIndex]->barcodeText);
        }
    
        // Finally release BarcodeResultArray and destroy instance
        DBR_FreeTextResults(&pResult);
        DBR_DestroyInstance(hBarcode);
        system("pause");
        return 0;
    }
    

    Please update <your image file full path> and <your license key here> in the code accordingly.

  4. Run the project.

    Build the application and copy the related DLL files to the same folder as the EXE file. The DLLs can be found in DBR-C_CPP-{version number}\DynamsoftBarcodeReader\Lib\{Platform}\.

    To test, you can open the Command Prompt and execute the EXE file with a barcode image.

To deploy your application, make sure the DLLs are in the same folder as the EXE file. See the Distribution section for more details.

 

Decoding Methods

The SDK provides multiple decoding methods that support reading barcodes from different sources, including static images, video stream, files in memory, base64 string, bitmap, etc. Here is a list of all decoding methods:

You can find more samples in more programming languages at Code Gallery.

 

Barcode Reading Settings

Calling the decoding methods directly will use the default scanning modes and it will satisfy most of the needs. The SDK also allows you to adjust the scanning settings to optimize the scanning performance for different usage scenarios.

There are two ways to change the barcode reading settings - using the PublicRuntimeSettings Struct or template. For new developers, We recommend you to start with the PublicRuntimeSettings struct; For those who are experienced with the SDK, you may use a template which is more flexible and easier to update.

Use PublicRuntimeSettings Struct to Change Settings

Here are some common scanning settings you might find helpful:

For more scanning settings guide, check out the How To section.

Specify Barcode Type to Read

By default, the SDK will read all the supported barcode formats except Postal Codes and Dotcode from the image. (See Product Overview for the full supported barcode list.)

If your full license only covers some barcode formats, you can use BarcodeFormatIds and BarcodeFormatIds_2 to specify the barcode format(s). Check out BarcodeFormat and BarcodeFormat_2.

For example, to enable only 1D barcode reading, you can use the following code:

void *hBarcode = NULL;
char sError[512];
TextResultArray* pResult = NULL;
PublicRuntimeSettings runtimeSettings;
hBarcode = DBR_CreateInstance();
// Initialize license prior to any decoding
//Replace "<Put your license key here>" with your own license
DBR_InitLicense(hBarcode, "<Put your license key here>");
//Set the barcode format
DBR_GetRuntimeSettings(hBarcode, &runtimeSettings);
runtimeSettings.barcodeFormatIds = 2047; // OneD barcode
DBR_UpdateRuntimeSettings(hBarcode, &runtimeSettings,sError,512);
//Replace "Put the path of your file here" with your own file path
DBR_DecodeFile(hBarcode,"<Put your file path here>","");
DBR_GetAllTextResults(hBarcode, &pResult);
DBR_FreeTextResults(&pResult);
DBR_DestroyInstance(hBarcode);

Specify maximum barcode count

By default, the SDK will read as many barcodes as it can. To increase the recognition efficiency, you can use expectedBarcodesCount to specify the maximum number of barcodes to recognize according to your scenario.

void *hBarcode = NULL;
char sError[512];
TextResultArray* pResult = NULL;
PublicRuntimeSettings runtimeSettings;
hBarcode = DBR_CreateInstance();
// Initialize license prior to any decoding
//Replace "<Put your license key here>" with your own license
DBR_InitLicense(hBarcode, "<Put your license key here>");
//Set the number of barcodes to be expected
DBR_GetRuntimeSettings(hBarcode, &runtimeSettings);
runtimeSettings.expectedBarcodesCount = 1;
DBR_UpdateRuntimeSettings(hBarcode, &runtimeSettings, sError, 512);
//Replace "<Put the path of your file here>" with your own file path
DBR_DecodeFile(hBarcode,"<Put your file path here>","");
DBR_GetAllTextResults(hBarcode, &pResult);
DBR_FreeTextResults(&pResult);
DBR_DestroyInstance(hBarcode);

Specify a scan region

By default, the barcode reader will search the whole image for barcodes. This can lead to poor performance especially when dealing with high-resolution images. You can speed up the recognition process by restricting the scanning region.

To specify a region, you will need to define an area. The following code shows how to create a template string and define the region.

void *hBarcode = NULL;
char sError[512];
TextResultArray* pResult = NULL;
PublicRuntimeSettings runtimeSettings;
hBarcode = DBR_CreateInstance();
// Initialize license prior to any decoding
//Replace "<Put your license key here>" with your own license
DBR_InitLicense(hBarcode, "<Put your license key here>");
//Decode the barcodes on the left half of the image
DBR_GetRuntimeSettings(hBarcode, &runtimeSettings);
runtimeSettings.region.regionBottom = 100;
runtimeSettings.region.regionLeft = 0;
runtimeSettings.region.regionRight = 50;
runtimeSettings.region.regionTop = 0;
runtimeSettings.region.regionMeasuredByPercentage = 1; //The region is determined by percentage
DBR_UpdateRuntimeSettings(hBarcode, &runtimeSettings,sError,512);
//Replace "<Put the path of your file here>" with your own file path
DBR_DecodeFile(hBarcode,"put your file path here","");
DBR_GetAllTextResults(hBarcode, &pResult);
DBR_FreeTextResults(&pResult);
DBR_DestroyInstance(hBarcode);

Use A Template to Change Settings

Besides the option of using the PublicRuntimeSettings struct, the SDK also provides DBR_InitRuntimeSettingsWithString and DBR_InitRuntimeSettingsWithFile APIs that enable you to use a template to control all the runtime settings. With a template, instead of writing many codes to modify the settings, you can manage all the runtime settings in a JSON file/string.

void *hBarcode = NULL;
char sError[512];
TextResultArray* pResult = NULL;
hBarcode = DBR_CreateInstance();
// Initialize license prior to any decoding
//Replace "<Put your license key here>" with your own license
DBR_InitLicense(hBarcode, "<Put your license key here>");
//Use a template to modify the runtime settings
//DBR_InitRuntimeSettingsWithString() can also be used to modify the runtime settings with a json string
DBR_InitRuntimeSettingsWithFile(hBarcode, "<Put your file path here>", CM_OVERWRITE, sError, 512);
//Output runtime settings to a json file.
//DBR_OutputLicenseToString() can also be used to output the settings to a string
DBR_OutputSettingsToFile(hBarcode, "<Put your file path here>", "runtimeSettings");
//Replace "<Put your file path here>" with your own file path
DBR_DecodeFile(hBarcode,"put your file path here","");
DBR_GetAllTextResults(hBarcode, &pResult);
DBR_FreeTextResults(&pResult);
DBR_DestroyInstance(hBarcode);

Below is a template for your reference. To learn more about the APIs, you can check out PublicRuntimeSettings Struct.

{
   "ImageParameter" : {
      "BarcodeFormatIds" : [ "BF_ALL" ],
      "BinarizationModes" : [
         {
            "BlockSizeX" : 0,
            "BlockSizeY" : 0,
            "EnableFillBinaryVacancy" : 1,
            "ImagePreprocessingModesIndex" : -1,
            "Mode" : "BM_LOCAL_BLOCK",
            "ThreshValueCoefficient" : 10
         }
      ],
      "Description" : "",
      "ExpectedBarcodesCount" : 0,
      "GrayscaleTransformationModes" : [
         {
            "Mode" : "GTM_ORIGINAL"
         }
      ],
      "ImagePreprocessingModes" : [
         {
            "Mode" : "IPM_GENERAL"
         }
      ],
      "IntermediateResultSavingMode" : {
         "Mode" : "IRSM_MEMORY"
      },
      "IntermediateResultTypes" : [ "IRT_NO_RESULT" ],
      "MaxAlgorithmThreadCount" : 4,
      "Name" : "runtimesettings",
      "PDFRasterDPI" : 300,
      "Pages" : "",
      "RegionDefinitionNameArray" : null,
      "RegionPredetectionModes" : [
         {
            "Mode" : "RPM_GENERAL"
         }
      ],
      "ResultCoordinateType" : "RCT_PIXEL",
      "ScaleDownThreshold" : 2300,
      "TerminatePhase" : "TP_BARCODE_RECOGNIZED",
      "TextFilterModes" : [
         {
            "MinImageDimension" : 65536,
            "Mode" : "TFM_GENERAL_CONTOUR",
            "Sensitivity" : 0
         }
      ],
      "TextResultOrderModes" : [
         {
            "Mode" : "TROM_CONFIDENCE"
         },
         {
            "Mode" : "TROM_POSITION"
         },
         {
            "Mode" : "TROM_FORMAT"
         }
      ],
      "TextureDetectionModes" : [
         {
            "Mode" : "TDM_GENERAL_WIDTH_CONCENTRATION",
            "Sensitivity" : 5
         }
      ],
      "Timeout" : 10000
   },
   "Version" : "3.0"
}

How to Distribute

Distribute the required library files with the applications using the Dynamsoft Barcode Reader SDK. The distribution files can be found under:

DBR-C_CPP-{version number}\DynamsoftBarcodeReader\Lib\{Platform}\

How to Upgrade

From version 7.x

You need to replace the old assembly files with the ones in the latest version. Download the latest version here.

Your previous SDK license for version 7.x is not compatible with the version 8.x. Please contact us to upgrade your license.

After you upgraded your license to version 8.x:

  • If you were using DBR_InitLicense, please replace the old license with the newly generated one.

  • If you were using DBR_InitLicenseFromServer to connect to Dynamsoft server for license verification, then no need to change the license key. But please make sure the device has Internet connection.

  • If you were using DBR_InitLicenseFromServer + DBR_InitLicenseFromLicenseContent to connect to Dynamsoft server once and use the SDK offline, please follow these steps to re-register the device.

  • If you were using DBR_InitLicenseFromLicenseContent to use the SDK offline, please follow these steps to re-register the device.

From version 6.x

We made some structural updates in the new version. To upgrade from 6.x to 8.x, we recommend you to review our sample code and re-write the barcode scanning module.

This page is compatible for:

Version 7.5

Is this page helpful?

YesYes NoNo

In this article:

latest version

    • Latest Version
    • version 7.6
    • version 7.5
    Change +
    © 2003–2020 Dynamsoft. All rights reserved.
    Privacy Statement / Site Map / Home / Purchase / Support