Current version is v1.0.10, released 2019-01-14.
Barcode concepts
A barcode is an image with alternating black and white bars of varying width especially designed to be easily read by a computer.
Main Parts of a barcode
A barcode consists of the following main parts.
B) The barcode consisting of alternating black and white bars of varying width.
A and C) Quiet zones on each side of the barcode, to help barcode readers see the edges of the barcode.
D) Text underneath the barcode is present in some types of barcodes. The primary role of the text is when barcode readers are unable to read the barcode. Then a person can read off and enter the text manually.
Internal parts of the barcode itself
Internally, a barcode has these common parts.
E and J) These are the start symbol and the stop symbol. Different barcode types have different start and stop symbols. Not all barcodes have them, but they are quite common.
G) Middle marker. Some barcodes have a special symbol that marks the middle of the barcode.
F and H) The main payload of the barcode. These bars encode the main data, which in this example is “306561111111”.
I) This is the checksum digit. It is computed based on the main payload. It is used to increase the probability that the read of the entire barcode was correct.
Functions in the package
The following list of functions are available in the library.
As each function is available in many different programming languages, the signatures are given in a generic way:
<return type> <function name>( <Parameter 1 type> <Parameter 1 name> ...)
Code 128
GenerateBarcodeCode128
Signature:
RGBABitmapImage GenerateBarcodeCode128(char [] chars, double height)
Parameters:
- chars – An array of Code 128 supported characters: All 127 ASCII characters, equivalent to the first 127 UTF-8 characters.
- height – Heights of the barcode in pixels.
Returns:
- RGBABitmapImage of the Code 128 barcode, with the smallest bar of the barcode being 2 pixels. The barcode will be as short as possible by utilizing the internal A, B and C-encodings of Code 128, e.g. the C-encoding allows packing two decimal digits into one barcode-symbol.
GenerateBarcodeCode128AllParams
Signature:
boolean GenerateBarcodeCode128AllParams(char [] chars, double height, double moduleWidth, RGBABitmapImage image, StringReference errorMessages)
Parameters:
- chars – An array of Code 128 supported characters: All 127 ASCII characters, equivalent to the first 127 UTF-8 characters.
- height – Heights of the barcode in pixels.
- moduleWidth – Width of the narrowest bar (the module) in pixels.
- image – RGBABitmapImage of the Code 128 barcode, with the smallest bar of the barcode being 2 pixels. The barcode will be as short as possible by utilizing the internal A, B and C-encodings of Code 128, e.g. the C-encoding allows packing two decimal digits into one barcode-symbol.
- errorMessages – If the method returns false, an error message is given in errorMessages.string.
Returns:
- boolean – true if the barcode was successfully generated, and if not, false. The reason for why the generation failed is given in errorMessages.string.
Code128Encode
Signature:
double [] Code128Encode(char [] cs)
Parameters:
- cs – An array of Code 128 supported characters.
Returns:
- The list of Code 128 codes, including the start code, checksum and stop code.
CalculateCode128Checksum
Signature:
double CalculateCode128Checksum(double [] codes)
Parameters:
- codes – An array of Code 128 supported characters, not including the checksum or stop code.
Returns:
- The Code 128 checksum.
Code 39
GenerateBarcodeCode39
Signature:
RGBABitmapImage GenerateBarcodeCode39(char [] chars, double height)
Parameters:
- chars – An array of supported characters: 0-9, A-Z,
-
,.
, space,$
,/
,+
,%
. - height – Heights of the barcode in pixels.
Returns:
- RGBABitmapImage of Code 39 barcode, with the smallest bar of the barcode being 2 pixels.
GenerateBarcodeCode39WithChecksumOption
Signature:
RGBABitmapImage GenerateBarcodeCode39WithChecksumOption(char [] chars, double height, boolean includeChecksum)
Parameters:
- Same as
GenerateBarcodeCode39
. - includeChecksum – Set to true to include a checksum. Barcode readers must be configured to support this option.
Returns:
- Same as
GenerateBarcodeCode39
.
EAN-13
GenerateBarcodeEAN13
Signature:
RGBABitmapImage GenerateBarcodeEAN13(char [] code, double widthInMm, double heightInMm, double pixelsPerMm)
Parameters:
- code – An array of 12 digits: 0-9.
- widthInMm – The width of the barcode: Valid widths are 29.832-74.58 mm corresponding to zoom-levels 80% – 200%. 100% is 37.29mm.
- heightInMm – Height of the barcode in mm, should be minimum 19. The text-part is 3.08mm at 100% (i.e. with widthInMm = 37.29mm) and scales with width.
- pixelsPerMm – Pixels per mm. To convert from DPI use e.g.
DPIToDotsPerMm(300d)
for 300 DPI. A DPI of 300-600 is recommended. A dpi below 200 might be difficult to read for barcode scanners.
Returns:
- RGBABitmapImage of the EAN-13 barcode. Use
ConvertToPNGWithOptions
to convert to PNG.
EAN-8
GenerateBarcodeEAN8
Signature:
RGBABitmapImage GenerateBarcodeEAN8(char [] code, double widthInMm, double heightInMm, double pixelsPerMm)
Parameters:
- code – An array of 7 digits: 0-9.
- widthInMm – The width of the barcode: Valid widths are 19.272-48.18 mm corresponding to zoom-levels 80% – 200%. 100% is 24.09mm.
- heightInMm – Height of the barcode in mm, should be minimum 12.5. The text-part is 3.08mm at 100% (i.e. with widthInMm = 24.09mm) and scales with width.
- pixelsPerMm – Pixels per mm. To convert from DPI use e.g.
DPIToDotsPerMm(300d)
for 300 DPI. A DPI of 300-600 is recommended. A dpi below 200 might be difficult to read for barcode scanners.
Returns:
- RGBABitmapImage of the EAN-8 barcode. Use
ConvertToPNGWithOptions
to convert to PNG.
UPC-A
GenerateBarcodeUPCA
Signature:
RGBABitmapImage GenerateBarcodeUPCA(char [] code, double widthInMm, double heightInMm, double pixelsPerMm)
Parameters:
- code – An array of 11 digits: 0-9.
- widthInMm – The width of the barcode: Valid widths are 19.272-48.18 mm corresponding to zoom-levels 80% – 200%. 100% is 32.67mm.
- heightInMm – Height of the barcode in mm, should be minimum 12.5. The text-part is 3.08mm at 100% (i.e. with widthInMm = 32.67mm) and scales with width.
- pixelsPerMm – Pixels per mm. To convert from DPI use e.g.
DPIToDotsPerMm(300d)
for 300 DPI. A DPI of 300-600 is recommended. A dpi below 200 might be difficult to read for barcode scanners.
Returns:
- RGBABitmapImage of UPC-A barcode. Use
ConvertToPNGWithOptions
to convert to PNG.
UPC-E
GenerateBarcodeUPCE
Signature:
RGBABitmapImage GenerateBarcodeUPCE(char [] e, double widthInMm, double heightInMm, double pixelsPerMm
Parameters:
- e – An array of 7 digits: 0-9.
- widthInMm – The width of the barcode: Valid widths are 17.424-43.56 mm corresponding to zoom-levels 80% – 200%. 100% is 19.27mm.
- heightInMm – Height of the barcode in mm, should be minimum 23mm. The text-part is 3.08mm at 100% (i.e. with widthInMm = 19.27mm) and scales with width.
- pixelsPerMm – Pixels per mm. To convert from DPI use e.g.
DPIToDotsPerMm(300d)
for 300 DPI. A DPI of 300-600 is recommended. A dpi below 200 might be difficult to read for barcode scanners.
Returns:
- RGBABitmapImage of UPC-E barcode. Use
ConvertToPNGWithOptions
to convert to PNG.
UPCAToUPCE
Signature:
char [] UPCAToUPCE(char [] a)
Parameters:
- a – An UPC-A code without checksum.
Returns:
- char [] – The UPC-E version of the barcode.
UPCEToUPCA
Signature:
char [] UPCEToUPCA(char [] e)
Parameters:
- e – An UPC-E code without checksum.
Returns:
- char [] – The UPC-A version of the barcode.
Standard 2 out of 5
GenerateBarcodeStandard2of5
Signature:
RGBABitmapImage GenerateBarcodeStandard2of5(char [] chars, double height)
Parameters:
- chars – An array of supported characters: 0-9.
- height – The height of the barcode in pixels.
Returns:
- RGBABitmapImage of Standard 2 out of 5 barcode, with the narrowest bar of the barcode being 2 pixels.
GenerateBarcodeStandard2of5WithChecksumOption
Signature:
RGBABitmapImage GenerateBarcodeStandard2of5WithChecksumOption(char [] chars, double height, boolean withChecksum)
Parameters:
- chars – An array of supported characters: 0-9.
- height – The height of the barcode in pixels.
- withChecksum – Generates and adds an EAN-13 checksum digit.
Returns:
- RGBABitmapImage of Standard 2 out of 5 barcode, with the narrowest bar of the barcode being 2 pixels.
Interleaved 2 out of 5
GenerateBarcodeInterleaved2of5
Signature:
RGBABitmapImage GenerateBarcodeInterleaved2of5(char [] chars, double height)
Parameters:
- chars – An array of supported characters: 0-9.
- height – The height of the barcode in pixels.
Returns:
- RGBABitmapImage of Interleaved 2 out of 5 barcode, with the narrowest bar of the barcode being 2 pixels.
GenerateBarcodeInterleaved2of5WithChecksumOption
Signature:
RGBABitmapImage GenerateBarcodeInterleaved2of5WithChecksumOption(char [] chars, double height, boolean withChecksum)
Parameters:
- chars – An array of supported characters: 0-9.
- height – The height of the barcode in pixels.
- withChecksum – Generates and adds an EAN-13 checksum digit.
Returns:
- RGBABitmapImage of Interleaved 2 out of 5 barcode, with the narrowest bar of the barcode being 2 pixels.
Writing PNG
ConvertToPNGGrayscale
Signature:
double [] ConvertToPNGGrayscale(RGBABitmapImage image)
Parameters:
- image – An image.
Returns:
- A list of byte values of the PNG image.
ConvertToPNGWithOptions
Signature:
double [] ConvertToPNGWithOptions(RGBABitmapImage image, double colorType, boolean setPhys, double pixelsPerMeter)
Parameters:
- image – An image.
- colorType – 6 for RGBA and 0 for grayscale.
- setPhys – Set to true to include pixels per meter in image file.
- pixelsPerMeter – Pixels per meter. For example, use
DPIToDotsPerMm(300)*1000
to convert 300 DPI to pixels per meter.
Returns:
- A list of byte values of the PNG image.
Scaling
BilinaerScaleUp
Signature:
RGBABitmapImage BilinaerScaleUp(RGBABitmapImage src, double newWidth, double newHeight)
Parameters:
- src – An image.
- newWidth – The new width of the image.
- newHeight – The new height of the image.
Returns:
- RGBABitmapImage of the scaled image. Note that using this scaling method multiple times on the same image is not recommended.
Getting Started
Generating a barcode with the Barcode Software Component, Library and SDK is easy:
Java
- Alternative 1: Source Code
- Download the source code.
- Copy the source code to your source code directory.
- Alternative 2: Jar file
- Download the JAR file
- Include the JAR file as a library in your Java build
- Use the following function to convert the barcode to a Java BufferedImage class:
ImageConversions.convertRGBABitmapImageToJavaBufferedImage
.
The following example will write the Code 128 barcode image to the PNG file java-Code128.png
.
import java.io.*;
import static Barcodes.*;
public class BarcodeWriter {
public static void main(String[] args) throws IOException {
RGBABitmapImage image = GenerateBarcodeCode128("abcdefgh".toCharArray(), 50d);
double [] pngdata = ConvertToPNGGrayscale(image);
byte [] bytes = ArrayConversions.doubleArrayToByteArray(data);
try(FileOutputStream fos = new FileOutputStream("java-Code128.png")) {
fos.write(bytes);
}
DeleteImage(image);
}
}
C/C++/Objective C
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Copy the source code to your source code directory.
- Include all the .h files
- Add all the .c files
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Compile the bundle as a static or dynamic library.
- Include the source file or library in your build.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
- Popular image libraries include libpng for PNG and libjpeg for JPEG.
The following example will write a Code 128 barcode to the file Code128-123456.png
.
#include <stdio.h>
#include "Barcodes-bundle.c"
int main(){
size_t length;
RGBABitmapImage *image = GenerateBarcodeCode128(strparam("123456"), 50);
double *pngdata = ConvertToPNGGrayscale(&length, image);
writeToFile(pngdata, length, "Code128-123456.png");
free(pngdata);
DeleteImage(image);
return 0;
}
JavaScript
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example for Node that will write a Code 128 barcode to js-Code128.png:
var fs = require('fs');
eval(fs.readFileSync('Barcodes-bundle.js')+'');
eval(fs.readFileSync('supportLib.js')+'');
var image = GenerateBarcodeCode128("abcdefgh", 50);
var pngdata = ConvertToPNGGrayscale(image);
writeToFile(pngdata, "js-Code128.png");
DeleteImage(image);
PHP
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example. The variable bytes contains the bytes of the PNG image of the barcode:
<?php
include("Barcodes-bundle.php");
$image = GenerateBarcodeCode128($literal = str_split("abcdefgh"), 50);
$pngdata = ConvertToPNGGrayscale($image);
$bytes = pack('C*', ...$pngdata);
$fp = fopen('php-Code128.png', 'wb');
fwrite($fp, $bytes);
fclose($fp);
DeleteImage($image);
Python
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example:
import struct
execfile("Barcodes-bundle.py")
image = GenerateBarcodeCode128("abcdefgh", 50)
pngdata = ConvertToPNGGrayscale(image)
bytes = ''.join(struct.pack("B", x) for x in pngdata)
f = open('py-Code128.png', 'wb')
f.write(bytes)
f.close()
Ruby
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example. The array data
contains the bytes of the PNG image of the barcode:
load 'Barcodes-bundle.rb'
image = GenerateBarcodeCode128("abcdefgh".split(""), 50)
pngdata = ConvertToPNG(image)
data = pngdata.pack('C*')
outfile = File.new("ruby-Code128.png", "wb")
outfile.puts(data)
outfile.close
C#
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example. The array bytes contains the bytes of the PNG image of the barcode:
using System;
using System.IO;
using static Barcodes;
class BarcodeWriter {
public static void Main(){
RGBABitmapImage image = GenerateBarcodeCode128("abcdefgh".ToCharArray(), 50);
double [] pngdata = ConvertToPNGGrayscale(image);
byte [] bytes = SupportLib.doubleArrayToByteArray(pngdata);
File.WriteAllBytes("cs-Code128.png", bytes);
DeleteImage(image);
}
}
Visual Basic .NET
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example:
Module BarcodeWriter
Sub Main(args As String())
Dim image as RGBABitmapImage = Barcodes.GenerateBarcodeCode128("abcdefgh".ToCharArray(), 50)
Dim pngdata as Double() = ConvertToPNGGrayscale(image)
Dim bytes as byte() = SupportLib.doubleArrayToByteArray(pngdata)
System.IO.File.WriteAllBytes("vb-Code128.png", bytes)
Barcodes.DeleteImage(image)
End Sub
End Module
Swift
- Alternative 1: Source Code – Separate Files
- Download the source code with separate files.
- Alternative 2: Source Code – Bundle
- Download the source code with the bundle.
- Copy the source code to your source code directory.
- The RGBABitmapImage structure contains the image. It should be easy to convert this structure to any other bitmap image format. See the documentation of RGBABitmapImage for more information.
Here is an example:
var image : RGBABitmapImage = GenerateBarcodeCode128(&characterArray("abcdefgh").ca, 50).ref
var pngdata : [Double] = ConvertToPNGGrayscale(&image)
var bytes : [UInt8] = doubleArrayToByteArray(pngdata)
writeBytesToFile(bytes, "swift-Code128.png");
RGBABitmapImage structure
The images are returned in the RGBABitmapImage structure. It contains rows and columns of pixels, each with a red, green, blue and alpha channel.
RGBABitmapImage contains one element x
, the columns of pixels.
Each column is a RGBABitmap structure which contains one element, y
the rows of pixels.
Each row is an RGBA structure which contains four values from 0-1. For red, green and blue, 1 is full, 0 is none. The alpha value is 0 for transparent and 1 for opaque.
Notes on Each Programming Language
The library works exactly the same for all programming languages. This includes even minor updates. There is no risk of deviation. We have an advanced proprietary technology that allows us to ensure this.
Java
The Java version is delivered as source code and as a Java Archive (JAR). It requires Java 1.5+. Contact us if you require support for Java 1.0-1.4.
C/C++/Objective C
This version of the library uses only features of C for which C++ and Objective C is backwards compatible. Therefore, you can use the same library for C, C++ and Objective C.
The code is delivered as separate source code files or as a single source code file. Both are easily built to a static or dynamic library.
The requirements are C 89+, C++98+ and Objective C 1. Contact us if you require support for earlier versions.
JavaScript
The JavaScript version of the library is JavaScript 5+. This works in most browsers, NodeJS and the Java Scripting Engine (Nashorn).
The code is delivered as separate source code files or as a single source code file.
The requirements are JavaScript 5+ (ECMAScript 5). Contact us if you require support for earlier versions.
PHP
The PHP version of the library is PHP 5+. This works both for the Apache PHP module and with the PHP client.
The code is delivered as separate source code files or as a single source code file.
The requirements are PHP 5+. Contact us if you require support for earlier versions.
Python
The Python version of the library is Python 2+.
We deliver the code as separate source code files or as a single source code file.
The requirements are Python 2+. Contact us if you require support for earlier versions.
Ruby
The Ruby version of the library is Ruby 2+.
We deliver the code as separate source code files or as a single source code file.
The requirements are Ruby 2+. Contact us if you require support for earlier versions.
C#
The C# version of the library is C# 7+.
We deliver the code as separate source code files or as a single source code file.
The requirements are C# 7+. Contact us if you require support for earlier versions.
Visual Basic .NET
The Visual Basic .NET version of the library is Visual Basic .NET 9+.
We deliver the code as separate source code files or as a single source code file.
The requirements are Visual Basic .NET 9+. Contact us if you require support for earlier versions. Note that Visual Basic for Application is a separate programming language. There are many Basic variants.
Swift
The Swift version of the library is Swift 4.2+.
We deliver the code as separate source code files or as a single source code file.
The requirements are Swift 4.2+. Contact us if you require support for earlier versions.
Release Notes
v1.0.10 – 2019-01-14
- EAN-13, EAN-8, UPC-A and UPC-E now takes height and width in mm and pixels per mm as input.
- Code 39, Standard 2 of 5/Industrial 2 of 5 and interleaved 2 of 5 now have quiet zones.
- Standard 2 of 5/Industrial 2 of 5 and interleaved 2 of 5 no longer include the text underneath.
- A new recommended scaling method is included: Bilinear interpolation.
v1.0.9 – 2019-01-09
- Added function for validating Code 128 input.
- Added function for scaling that preserves the barcode.
v1.0.8 – 2019-01-05
- Added support for export to PNG.
- Bundled a support library with C, JavaScript, C#, VB.NET and Swift.
- Fixed width-issue in Interleaving 2 of 5.
- Fixed encoding issue in Code39.
- Updated manual with full examples.
- Code128 now utilizes C for denser numeric data encoding.
v1.0.7 – 2019-01-02
- Code 39 now has 2 pixel module width.
v1.0.6 – 2018-12-29
- Supports Code 128 with automatic optimizing w.r.t. Code 128 A, B and C.
v1.0.5 – 2018-12-28
- Added support for UPC-A, UPC-E.
- Added support for converting between UPC-A and UPC-E.
- Added support for Standard/Industrial 2 out of 5, Interleaved 2 out of 5, both with optional EAN/UPC checksum.
v1.0.4 – 2018-12-21
- Added support for EAN-8 and EAN-13.
v1.0.2 – 2018-12-20
- Added support for Code 39.
- Added support for Code 128 C excluding control characters Code A, Code B, FNC 1 and Reverse Stop.
v1.0.0 – 2018-06-20
First release. Supports Code 128 B excluding control characters FNC1-4, Code A, Code C, Del, Shift A and Reverse Stop.