Read barcodes from an image if image data is stored in a byte array.

Code samples for VintaSoft Barcode .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 1443
Joined: Thu Jul 10, 2008 2:21 pm

Read barcodes from an image if image data is stored in a byte array.

Post by Alex » Tue Aug 09, 2016 9:45 am

Here is an example that shows how to read barcodes from image if image data are stored in a byte array:

Code: Select all

/// <summary>
/// Reads barcodes from an image. Image data are stored in a byte array.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="stride">The stride.</param>
/// <param name="format">The format.</param>
/// <param name="pixelData">The pixel data.</param>
public static IBarcodeInfo[] ReadBarcodes(
    BarcodeReader reader, 
    int width, 
    int height, 
    int stride, 
    PixelFormat format, 
    byte[] pixelData)
{
    // lock managed data
    GCHandle bitmapDataHandle = GCHandle.Alloc(pixelData, GCHandleType.Pinned);
    if (!bitmapDataHandle.IsAllocated)
        throw new InvalidOperationException();

    // get IntPtr to managed data
    IntPtr pixelDataPtr = Marshal.UnsafeAddrOfPinnedArrayElement(pixelData, 0);

    try
    {
        // read barcodes 
        return ReadBarcodes(reader, width, height, stride, format, pixelDataPtr);
    }
    finally
    {
        // free GCHandle
        bitmapDataHandle.Free();
    }
}

/// <summary>
/// Reads barcodes from an image. Image data are stored in an unmanaged memory.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="stride">The stride.</param>
/// <param name="format">The format.</param>
/// <param name="pixelDataScan0">The Scan0 pointer to pixel data.</param>
public static IBarcodeInfo[] ReadBarcodes(BarcodeReader reader, int width, int height, int stride, PixelFormat format, IntPtr pixelDataScan0)
{
    using (Bitmap bitmap = new Bitmap(width, height, stride, format, pixelDataScan0))
    {
        // read barcodes 
        return reader.ReadBarcodes(bitmap);
    }
}

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests