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.

9 thoughts on “Web Services Examples Part 3 – Generate Barcode

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

  2. hi, you solution is so interresting for me, thx for this posting ,
    just when i need to print the barcode , whats the best solution to do it from the page 60004

    • It’s an image, so you can put it on a report.
      In case you use a label printer, you could consider to use specific barcode instruction. Most label printer support that.

  3. Hello there. You solution seems to work great when using the Development environment. My requirement is to use extensions. When I create the table via AL code, I get an error that the DotNet functions are not supported in al for the variables that are defined as DotNet in the GetOptionStringValue function. Any ideas how to get your code to work with Extensions ??

    Thanks

  4. Please disregard my comment. I clicked on the wrong link which took me to the table and code . I should have clicked on the github link.

  5. Hi,

    We are generating the barcode printing and scanning activity.

    In order to print/generate the barcode, we are following your steps in the blog.

    By this method, for 1D Barcodes, able to generate any number of characters but scanning the barcode using a barcode scanner works till 8 characters only (Only 1D is facing issue. QR code is working fine.)

    Anything beyond 8 character, is not scannable. The Barcode scanner is able to scan any other Retail product’s Barcode which is more than 8 characters also. Only the Barcode generated through system is not working.

    Please assist to know if there is any restriction on scanning above 8 characters.

    • I don’t think I do have an answer for you. If barcodes generated with the webservice are not readable beyond 8 characters, then you might want to look at another service.

  6. Hello Arend-Jan,

    On the Barcode4.me site, they say for commercial use we need to provide the attribution link to their site.
    How do you get around this requirement?
    Do you use barcodes only for internal documents?

    Best,
    Kateryna

    • Well, I would say that a link to his page can be displayed on a page called Barcodse4me setup.
      Please keep in mind that his service is free and does not provide any warranty or guarantee that it will be up and running.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.