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 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# 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:

    //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

Reads barcodes from a bitmap. It will only decode the current page when handling multi-page images.

Example:

    //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:

    //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:

    // 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 ""