Barcode Reading Settings

Read Barcodes with Default Mode

The general steps to use the SDK are as follows:

  1. Initialize the SDK with a license key. Typically, it only needs to be done once in the application, before calling decoding methods.

  2. Set optional barcode reading settings, such as barcode type, scanning region, etc. For new customers, you can skip this step and it will use the default settings to search for all barcode types at any angle in the whole image. For customers who want to customize the barcode reading settings, please refer to the following chapters.

  3. Perform barcode reading.

    Below are the code snippets for using default settings in C/C++/C#.

    //C
    void *hBarcode = NULL;
    int iRet = -1;
      int iIndex = 0;
      int iLicMsg = -1;
      TextResultArray* pResult = NULL;
    hBarcode = DBR_CreateInstance();
    
    // Initialize license prior to any decoding
    //Replace “Put your license key here” with your own license
    iLicMsg = DBR_InitLicense(hBarcode, "Put your license key here");
    
    //If error occurs to the license
    if (iLicMsg != DBR_OK) {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }
    
    //Replace “Put the path of your file here” with your own file path
    DBR_DecodeFile(hBarcode,"Put your file path here","");
    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;
     }
    
    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);
     }
    
    DBR_FreeTextResults(&pResult);
    DBR_DestroyInstance(hBarcode);
    return 0;
    
    //C++
    int main()
    {
    int iRet = -1;
     int iLicMsg = -1;
     char sError[512];
     TextResultArray* paryResult = NULL;
     PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
     CBarcodeReader* reader = new CBarcodeReader();
    
    //Initialize license prior to any decoding
    //Replace "Put your license key here" with your own license
    iLicMsg = reader->InitLicense("Put your license key here");
    
    //If error occurs to the license
    if (iLicMsg != DBR_OK) 
     {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }
    
    //Replace “Put the path of your file here” with your own file path
    iRet = reader->DecodeFile("Put your file path here", "");
    
    // 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
     reader->GetAllTextResults(&paryResult);
     printf("%d total barcodes found. \r\n", paryResult->resultsCount);
      for (int iIndex = 0; iIndex < paryResult->resultsCount; iIndex++)
      {
        printf("Result %d\r\n", iIndex + 1);
        printf("BarcodeFormat: %s\r\n", paryResult->results[iIndex]->barcodeFormatString);
        printf("Text read: %s\r\n", paryResult->results[iIndex]->barcodeText);
     }
    
     CBarcodeReader::FreeTextResults(&paryResult);
     delete runtimeSettings;
     delete reader;
    return 0;
    }
    
    //C#
    BarcodeReader barcodeReader = new BarcodeReader();
    // Replace <Put your license key here> with your own license. 
    barcodeReader.ProductKeys = "<Put your license key here>";
    TextResult[] aryResult = barcodeReader.DecodeFile(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\Images\AllSupportedBarcodeTypes.tif”, string.Empty);
    

If you have already installed the Dynamsoft Barcode Reader 30-day free trial, you can find the license in the "C:\Program Files (x86)\Dynamsoft\Barcode Reader {version number}\LicenseManager.exe".

Note that if you don’t pass a license to the SDK or the license has expired, the SDK will continue to function normally but the last three characters of the barcode result will be masked with “*”.

Specify Which Barcode Type to Read

By default, the SDK will read the following supported barcode types from the image:

  • 1D barcodes: Code39, Code93, Code128, Codabar, ITF, EAN13, EAN8, UPCA, UPCE, INDUSTRIAL 2 OF 5;
  • 2D barcodes: QRCode, PDF417, DataMatrix, Aztec.

If your full license only covers one or part of barcode types, you must specify which barcode type(s) the SDK should read. For example, to enable only 1D barcode reading, please use the following:

    //C
    void *hBarcode = NULL;
    int iRet = -1;
      int iIndex = 0;
      int iLicMsg = -1;
    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
    iLicMsg = DBR_InitLicense(hBarcode, "Put your license key here");

    //If error occurs to the license
    if (iLicMsg != DBR_OK) {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }

    //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","");
    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;
      }

    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);
      }

    DBR_FreeTextResults(&pResult);
    DBR_DestroyInstance(hBarcode);
    return 0;
    //C++
    int main()
    {
            int iRet = -1;
            int iLicMsg = -1;
            char sError[512];
            TextResultArray* paryResult = NULL;
            PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
            CBarcodeReader* reader = new CBarcodeReader();

            //Initialize license prior to any decoding
            //Replace "Put your license key here" with your own license
            iLicMsg = reader->InitLicense("Put your license key here");

            //If error occurs to the license
            if (iLicMsg != DBR_OK) 
            {
                printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
                return iLicMsg;
            }

    //Set the barcode format
    reader->GetRuntimeSettings(runtimeSettings);
     runtimeSettings->barcodeFormatIds = 2047; //OneD barcode
    reader->UpdateRuntimeSettings(runtimeSettings, sError, 512);

    //Replace “Put the path of your file here” with your own file path
    iRet = reader->DecodeFile("Put your file path here", "");

    // 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
     reader->GetAllTextResults(&paryResult);
     printf("%d total barcodes found. \r\n", paryResult->resultsCount);
      for (int iIndex = 0; iIndex < paryResult->resultsCount; iIndex++)
      {
        printf("Result %d\r\n", iIndex + 1);
        printf("BarcodeFormat: %s\r\n", paryResult->results[iIndex]->barcodeFormatString);
        printf("Text read: %s\r\n", paryResult->results[iIndex]->barcodeText);
     }

     CBarcodeReader::FreeTextResults(&paryResult);
     delete runtimeSettings;
     delete reader;
    return 0;
    }
    //C#
    BarcodeReader barcodeReader = new BarcodeReader();
    barcodeReader.LicenseKeys = "Put your license key here.";
    // get the runtime settings from the BarcodeReader object
    PublicRuntimeSettings settings = barcodeReader.GetRuntimeSettings();
    //only have the barcode types which you have a license for included. 
    settings.BarcodeFormatIds = 2047; // OneD barcode
    // update the runtime settings to the BarcodeReader object
    barcodeReader.UpdateRuntimeSettings(settings);
    // setting the template name to empty will use the runtime settings you just updated

    TextResult[] aryResult = barcodeReader.DecodeFile(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\Images\AllSupportedBarcodeTypes.tif”, string.Empty);

Read a Single Barcode

By default, the SDK will read as many barcodes as it can. You can specify the maximum number of barcodes to read before decoding according to your use case. This can increase the recognition efficiency.

    //C
    void *hBarcode = NULL;
    int iRet = -1;
     int iIndex = 0;
     int iLicMsg = -1;
    char sError[512];
     TextResultArray* pResult = NULL;
    PublicRuntimeSettings runtimeSettings; 
    hBarcode = DBR_CreateInstance();

    //Replace “Put your license key here” with your own license
    iLicMsg = DBR_InitLicense(hBarcode, "Put your license key here");

    //If error occurs to the license
    if (iLicMsg != DBR_OK) {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }

    //Set the number of barcodes to be expected 
    DBR_GetRuntimeSettings(hBarcode, &runtimeSettings);
    runtimeSettings.expectedBarcodesCount = 1;
    DBR_UpdateRuntimeSettings(hBarcode, &runtimeSettings, szError, 512);

    //Replace "Put the path of your file here" with your own file path
    DBR_DecodeFile(hBarcode,"put your file path here","");
    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;
     }

    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);
     }

    DBR_FreeTextResults(&pResult);
    DBR_DestroyInstance(hBarcode);
    return 0;
    //C++
    int main()
    {
    int iRet = -1;
     int iLicMsg = -1;
     char sError[512];
     TextResultArray* paryResult = NULL;
     PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
     CBarcodeReader* reader = new CBarcodeReader();

    //Initialize license prior to any decoding
    //Replace "Put your license key here" with your own license
    iLicMsg = reader->InitLicense("Put your license key here");

    //If error occurs to the license
    if (iLicMsg != DBR_OK) 
     {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }

    //Set the number of barcodes to be expected 
    reader->GetRuntimeSettings(runtimeSettings);
    runtimeSettings->expectedBarcodesCount = 1;
    reader->UpdateRuntimeSettings(runtimeSettings, sError, 512);

    //Replace “Put the path of your file here” with your own file path
    iRet = reader->DecodeFile("Put your file path here", "");

    // 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
     reader->GetAllTextResults(&paryResult);
     printf("%d total barcodes found. \r\n", paryResult->resultsCount);
      for (int iIndex = 0; iIndex < paryResult->resultsCount; iIndex++)
      {
        printf("Result %d\r\n", iIndex + 1);
        printf("BarcodeFormat: %s\r\n", paryResult->results[iIndex]->barcodeFormatString);
        printf("Text read: %s\r\n", paryResult->results[iIndex]->barcodeText);
     }

     CBarcodeReader::FreeTextResults(&paryResult);
     delete runtimeSettings;
     delete reader;
    return 0;
    }
    //C#
    // get the runtime settings from the BarcodeReader object
    PublicRuntimeSettings settings = barcodeReader.GetRuntimeSettings();
    // limit it to decode only 1 barcode
    settings.ExpectedBarcodesCount = 1; 
    // update the runtime settings to the BarcodeReader object
    barcodeReader.UpdateRuntimeSettings(settings);
    // setting the template name to empty will use the runtime settings you just updated
    TextResult[] aryResult = barcodeReader.DecodeFile(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\Images\AllSupportedBarcodeTypes.tif”, string.Empty);

Read from a Region

By default, the barcode reader will search the whole image. 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 create a template string/file and define the area in the RegionDefinition. For more details on RegionDefinition, please refer to this chapter.

The following function shows how to create a template string and define the region:

    //C
    void *hBarcode = NULL;
    int iRet = -1;
     int iIndex = 0;
     int iLicMsg = -1;
     char sError[512];
     TextResultArray* pResult = NULL;
     PublicRuntimeSettings runtimeSettings;

     hBarcode = DBR_CreateInstance();
     // Initialize license prior to any decoding
     iLicMsg = DBR_InitLicense(hBarcode, "put your license key here");

    //If error occurs to the license
     if (iLicMsg != DBR_OK) {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }

    //Decode the barcodes on the left 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","");
    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);
     }
    DBR_FreeTextResults(&pResult);
    DBR_DestroyInstance(hBarcode);
    return 0;
    //C++
    int main()
    {
    int iRet = -1;
     int iLicMsg = -1;
     char sError[512];
     TextResultArray* paryResult = NULL;
     PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
     CBarcodeReader* reader = new CBarcodeReader();

    //Initialize license prior to any decoding
    //Replace "Put your license key here" with your own license
    iLicMsg = reader->InitLicense("Put your license key here");

    //If error occurs to the license
    if (iLicMsg != DBR_OK) 
     {
        printf("Failed to initialize the license successfully: %d\r\n%s\r\n", iLicMsg, DBR_GetErrorString(iLicMsg));
        return iLicMsg;
     }

    //Decode the barcodes on the left of the image
    reader->GetRuntimeSettings(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
    reader->UpdateRuntimeSettings(runtimeSettings, sError, 512);

    //Replace “Put the path of your file here” with your own file path
    iRet = reader->DecodeFile("Put your file path here", "");

    // 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
     reader->GetAllTextResults(&paryResult);
     printf("%d total barcodes found. \r\n", paryResult->resultsCount);
     for (int iIndex = 0; iIndex < paryResult->resultsCount; iIndex++)
     {
        printf("Result %d\r\n", iIndex + 1);
        printf("BarcodeFormat: %s\r\n", paryResult->results[iIndex]->barcodeFormatString);
        printf("Text read: %s\r\n", paryResult->results[iIndex]->barcodeText);
     }

     CBarcodeReader::FreeTextResults(&paryResult);
     delete runtimeSettings;
     delete reader;
     return 0;
    }
    //C#
    // get the runtime settings from the BarcodeReader object
    PublicRuntimeSettings settings = barcodeReader.GetRuntimeSettings();
    // define the region, top 20%
    settings.Region.RegionTop  = 0; 
    settings.Region.RegionLeft  = 0; 
    settings.Region.RegionBottom  = 20; 
    settings.Region.RegionRight  = 100; 
    settings.Region.RegionMeasuredByPercentage  = true; 
    // update the runtime settings to the BarcodeReader object
    barcodeReader.UpdateRuntimeSettings(settings);
    // setting the template name to empty will use the runtime settings you just updated
    TextResult[] aryResult = barcodeReader.DecodeFile(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\Images\AllSupportedBarcodeTypes.tif”, string.Empty);

For example:

results matching ""

    No results matching ""