Web Services Examples Part 3 – Generate Barcode

9 Jan

What a busy december month it has been! That last month of 2015 ended before I realized it. I wish everybody a very happy and prosperous 2016!

The last post in the web services series was about one month ago, so it is about time to post the next one. This time we will look at generating barcodes.

The challenge

One might argue “why do we need a web service to create a barcode?”. And this is a good point, as we can print barcodes on reports fairly easy. Here and here are some examples. However, these solutions require you to download and install a font or to download an external library. Not a big deal of course and servers might already have the correct font installed.

Things become a little more complex if you look for a solution that does not require any additional installation. Or you maybe want to display a barcode an a screen or store it in a database as an image so it can be retrieved with a NAV web service. Well, here a web service for barcodes does a very good job.

The web service

At http://www.barcodes4.me/ you can create a barcode very easy. This web site provides a bar code as a downloadable image. This image can be found by composing a hyperlink: http://www.barcodes4.me/barcode/c39/HAPPYNEWYEAR.png?width=250&height=100&istextdrawn=1

HAPPYNEWYEAR

Supported barcodes are:

  • Code 39 (c39)
  • Code 128a (c128a)
  • Code 128b (c128b)
  • Code 128c (c128c)
  • 4 of 5 Interleaved (i2of5)
  • QR

The URL format for a barcode is (QR has a slightly different URL):

http://barcodes4.me/barcode/[type]/[value].[imagetype]

To build your URL:

  1. Replace [type] with the necessary type (c39, c128a, c128b, c128c, i2of5).
  2. Replace [value] with the value of your requested barcode.
  3. Replace [imagetype] with the type of image you would like created (png, gif, jpg). Use htm for more information about the barcode.

For more information look at this very clear explanation: http://www.barcodes4.me/apidocumentation

This response from this web service is an image. Instead of a JSON or XML response, it provides you with the image data. In other words, the content-type in the response headers is image/png.

The code

For this example I have redesigned the original demo code from NAV Techdays so I could demonstrate all available options.

First I have create a table that can hold all types of barcodes.

image

This is combined with a list page and a card page to display and enter the data.

image

 

image

image

I assume that you are not waiting for an explanation how you can bind a record in this table with a item record or any other record, am I right?

What’s probably more interesting is the code behind. Well, here it is:

GenerateBarCode()
IF Type = Type::" " THEN BEGIN
  CLEAR(Image);
  EXIT;
END;

Window.OPEN('Generating Bar Code');

IF Type = Type::qr THEN
  Method := STRSUBSTNO('barcode/qr/qr.%1?value=%2&size=%3&ecclevel=%4',
                        GetOptionStringValue("Image Type",FIELDNO("Image Type")),
                        WebUtility.UrlEncode(Value),
                        GetOptionStringValue(Size,FIELDNO(Size)),
                        GetOptionStringValue("ECC Level",FIELDNO("ECC Level")))
ELSE
  Method := STRSUBSTNO('barcode/%1/%2.%3?istextdrawn=%4&isborderdrawn=%5&isreversecolor=%6',
                       GetOptionStringValue(Type,FIELDNO(Type)),
                       WebUtility.UrlEncode(Value),
                       GetOptionStringValue("Image Type",FIELDNO("Image Type")),
                       FORMAT("Include Text",0,2),
                       FORMAT(Border,0,2),
                       FORMAT("Reverse Colors",0,2));

RESTWSManagement.CallRESTWebService('http://barcodes4.me/',
                                    Method,
                                    'GET',
                                    null,
                                    HttpResponseMessage);

ImageStream := HttpResponseMessage.Content.ReadAsStreamAsync.Result;

CLEAR(Image);
Image.CREATEOUTSTREAM(OutStr);
ImageStream.WriteTo(OutStr);

Window.CLOSE();

LOCAL GetOptionStringValue(option : Integer;fieldno : Integer) : Text
RecRef.GETTABLE(Rec);
FldRef := RecRef.FIELD(fieldno);
OptionString := FldRef.OPTIONSTRING;

separator := ',';
options := OptionString.Split(separator.ToCharArray());
EXIT(options.GetValue(option));

The response

What is different from the previous examples is that the response contains an image. That is binary data, so it doesn’t make sense to get the data as text. (Only when you deliberately want to save a base64 representation of the image.)

So instead of using ReadAsStringAsync on the response content, we use ReadAsStreamAsync and stream the result into a .Net MemoryStream. This stream is then saved into a BLOB file in the record. The BLOB field has as subtype Bitmap, so Dynamics NAV knows how to display it.

Download the complete code of this web service example here

That’s it! Next example will be about sending SMS messages from Dynamics NAV.

One thought on “Web Services Examples Part 3 – Generate Barcode

  1. Pingback: How Do I: Print Barcodes in RDLC? – think about IT

Leave a Reply

Your email address will not be published.