Thanks for downloading Dynamsoft Barcode Reader Package!
Your download will start shortly. If your download does not begin, click here to retry.
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.
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.
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 and compiled program from Here.
Open Visual Studio. Go to File > New > Project, create a new Empty Project and set Project name as DBRCSample
.
Add a new source file named DBRCSample.c
into the project.
DBRCSample.c
and place it into the folder [INSTALLATION FOLDER]/Samples
.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
Create an instance of Dynamsoft Barcode Reader.
void *hBarcode = DBR_CreateInstance();
Initialize the license key.
DBR_InitLicense(hBarcode, "<insert DBR license key here>");
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.
Set barcode format and count to read.
char sError[512];
PublicRuntimeSettings runtimeSettings;
DBR_GetRuntimeSettings(hBarcode, &runtimeSettings);
runtimeSettings.barcodeFormatIds = BF_ALL;
runtimeSettings.barcodeFormatIds_2 = BF2_POSTALCODE | BF2_DOTCODE;
runtimeSettings.expectedBarcodesCount = 32;
DBR_UpdateRuntimeSettings(hBarcode, &runtimeSettings, sError, 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.
Decode barcodes from an image file.
int iErrorCode = -1;
iErrorCode = DBR_DecodeFile(hBarcode, "[INSTALLATION FOLDER]/Images/AllSupportedBarcodeTypes.png", "");
if(iErrorCode != DBR_OK)
printf("%s\n", DBR_GetErrorString(iErrorCode));
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.
Get and output barcode results.
TextResultArray* pResult = NULL;
DBR_GetAllTextResults(hBarcode, &pResult);
if (pResult != NULL && pResult->resultsCount > 0)
{
printf("%d total barcode(s) found. \n", pResult->resultsCount);
for (int iIndex = 0; iIndex < pResult->resultsCount; iIndex++)
{
printf("Result %d\n", iIndex + 1);
printf("Barcode Format: %s\n", pResult->results[iIndex] ->barcodeFormatString);
printf("Barcode Text: %s \n", pResult->results[iIndex] ->barcodeText);
}
}
getchar();
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 the allocated memory for the barcode results and instance.
if(pResult != NULL)
DBR_FreeTextResults(&pResult);
DBR_DestroyInstance(hBarcode);
Note:
Please change all[INSTALLATION FOLDER]
in above code snippet to your unpacking path.
In Visual Studio, set the solution to build as Release|x64.
Build the project to generate program DBRCSample.exe
.
Copy ALL *.dll
files under [INSTALLATION FOLDER]\Lib\Windows\x64
to the same folder as the DBRCSample.exe
.
Run the program DBRCSample.exe
.
The SDK supports both x86 and x64, please set the platform based on your needs.
Create a file named Makefile
with following content and put it in the same directory as the file DBRCSample.c
.
CC=gcc
CCFLAGS=-c
DBR_LIB_PATH=../Lib/Linux
DBR_INCLUDE_PATH=../Include
LDFLAGS=-lDynamsoftBarcodeReader -L $(DBR_LIB_PATH) -Wl,-rpath=$(DBR_LIB_PATH) -Wl,-rpath=./
TARGET=DBRCSample
OBJECT=DBRCSample.o
SOURCE=DBRCSample.c
# build rule for target.
$(TARGET): $(OBJECT)
$(CC) -o $(TARGET) $(OBJECT) $(LDFLAGS)
# target to build an object file
$(OBJECT): $(SOURCE)
$(CC) $(CCFLAGS) -I $(DBR_INCLUDE_PATH) $(SOURCE)
# the clean target
.PHONY : clean
clean:
rm -f $(OBJECT) $(TARGET)
Open a terminal and change to the target directory where Makefile
located in. Build the sample:
make
Run the program DBRCSample
.
./DBRCSample
You can download the entire source code and compiled program from Here.
latest version