Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store image into pdf417 barcode?

Is it possible to store(Encode) image/Pictures into pdf417 barcode? if so is there any tutorial or sample code?

The barcode cannot just hold a reference to an image in a database. The customer also expect to be able to store any image he wants.

Thank you.

like image 796
ashesh16 Avatar asked Mar 31 '14 17:03

ashesh16


1 Answers

As ssasa mentionned you could store the image as a byte array:

public static byte[] GetBytes(Image image)
{
    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        // you may want to choose another image format than PNG
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}

... or, if it MUST be a string, you could base64 encode it:

public static string GetBase64(Image image)
{
    Image yourImage;

    // using the function from the first example
    var imageBytes = GetBytes(yourImage);   

    var encodedString = Convert.ToBase64String(imageBytes);

    return Encoding.UTF8.GetBytes(encodedString);
}

Remember, though: a PDF417 barcode allows storing up to 2710 characters. While this is more than enough for most structures you'd ever want to encode, it's rather limitating for an image. It may be enough for small-sized bitmaps, monochrome images and/or highly compressed JPEGs, but don't expect being able to do much more than that, especially if you want to be able to store other data along.

If your customers expects to be able to store, as you say, any picture they want, you'd better be lowering their expectations as soon as possible before writing any code.

If it's an option, you may want to consider using QR Codes instead. Not that you'll work miracles with those either but you may like the added storage capacity.

like image 144
Crono Avatar answered Sep 20 '22 16:09

Crono