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.
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
Supported barcodes are:
- Code 39 (c39)
- Code 128a (c128a)
- Code 128b (c128b)
- Code 128c (c128c)
- 4 of 5 Interleaved (i2of5)
The URL format for a barcode is (QR has a slightly different URL):
To build your URL:
- Replace [type] with the necessary type (c39, c128a, c128b, c128c, i2of5).
- Replace [value] with the value of your requested barcode.
- 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.
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.
This is combined with a list page and a card page to display and enter the data.
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:
<br /> GenerateBarCode()<br /> IF Type = Type::" " THEN BEGIN<br /> CLEAR(Image);<br /> EXIT;<br /> END;</p> <p>Window.OPEN('Generating Bar Code');</p> <p>IF Type = Type::qr THEN<br /> Method := STRSUBSTNO('barcode/qr/qr.%1?value=%2&size=%3&ecclevel=%4',<br /> GetOptionStringValue("Image Type",FIELDNO("Image Type")),<br /> WebUtility.UrlEncode(Value),<br /> GetOptionStringValue(Size,FIELDNO(Size)),<br /> GetOptionStringValue("ECC Level",FIELDNO("ECC Level")))<br /> ELSE<br /> Method := STRSUBSTNO('barcode/%1/%2.%3?istextdrawn=%4&isborderdrawn=%5&isreversecolor=%6',<br /> GetOptionStringValue(Type,FIELDNO(Type)),<br /> WebUtility.UrlEncode(Value),<br /> GetOptionStringValue("Image Type",FIELDNO("Image Type")),<br /> FORMAT("Include Text",0,2),<br /> FORMAT(Border,0,2),<br /> FORMAT("Reverse Colors",0,2));</p> <p>RESTWSManagement.CallRESTWebService('http://barcodes4.me/',<br /> Method,<br /> 'GET',<br /> null,<br /> HttpResponseMessage);</p> <p>ImageStream := HttpResponseMessage.Content.ReadAsStreamAsync.Result;</p> <p>CLEAR(Image);<br /> Image.CREATEOUTSTREAM(OutStr);<br /> ImageStream.WriteTo(OutStr);</p> <p>Window.CLOSE();</p> <p>LOCAL GetOptionStringValue(option : Integer;fieldno : Integer) : Text<br /> RecRef.GETTABLE(Rec);<br /> FldRef := RecRef.FIELD(fieldno);<br /> OptionString := FldRef.OPTIONSTRING;</p> <p>separator := ',';<br /> options := OptionString.Split(separator.ToCharArray());<br /> EXIT(options.GetValue(option));<br />
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.
That’s it! Next example will be about sending SMS messages from Dynamics NAV.