6

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.

Crono
  • 10,211
  • 6
  • 43
  • 75
ashesh16
  • 73
  • 1
  • 3
  • 2
    PDF417 allows storing 2710 chars so I think it could be done for relatively small images. I have to ask though: why would you want to do that? – Crono Mar 31 '14 at 17:09
  • One of my customer wants to add a picture into barcode, so when they scan most probably they want to see the picture. – ashesh16 Mar 31 '14 at 17:13
  • 2
    Then the images should be stored in a database and the barcode should hold a structure with every pertinent information PLUS a reference (id) to the associated image. Storing the image itself in the barcode is unefficient IMHO. – Crono Mar 31 '14 at 17:17
  • What kind of images your customer wants? Symbols, signs? Most certainly they're not expecting high-resolution bitmap files, are they? – Crono Mar 31 '14 at 17:26
  • @Crono: Early PDF-417 promotional materials included a barcode which contained a very small picture. The intention was that an ID should contain both a high-quality physically-printed mugshot and a (much) lower-quality one, along with a PDF-417-encoded, digitally-signed, scan of the latter. Although high-speed Internet access is ubiquitous today, that was not always true; including the picture within the scan would avoid the need for a "live" network connection. – supercat Mar 31 '14 at 17:29
  • why not make the barcode encode a url to the image, when you decode the barcode, you can then view the image. – Peter Ritchie Mar 31 '14 at 18:03
  • @Crono, I fought for storing image in database and getting the reference from barcode.. but they want to save those in barcode. – ashesh16 Mar 31 '14 at 18:09
  • @user3482061 Do you have *any* idea about the kind of images they expect to be stored? – Crono Mar 31 '14 at 18:27
  • at this point they can select any picture they want. – ashesh16 Mar 31 '14 at 18:42
  • Yes, it is possible to encode images in PDF417 barcode. Some driver licenses with barcode have this. But you should expect low resolution of the image as barcode is not able to contain much data. – ssasa Apr 01 '14 at 06:45

2 Answers2

5

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.

Crono
  • 10,211
  • 6
  • 43
  • 75
  • It is not necessary to base64 encode the image as PDF417 can contain binary data. – ssasa Apr 01 '14 at 06:48
  • @ssasa likely yes, but I figured that if the OP was asking the question that could have been because he was using a 3rd party API which only produce barcodes out of strings. In any case, you do have a point. I edited my answer. Thanks for your observation. – Crono Apr 01 '14 at 12:25
  • @Corono if i do like what you said above then the customer has to decode the information after they get from barcode right – ashesh16 Apr 01 '14 at 13:55
  • @user3482061 I'm afraid I don't quite understand what you're asking here. Barcodes are supposed to hold on to encoded information for a scanning device to decode. That device must in turn pass the decoded information to a software component that will know what to do with it. Anything beyond that scope is up to you, really. – Crono Apr 01 '14 at 14:43
  • @user3482061 you are welcome. If you feel what I've said has been helpful enough, please mark this as answer. Thanks. – Crono Apr 01 '14 at 15:03
0

Yes, Department of Defense Common Access Cards (CAC) store JPEG image of the cardholder:

enter image description here

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219