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();
    //Replace “Put your license key here” with your own license
    DBR_InitLicense(hBarcode, "Put your license key here");
    unsigned char* pBufferBytes;
    int nFileSize = 0;
    char* strBase64String;
    //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);
    int errorCode = DBR_DecodeBase64String(hBarcodeReader, strBase64String, "");
    DBR_DestroyInstance(hBarcode);
    //C++
    CBarcodeReader* reader = new CBarcodeReader();
    //Replace “Put your license key here” with your own license
    reader->InitLicense("Put your license key here ");
    unsigned char* pFileBytes;
    int nFileSize = 0;
    //Replace “Put the path of your file here” with your own file path
    GetFileStream("Put the path of your file here ", &pFileBytes, &nFileSize);
    char* strBase64String;
    GetFileBase64String(pBufferBytes, &strBase64String);
    int errorCode = reader->DecodeBase64String(strBase64String, "");
    delete reader;
    //C#
    //convert the bitmap content to a base64 string
    Bitmap barCodeBitmap = new Bitmap(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 6.4\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();
    //Replace “Put your license key here” with your own license
    DBR_InitLicense(hBarcode, " Put your license key here ");
    HANDLE pDIB;
    //Replace “Put the path of your file here” with your own file path
    GetDIBFromImage("Put the path of your file here ", &pDIB);
    int errorCode = DBR_DecodeDIB(hBarcode, pDIB, "");
    DBR_DestroyInstance(hBarcode);
    //C++
    CBarcodeReader* reader = new CBarcodeReader();
    //Replace “Put your license key here” with your own license
    reader->InitLicense("Put your license key here ");
    HANDLE pDIB;
    //Replace “Put the path of your file here” with your own file path
    GetDIBFromImage("Put the path of your file here ", &pDIB);
    int errorCode = reader->DecodeDIB(pDIB "");
    delete reader;
    //C#
    //create a bitmap from a file
    Bitmap barCodeBitmap = new Bitmap(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 6.4\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();
    //Replace “Put your license key here” with your own license
    DBR_InitLicense(hBarcode, " Put your license key here ");
    unsigned char* pBufferBytes;
    int iWidth = 0;
    int iHeight = 0;
    int iStride = 0;
    ImagePixelFormat format;
    //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);
    int errorCode = DBR_DecodeBuffer(hBarcode, pBufferBytes, iWidth, iHeight, iStride, format, "");
    DBR_DestroyInstance(hBarcode);
    //C++
    CBarcodeReader* reader = new CBarcodeReader();
    //Replace “Put your license key here” with your own license
    reader->InitLicense("Put your license key here ");
    unsigned char* pBufferBytes;
    int iWidth = 0;
    int iHeight = 0;
    int iStride = 0;
    ImagePixelFormat format;
    //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);
    int errorCode = reader->DecodeBuffer(pBufferBytes, iWidth, iHeight, iStride, format, "");
    delete reader;
    //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();
    //Replace “Put your license key here” with your own license
    DBR_InitLicense(hBarcode, " Put your license key here ");
    unsigned char* pFileBytes;
    int nFileSize = 0;
    //Replace “Put the path of your file here” with your own file path
    GetFileStream("Put the path of your file here ", &pFileBytes, &nFileSize);
    int errorCode = DBR_DecodeFileInMemory(hBarcode, pFileBytes, nFileSize, "");
    DBR_DestroyInstance(hBarcode);
    //C++
    CBarcodeReader* reader = new CBarcodeReader();
    //Replace “Put your license key here” with your own license
    reader->InitLicense("Put your license key here ");
    unsigned char* pFileBytes;
    int nFileSize = 0;
    //Replace “Put the path of your file here” with your own file path
    GetFileStream("Put the path of your file here ", &pFileBytes, &nFileSize);
    int errorCode = reader->DecodeFileInMemory(pFileBytes, nFileSize, "");
    delete reader;
    //C#
    // read the file content into byte array
    FileStream fileStream = new FileStream(@“C:\Program Files (x86)\Dynamsoft\Barcode Reader 6.4\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 ""