Decoding Methods

In addition to reading barcodes from a static image the SDK also supports reading barcodes from a video stream, file in memory, base64 string, bitmap, etc. Here is a list of all decoding methods and related code snippet/samples:

Method Description
DecodeFile Reads barcodes from a specified image file.
DecodeBase64String Reads barcodes from the base64 encoded string of a file.
DecodeBitmap and DecodeDIB Reads barcodes from a bitmap. It will only decode the current page when handling multi-page images.
DecodeBuffer Reads barcodes from raw buffer.
DecodeFileInMemory Decodes barcodes from an image file in memory.

Code snippets are provided in C/C++/C# as examples, but not all supported programming languages are covered in this guide. If you are looking for samples in other programming languages, please refer to our Code Gallery.

DecodeFile

We have talked about how to read barcodes from a specified file in the previous chapter. Please refer to here for more information on how to use the method.

DecodeBase64String

Reads barcodes from the base64 encoded string of a file.

Example:

    //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 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);
    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;
    //C++
    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, 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);
    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, 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;
     getchar();
     return 0;
    }
    //C#
    //convert the bitmap content to a base64 string
    Bitmap barCodeBitmap = new Bitmap(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\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);

DecodeBitmap and DecodeDIB

DecodeDIB decodes barcode 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.

Example:

    //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;
    //C++
    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, 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);
    iRet = reader->DecodeDIB(pDIB "");
    // 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;
     getchar();
     return 0;
    }
    //C#
    //create a bitmap from a file
    Bitmap barCodeBitmap = new Bitmap(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\Images\AllSupportedBarcodeTypes.tif”);
    //Decode the bitmap barcode
    TextResult[] aryResult = barcodeReader.DecodeBitmap(barCodeBitmap, string.Empty);

DecodeBuffer

Reads barcodes from raw buffer.

Example:

    //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;
    //C++
    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, 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);
    iRet = reader->DecodeBuffer(pBufferBytes, iWidth, iHeight, iStride, format, "");

     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;
     getchar();
     return 0;
    }
    //C#
    //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;
    //Reads barcodes from raw buffer.
    TextResult[] aryResult = barcodeReader.DecodeBuffer(barcodeImageRawBuffer, iBarcodeImageWidth, iBarcodeImageHeight, iBarcodeImageStride, emImagePixelFormat, string.Empty);

DecodeFileInMemory

Decodes barcodes from an image file in memory.

Example:

    //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;
    //C++
    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->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, 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;
     getchar();
     return 0;
    }
    //C#
    // read the file content into byte array
    FileStream fileStream = new FileStream(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 7.0\Images\AllSupportedBarcodeTypes.tif”, FileMode.Open, FileAccess.Read);
    byte[] barcodeFileStream = new byte[fileStream.Length];
    fileStream.Read(barcodeFileStream, 0, barcodeFileStream.Length);
    fileStream.Close();

    // Decodes barcode from the barcode file in byte array.
    TextResult[] aryResult = barcodeReader.DecodeFileInMemory(barcodeFileStream, string.Empty);

results matching ""

    No results matching ""