Build a Hello World Application

Prerequisites

Before getting started, please make sure you have already installed Dynamsoft Barcode Reader SDK. If not, please refer to the Installation section to install the development kit on your machine.

The trial installer includes a 30-day free trial license by default that allows you to evaluate full features of the SDK. For more information, please refer to the Use a trial key section.

Start coding

This section will show how to build a Hello World application that reads barcodes from a static image in different programming languages.

Create a C app for barcode reading

To build a C application that reads barcodes from an image, you can follow the steps below:

  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.

    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 [INSTALLATION FOLDER]\Components\C_C++\Include\, and the LIB files can be found in [INSTALLATION FOLDER]\Components\C_C++\Lib\.

     #include <stdio.h>
     #include "<relative path>/DynamsoftBarcodeReader.h"
    
     #ifdef _WIN64
     #pragma comment(lib, "<relative path>/DBRx64.lib")
     #else
     #pragma comment(lib, "<relative path>/DBRx86.lib")
     #endif
    
  3. Update the main function in BarcodeReadDemo_C.c.

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

    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;
    }
    
  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 [INSTALLATION FOLDER]\Components\C_C++\Redist\.

    • For x86 mode, required DLLs are: x86\DynamsoftBarcodeReaderx86.dll,x86\vcomp110.dll,x86\DynamicPdf.dll (for decoding barcodes from PDF files), x86\DynamsoftLicClientx86.dll.
    • For x64 mode, required DLLs are: x64\DynamsoftBarcodeReaderx64.dll, x64\vcomp110.dll, x64\DynamicPdfx64.dll (for decoding barcodes from PDF files), x64\DynamsoftLicClientx64.dll.

    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.

Create a C++ app for barcode reading

To build a C++ application that reads barcodes from an image, you can follow the steps below:

  1. Start Visual Studio and create a new Win32 Console Application in C++. Let's name it BarcodeReadDemo_CPP.

  2. Add headers and libs in BarcodeReadDemo_CPP.cpp.

    Please replace <relative path> in the code with the relative path to the BarcodeReadDemo_CPP.cpp file. Typically, The DynamsoftBarcodeReader.h file can be found in [INSTALLATION FOLDER]\Components\C_C++\Include\, and the LIB files can be found in [INSTALLATION FOLDER]\Components\C_C++\Lib\.

     #include <stdio.h>
     #include "<relative path>/DynamsoftBarcodeReader.h"
    
     #ifdef _WIN64
     #pragma comment(lib, "<relative path>/DBRx64.lib")
     #else
     #pragma comment(lib, "<relative path>/DBRx86.lib")
     #endif
    
  3. In BarcodeReadDemo_CPP.cpp, please update the main function.

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

    int main()
    {
     // Define variables
     int iRet = -1;
     int iLicMsg = -1;
     TextResultArray *paryResult = NULL;
    
     // Initialize license prior to any decoding
     CBarcodeReader reader;
     iLicMsg = reader.InitLicense("<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.
     iRet = reader.DecodeFile("<your image file full path>", "");
    
     // 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);
     }
    
     // Finally release BarcodeResultArray
     CBarcodeReader::FreeTextResults(&paryResult);
     system("pause");
     return 0;
    }
    
  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 [INSTALLATION FOLDER]\Components\C_C++\Redist\.

    • For x86 mode, required DLLs are: x86\DynamsoftBarcodeReaderx86.dll,x86\vcomp110.dll,x86\DynamicPdf.dll (for decoding barcodes from PDF files) and x86\DynamsoftLicClientx86.dll.
    • For x64 mode, required DLLs are: x64\DynamsoftBarcodeReaderx64.dll, x64\vcomp110.dll, x64\DynamicPdfx64.dll (for decoding barcodes from PDF files) and x64\DynamsoftLicClientx64.dll.

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

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

Create a C# app for barcode reading

To build a C# application that reads barcodes from an image, you can follow the steps below:

  1. Start Visual Studio and create a new Windows Forms Application in C#.

  2. Right click References in Solution Explorer and add Dynamsoft.BarcodeReader.dll to the project.

    The DLLs can be found in the installation folder. Typically, they are under:

    • For .NET Framework 2.0-3.5:

      [INSTALLATION FOLDER]\Components\DotNet\2.0\

    • For .NET Framework 4.0 and later:

      [INSTALLATION FOLDER]\Components\DotNet\4.0\

  3. Add namespace:

    using Dynamsoft.Barcode;

  4. Create a new button on the form and replace the code of button1_Click() with the following.

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

     private void button1_Click(object sender, EventArgs e)
     {
         try
         {
             BarcodeReader dbr = new BarcodeReader();
             dbr.ProductKeys = "<your license key here>";
             TextResult[] aryResult = dbr.DecodeFile("<your image file full path>", ""); // Leave the template name empty ("") will use the settings from PublicRuntimeSettings
    
             if (aryResult.Length == 0)
             {
                 Console.WriteLine("No barcode found.");
             }
             else
             {
                 for (var i = 0; i < aryResult.Length; i++)
                 {
                     Console.WriteLine("Barcode: {0}", (i + 1));
                     Console.WriteLine("BarcodeFormat: {0}", aryResult[i].BarcodeFormat.ToString());
                     Console.WriteLine("BarcodeText: {0}", aryResult[i].BarcodeText);
                 }
             }
         }
         catch (BarcodeReaderException exp)
         {
             Console.WriteLine("Error: {0}, {1}", exp.Code.ToString(), exp.Message);
         }
     }
    
  5. Run the project.

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

Create a VB.NET app for barcode reading

To build a VB.NET application that reads barcodes from an image, you can follow the steps below:

  1. Start Visual Studio and create a new Windows Forms Application in VB.NET.

  2. Right click References in Solution Explorer and add Dynamsoft.BarcodeReader.dll to the project.

    Note: The DLLs can be found in the installation folder. Typically, they are under:

    • For .NET Framework 2.0-3.5:

      [INSTALLATION FOLDER]\Components\DotNet\2.0\

    • For .NET Framework 4.0 and later:

      [INSTALLATION FOLDER]\Components\DotNet\4.0\

  3. Add namespace:

    Imports Dynamsoft.Barcode

  4. Create a new button on the form and replace the code of button1_Click() with the following.

    Please update "<your image file full path>" and "<your license key here>" accordingly.

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Dim reader As New BarcodeReader
         reader.ProductKeys = "<Put your license key here>"
         Dim aryResult As TextResult()
         Try
             aryResult = reader.DecodeFile("<your image file full path>", "") 'Leave the template name empty ("") will use the settings from PublicRuntimeSettings
             If aryResult.length = 0 Then
                 MessageBox.Show("No barcode found.")
                 Return
             End If
    
             Dim strInfo As String
             strInfo = "Total barcode(s) found: " + aryResult.Length.ToString() + vbCrLf
             For i = 0 To aryResult.Length - 1
                 strInfo += "Barcode: " + (i + 1).ToString() + ":" + vbCrLf
                 strInfo += "BarcodeFormat: " + aryResult(i).BarcodeFormat.ToString() + vbCrLf
                 strInfo += "BarcodeText: " + aryResult(i).BarcodeText.ToString() + vbCrLf + vbCrLf
             Next
             MessageBox.Show(strInfo)
         Catch ex As BarcodeReaderException
             MessageBox.Show("Error Code:" + ex.Code.ToString() + vbCrLf + "Error String: " + ex.Message)
         End Try
     End Sub
    
  5. Run the project.

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

Create a Java app for barcode reading

To build a Java application that reads barcodes from an image, you can follow the steps below:

  1. Open Eclipse and create a new Java project HelloDBR.

  2. Add the required JAR file to your project.

    Click File > Properties > Java Build Path > Libraries > Add external JARs, add DynamsoftBarcodeReader_{edition}.jar and click Apply.

    The JAR file can be found at [INSTALLATION FOLDER]\Components\Java\.

  3. Import the header.

     import com.dynamsoft.barcode.*;
    
  4. Replace the code of HelloDBR with the following.

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

     public class HelloDBR {
         public static void main(String[] args) {
             try {
                 BarcodeReader dbr = new BarcodeReader();
                 dbr.initLicense("<Put your license key here>");
                 // Leave the template name empty ("") will use the settings from PublicRuntimeSettings
                 TextResult[] result = dbr.decodeFile("<your image file full path>", ""); 
                 String output = "";
                 for(int i =0; i<result.length;i++){
                     output += "Barcode Text: ";
                     output += result[i].barcodeText + "\n\n";                  
                 }
                 System.out.println(output);
             }
             catch(Exception ex) {
                 System.out.println(ex);
             }
         }
     }
    
  5. Run the project.

Create a Python app for barcode reading

You can easily create a Python extension with the C/C++ API of Dynamsoft Barcode Reader.

For more information on how to create a Python extension for barcode reading, please refer to DBR Python Extension .

Create a PHP app for barcode reading

Dynamsoft Barcode Reader provides a PHP wrapper for Windows. With it, you can easily implement barcode reading in your PHP web applications.

For more information on how to create a PHP application for barcode scanning, Please refer to Use Dynamsoft Barcode Reader to Build a Barcode Scanner in PHP on Windows and Barcode Reading Samples for PHP.

Before you run the PHP sample, please make sure that you have Visual C++ 2008 Redistributable Package (x86)/(x64) installed on your computer.

If the package is not installed, you will get the following error message:

Failed to create COM object 'DynamsoftBarcodeReaderCtrl.BarcodeReader': The application has failed to start because its side-by-side configuration is incorrect.

To resolve this, please install it on your machine and try again.

When you run the register.bat, please make sure you execute it with Administrator privilege, or you will get the following error message:

Failed to create COM object 'DynamsoftBarcodeReaderCtrl.BarcodeReader': Invalid syntax Your barcode reader component is not registered correctly, please refer to ReadMe.txt for details.

results matching ""

    No results matching ""