BarEncoder : Barcode and QR code encoder
The barencoder
module provides bar code encode function.
User can use the following code to import the barencoder
module.
var barencoder = require('barencoder');
Support
The following shows barencoder
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
barencoder.defaultOpt | ● | ● |
barencoder.encode | ● | ● |
BarEncoder Object
barencoder.defaultOpt(width, height, margin, eccLevel)
width
{Integer} Image width. default: 100.height
{Integer} Image height. default: 100.margin
{Integer} Image margin, used for Aztec, PDF417, and QRCode only. default: 10.eccLevel
{Integer} ECC level, can be [0-8], used for Aztec, PDF417, and QRCode only. default: 2.- Returns: {Object} Bar code encode options.
The returned object contains the following members:
width
{Integer} Image width.height
{Integer} Image height.margin
{Integer} Image margin.eccLevel
{Integer} ECC level, can be [0-8].
barencoder.encode(text, format, path[, opt])
text
{String} Text which need encoded.format
{String} Encoder format.path
{String} Output image file path, suffix name can be *.png and *.jpg.opt
{Object} Bar code encode option object. default:barencoder.defaultOpt(100, 100, 10, 2)
.- Returns: {Boolean}
true
means success,false
means failure.
format
is a string, can be:
Value | Description |
---|---|
barencoder.FORMAT_AZTEC | AXTEC encoder(2D, beta). |
barencoder.FORMAT_CODABAR | CODABAR encoder(1D industrial). |
barencoder.FORMAT_CODE_39 | CODE_39 encoder(1D industrial). |
barencoder.FORMAT_CODE_93 | CODE_93 encoder(1D industrial). |
barencoder.FORMAT_CODE_128 | CODE_128 encoder(1D industrial). |
barencoder.FORMAT_DATA_MATRIX | DATA_MATRIX encoder(2D). |
barencoder.FORMAT_EAN_8 | EAN_8 encoder(1D product). |
barencoder.FORMAT_EAN_13 | EAN_13 encoder(1D product). |
barencoder.FORMAT_ITF | ITF encoder(1D industrial). |
barencoder.FORMAT_PDF_417 | PDF_417 encoder(2D, beta). |
barencoder.FORMAT_QR_CODE | QR_CODE encoder(2D). |
barencoder.FORMAT_UPC_A | UPC_A encoder(1D product). |
barencoder.FORMAT_UPC_E | UPC_E encoder(1D product). |
Example
This example show how to use QR Code
encoder encode a text to a PNG
format image file.
var barencoder = require('barencoder');
var opt = barencoder.defaultOpt(100, 100, 5, 8);
// will generate sylixos_qr.png
barencoder.encode('www.sylixos.com', barencoder.FORMAT_QR_CODE, './sylixos_qr.png', opt);
Example
This example show how to use Code-128
encoder encode a text to a PNG
format image file.
var barencoder = require('barencoder');
// will generate sylixos_1d.png
barencoder.encode('www.sylixos.com', barencoder.FORMAT_CODE_128, './sylixos_1d.png');
barencoder.encode(text, format, imageFormat[, opt])
text
{String} Text which need encoded.format
{String} Encoder format.imageFormat
{Object} Output image format object.opt
{Object} Bar code encode option object. default:barencoder.defaultOpt(100, 100, 10, 2)
.- Returns: {Buffer} Image buffer.
The bar code imageFormat
object contains the following members:
pixelFormat
{Integer} Pixel format.pixelBytes
{Integer} Pixel byte size of image buffer. Only RGB format needed.redIndex
{Integer} Red index of RGB pixel. Only RGB format needed.greenIndex
{Integer} Green index of RGB pixel. Only RGB format needed.blueIndex
{Integer} Blue index of RGB pixel. Only RGB format needed.
pixelFormat
is a integer, can be:
Value | Description |
---|---|
barencoder.PIX_FMT_YUV420P | YUV pixel format. |
barencoder.PIX_FMT_GRAY8 | Grayscale pixel format. |
barencoder.PIX_FMT_RGB | RGB pixel format. |
barencoder.PIX_FMT_JPEG | JPEG pixel format. |
barencoder.PIX_FMT_PNG | PNG pixel format. |
RGB pixel format:
For example RGBA8888:
0 1 2 3
+-------+-------+-------+-------+
| red | green | blue | alpha |
+-------+-------+-------+-------+
pixelBytes
is 4
.redIndex
is 0
.greenIndex
is 1
.blueIndex
is 2
.
For example RGB888:
0 1 2
+-------+-------+-------+
| red | green | blue |
+-------+-------+-------+
pixelBytes
is 3
.redIndex
is 0
.greenIndex
is 1
.blueIndex
is 2
.
Example
This example show how to use QR Code
encoder encode a text to a RGB888
format image buffer.
var rgb888Buf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE,
{ pixelFormat: barencoder.PIX_FMT_RGB, pixelBytes: 3, redIndex: 0, greenIndex: 1, blueIndex: 2 },
{ width: 100, height: 100, margin: 10 });
console.log(rgb888Buf.length);
This example show how to use QR Code
encoder encode a text to a YUV
format image buffer.
var yuvBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE,
{ pixelFormat: barencoder.PIX_FMT_YUV420P },
{ width: 100, height: 100, margin: 10 });
console.log(yuvBuf.length);
This example show how to use QR Code
encoder encode a text to a Grayscale
format image buffer.
var grayscaleBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE,
{ pixelFormat: barencoder.PIX_FMT_GRAY8 },
{ width: 100, height: 100, margin: 10 });
console.log(grayscaleBuf.length);
This example show how to use QR Code
encoder encode a text to a JPEG
format image buffer.
var jpegBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE,
{ pixelFormat: barencoder.PIX_FMT_JPEG },
{ width: 100, height: 100, margin: 10 });
console.log(jpegBuf.length);
This example show how to use QR Code
encoder encode a text to a PNG
format image buffer.
var pngBuf = barencoder.encode("www.sylixos.com", barencoder.FORMAT_QR_CODE,
{ pixelFormat: barencoder.PIX_FMT_PNG },
{ width: 100, height: 100, margin: 10 });
console.log(pngBuf.length);