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:

Code snippets are provided in C/C++/C# as examples below. Not all supported programming languages are covered in this guide. You can find more samples in more programming languages at Code Gallery.

DecodeFile

Reads barcodes from a static image file.

C
C++
C#
Java
void *hBarcode = NULL;
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>");
//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);
char sError[512];
TextResultArray* paryResult = NULL;
CBarcodeReader* reader = new CBarcodeReader();
//Initialize license prior to any decoding
//Replace "<Put your license key here>" with your own license
reader->InitLicense("<Put your license key here>");
//Replace "Put the path of your file here" with your own file path
reader->DecodeFile("<Put your file path here>", "");
// If succeeds
reader->GetAllTextResults(&paryResult);
CBarcodeReader::FreeTextResults(&paryResult);
delete reader;
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(@"Put your file path here", "");
dbr.Dispose();
BarcodeReader dbr = new BarcodeReader();

// Initialize license prior to any decoding
//Replace "<Put your license key here>" with your own license
dbr.initLicense("<Put your license key here>");

//Replace "Put the path of your file here" with your own file path
TextResult[] result = dbr.decodeFile("<Put your file path here>","");

DecodeBase64String

Reads barcodes from the base64 encoded string of a file.

C
C++
C#
void* hBarcode = DBR_CreateInstance();
int iRet = -1;
int iIndex = 0;
int iLicMsg = -1;
unsigned char* pBufferBytes;
int nFileSize = 0;
char* strBase64String;
TextResultArray* pResult = NULL;
   
// Replace "Put your license key here" with your own trial 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
GetFileStream("Put the path of your file here", &pBufferBytes, &nFileSize);
GetFileBase64String(pBufferBytes, &strBase64String);
DBR_DecodeBase64String(hBarcode, strBase64String, "");
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;
int main()
{
   int iRet = -1;
   int iLicMsg = -1;
   char* strBase64String;
   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->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, CBarcodeReader::GetErrorString(iLicMsg));
      return iLicMsg;
   }
   // Replace "Put the path of your file here" with your own file path
   GetFileStream("Put the path of your file here ", &pBufferBytes, &nFileSize);
   GetFileBase64String(pBufferBytes, &strBase64String);
   iRet = reader->DecodeBase64String(strBase64String, "");
   // If error occurs
   if (iRet != DBR_OK)
   {
      printf("Failed to read barcode: %d\r\n%s\r\n", iRet, CBarcodeReader::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;
   getchar();
   return 0;
}
// Convert the bitmap content to a base64 string
Bitmap barCodeBitmap = new Bitmap(@"C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.1\Images\AllSupportedBarcodeTypes.tif");
MemoryStream ms = new MemoryStream();
barCodeBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bmpBytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(bmpBytes, 0, (int)ms.Length);
ms.Close();
string barcodeBase64String = Convert.ToBase64String(bmpBytes);
// Decode the base64 string barcode
TextResult[] aryResult = barcodeReader.DecodeBase64String(barcodeBase64String, string.Empty);
...
barcodeReader.Dispose(); // Release unmanaged resources

DecodeBitmap and DecodeDIB

DecodeDIB() decodes barcodes from a handle of device-independent bitmap (DIB).

DecodeBitmap() reads barcodes from a bitmap. It will only decode the current page when handling multi-page images.

C
C++
C#
void* hBarcode = DBR_CreateInstance();
HANDLE pDIB;
int iRet = -1;
int iIndex = 0;
int iLicMsg = -1;
TextResultArray* pResult = NULL;
// 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
GetDIBFromImage("Put the path of your file here ", &pDIB);
DBR_DecodeDIB(hBarcode, pDIB, "");
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;
int main()
{
   int iRet = -1;
   int iLicMsg = -1;
   HANDLE pDIB;
   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->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, CBarcodeReader::GetErrorString(iLicMsg));
      return iLicMsg;
   }
   // Replace "Put the path of your file here" with your own file path
   GetDIBFromImage("Put the path of your file here ", &pDIB);
   iRet = reader->DecodeDIB(pDIB, "");
   // If error occurs
   if (iRet != DBR_OK)
   {
      printf("Failed to read barcode: %d\r\n%s\r\n", iRet, CBarcodeReader::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;
   getchar();
   return 0;
}
// Create a bitmap from a file
Bitmap barCodeBitmap = new Bitmap(@"C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.1\Images\AllSupportedBarcodeTypes.tif");
// Decode the bitmap barcode
TextResult[] aryResult = barcodeReader.DecodeBitmap(barCodeBitmap, string.Empty);
...
barcodeReader.Dispose(); // Release unmanaged resources

DecodeBuffer

Reads barcodes from raw buffer.

C
C++
C#
void* hBarcode = DBR_CreateInstance();
int iRet = -1;
int iIndex = 0;
int iLicMsg = -1;
unsigned char* pBufferBytes;
int iWidth = 0;
int iHeight = 0;
int iStride = 0;
ImagePixelFormat format;
TextResultArray* pResult = NULL;
// 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
GetBufferFromFile("Put the path of your file here ", &pBufferBytes, &iWidth, &iHeight, &iStride, &format);
DBR_DecodeBuffer(hBarcode, pBufferBytes, iWidth, iHeight, iStride, format, "");
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;
int main()
{
   int iRet = -1;
   int iLicMsg = -1;
   int iWidth = 0;
   int iHeight = 0;
   int iStride = 0;
   unsigned char* pBufferBytes;
   ImagePixelFormat format;
   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->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, CBarcodeReader::GetErrorString(iLicMsg));
      return iLicMsg;
   }
   // Replace "Put the path of your file here" with your own file path
   GetBufferFromFile("Put the path of your file here ", &pBufferBytes, &iWidth, &iHeight, &iStride, &format);
   iRet = reader->DecodeBuffer(pBufferBytes, iWidth, iHeight, iStride, format, "");
   if (iRet != DBR_OK)
   {
      printf("Failed to read barcode: %d\r\n%s\r\n", iRet, CBarcodeReader::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;
   getchar();
   return 0;
}
// Create the image raw buffer from the barcode file
Bitmap barCodeBitmap = new Bitmap(strBarcodeFilePath);
EnumImagePixelFormat emImagePixelFormat = EnumImagePixelFormat.IPF_RGB_888;
switch (barCodeBitmap.PixelFormat)
{
   case PixelFormat.Format32bppArgb:
   case PixelFormat.Format32bppPArgb:
   case PixelFormat.Format32bppRgb:
      emImagePixelFormat = EnumImagePixelFormat.IPF_ARGB_8888;
      break;
   case PixelFormat.Format24bppRgb:
      emImagePixelFormat = EnumImagePixelFormat.IPF_RGB_888;
      break;
   case PixelFormat.Format1bppIndexed:
      emImagePixelFormat = EnumImagePixelFormat.IPF_BINARY;
      break;
   case PixelFormat.Format8bppIndexed:
      emImagePixelFormat = EnumImagePixelFormat.IPF_GRAYSCALED;
      break;
}
Rectangle rect = new Rectangle(0, 0, barCodeBitmap.Width, barCodeBitmap.Height);
BitmapData barCodeBitmapData = barCodeBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, barCodeBitmap.PixelFormat);
// Declare an array to hold the bytes of the bitmap.
int imageBytesSize = Math.Abs(barCodeBitmapData.Stride) * barCodeBitmap.Height;
byte[] barcodeImageRawBuffer = new byte[imageBytesSize];
// Get the address of the first line.
IntPtr ptr = barCodeBitmapData.Scan0;
// Copy the RGB values into the byte array.
System.Runtime.InteropServices.Marshal.Copy(ptr, barcodeImageRawBuffer, 0, imageBytesSize);               
barCodeBitmap.UnlockBits(barCodeBitmapData);                   

int iBarcodeImageWidth = barCodeBitmap.Width;
int iBarcodeImageHeight = barCodeBitmap.Height;
int iBarcodeImageStride = barCodeBitmapData.Stride;
// Read barcodes from raw buffer
TextResult[] aryResult = barcodeReader.DecodeBuffer(barcodeImageRawBuffer, iBarcodeImageWidth, iBarcodeImageHeight, iBarcodeImageStride, emImagePixelFormat, string.Empty);
...
barcodeReader.Dispose(); // Release unmanaged resources

DecodeFileInMemory

Decodes barcodes from an image file in memory.

C
C++
C#
void* hBarcode = DBR_CreateInstance();
int iRet = -1;
int iIndex = 0;
int iLicMsg = -1;
unsigned char* pFileBytes;
int nFileSize = 0;
TextResultArray* pResult = NULL;
// 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
GetFileStream("Put the path of your file here ", &pFileBytes, &nFileSize);
DBR_DecodeFileInMemory(hBarcode, pFileBytes, nFileSize, "");

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;
int main()
{
   int iRet = -1;
   int iLicMsg = -1;
   int nFileSize = 0;
   unsigned char* pFileBytes;
   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 ");
   // Replace "Put the path of your file here" with your own file path
   GetFileStream("Put the path of your file here ", &pFileBytes, &nFileSize);
   iRet = reader->DecodeFileInMemory(pFileBytes, nFileSize, "");
   // If error occurs
   if (iRet != DBR_OK)
   {
      printf("Failed to read barcode: %d\r\n%s\r\n", iRet, CBarcodeReader::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;
   getchar();
   return 0;
}
// Read the file content into byte array
FileStream fileStream = new FileStream(@"C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.1\Images\AllSupportedBarcodeTypes.tif", FileMode.Open, FileAccess.Read);
byte[] barcodeFileStream = new byte[fileStream.Length];
fileStream.Read(barcodeFileStream, 0, barcodeFileStream.Length);
fileStream.Close();
// Decode barcode from the barcode file in byte array.
TextResult[] aryResult = barcodeReader.DecodeFileInMemory(barcodeFileStream, string.Empty);
...
barcodeReader.Dispose(); // Release unmanaged resources

results matching ""

    No results matching ""