Example Code

cvi EAN8/13 barcode generator

Code and Documents

Attachment

Download All

Overview

Creation of bitmaps containing barcodes according to EAN standard.

Description

The file 'barcode.c' (written in C under LabWindows cvi) enables the generation of barcodes according to EAN convention as described at http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29 (English) or, better, at http://fr.wikipedia.org/wiki/Code-barres_EAN (in French). Codes can be either in 8-digit convention or in 13-digit one. In the first case it must be coded as an unsigned integer (unsigned int), in the second case, as a 64-bit unsigned (unsigned long long unsigned). The resulting barcodes are generated as bitmaps. They have to be released after use by calling the function DiscardBitmap().

The generated barcodes have been checked on some on-line generator (for instance http://barcode-coder.com/fr/generateur-de-code-barres-en-ligne-2.html).

Steps to Implement or Execute Code

4 functions are provided in the source file 'barcode.c' acting as a 'library' (see their prototypes below):

  1. extern int EAN8PutBitmap(short unitWidth, unsigned int n8);
  2. extern int EAN8WithKeyPutBitmap(short unitWidth, unsigned int n7);
  3. extern int EAN13PutBitmap(short unitWidth, unsigned __int64 n13);
  4. extern int EAN13WithKeyPutBitmap(short unitWidth, unsigned __int64 n12);

The functions include two parameters:

  1. unitWidth is the width of each 'module' (or strip). Each digit is coded on 7 modules. The width is expressed in pixel. The dimensions of the bitmap are ajusted accordingly with this parameter.
  2. the number to be 'barcoded' is either an unsigned int (n7 and n8 for 8-digit codes) either an unsigned long long int (n12 and n13 for 13-digit codes).

The variants '...WithKey...' generate themselves the control key normally required by the standard. When using these variants the argument given to the function must include one digit less (7 against 8 and 12 against 13 respectively) than usually so that (a) the given code is shifted on the left (x10) and (b) the least significant digit (rank zero or rightmost) is replaced by the control key.

The code ean8.c gives a simple example of application.

Requirements

Software

     LabWindows CVI supporting long long int (__int64).

Hardware

     None

Additional Images or Video

barcode.bmp     barcode.bmp

Typical EAN13 and EAN8 codebars generated by the code.         


Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
erickringot
Member
Member
on

1° NOTE : the source "g_barcode.c" has been updated for correcting a mismatched value in the codea table (digit 7).

2° For adding an additionnal two-digit value to generate a EAN13+2 code, the code is added on the right side of the barcode according to the following schema :

          <4-segment delimiter = 1011><MSB digit><2-segment delimiter = 01><LSB digit> (where add_value=10 x MSB + LSB)

The digits must be encoded with either tablea or tableb according to the rest of the additional value in the division by 4 :

add_value MOD 4 =0, MSB tablea, LSB tablea

add_value MOD 4 =1, MSB tablea, LSB tableb

add_value MOD 4 =2, MSB tableb, LSB tablea

add_value MOD 4 =3, MSB tableb, LSB tableb

(see the GS1 document http://www.gs1.org/docs/gsmp/barcodes/GS1_General_Specifications.pdf), section 5 "data carriers" starting page 221.

andyadams
Member
Member
on

Thanks for your sharing of this EAN8/13 barcode generator.I am doing my work with barcode generation,i am just looking for this one.

micagordon
Member
Member
on

Thanks, I love this sharing.I need to print bar code label with EAN 13, I don't know wether this one can creat EAN 13 in excel?

erickringot
Member
Member
on

Hi micagordon,

I suggest :

  1. Create an image from the bitmap by a call to SaveBitmapToPNGFile(),
  2. Link your code with the cvi instrument excelreportfp,
  3. Use the function ExcelRpt_InsertPicture().

See http://forums.ni.com/t5/LabWindows-CVI/Insert-a-picture-into-excel-file-and-set-the-picture-as/td-p/....

Contributors