Skip to content

Commit

Permalink
Add optional parameters to the qrcode() function
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsLeenheer committed Aug 7, 2018
1 parent ce6d4f7 commit 55f5513
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 14 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,24 @@ The following symbologies can be used: 'upca', 'ean13', 'ean8', 'code39', 'itf',

### Qrcode

Print a QR code. The first parameter is the value of the QR code.
Print a QR code. The first parameter is the data of the QR code.

let result = encoder
.qrcode('https://nielsleenheer.com')
.encode()

The qrcode function accepts the following additional parameters:

- *model* - a number that can be 1 for Model 1 and 2 for Model 2
- *size* - a number that can be between 1 and 8 for determining the size of the QR code
- *errorlevel* - a string that can be either 'l', 'm', 'q' or 'h'.

For example:

let result = encoder
.qrcode('https://nielsleenheer.com', 1, 8, 'h')
.encode()


### Image

Expand Down
85 changes: 74 additions & 11 deletions src/esc-pos-encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,26 +348,89 @@ class EscPosEncoder {
* QR code
*
* @param {string} value the value of the qr code
* @param {number} model model of the qrcode, either 1 or 2
* @param {number} size size of the qrcode, a value between 1 and 8
* @param {string} errorlevel the amount of error correction used, either 'l', 'm', 'q', 'h'
* @return {object} Return the object, for easy chaining commands
*
*/
qrcode(value) {
let bytes = iconv.encode(value, 'iso88591');
qrcode(value, model, size, errorlevel) {
/* Force printing the print buffer and moving to a new line */

this._queue([
0x0a,
]);

/* Model */

const models = {
1: 0x31,
2: 0x32,
};

if (typeof model === 'undefined') {
model = 2;
}

if (model in models) {
this._queue([
0x1d, 0x28, 0x6b, 0x04, 0x00, 0x31, 0x41, models[model], 0x00,
]);
} else {
throw new Error('Model must be 1 or 2');
}

/* Size */

// 0x1d, 0x21, [ 0x00, 0x11, 0x22, 0x33 ]
if (typeof size === 'undefined') {
size = 6;
}

// 0x1b, 0x4a, 0x28
if (typeof size !== 'number') {
throw new Error('Size must be a number');
}

if (size < 1 || size > 8) {
throw new Error('Size must be between 1 and 8');
}

this._queue([
0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, size,
]);

/* Error level */

const errorlevels = {
'l': 0x30,
'm': 0x31,
'q': 0x32,
'h': 0x33,
};

if (typeof errorlevel === 'undefined') {
errorlevel = 'm';
}

if (errorlevel in errorlevels) {
this._queue([
0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x45, errorlevels[errorlevel],
]);
} else {
throw new Error('Error level must be l, m, q or h');
}

/* Data */

let bytes = iconv.encode(value, 'iso88591');
let length = bytes.length + 3;

this._queue([
0x1d, 0x28, 0x6b, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00,
0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x06,
0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x45, 0x31,
0x1d, 0x28, 0x6b,
length % 0xff, length / 0xff,
0x31, 0x50, 0x30,
bytes,
0x1d, 0x28, 0x6b, length % 0xff, length / 0xff, 0x31, 0x50, 0x30, bytes,
]);

/* Print QR code */

this._queue([
0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30,
]);

Expand Down
12 changes: 10 additions & 2 deletions test/esc-pos-encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,16 @@ describe('EscPosEncoder', function() {
describe('qrcode(https://nielsleenheer.com)', function () {
let result = encoder.qrcode('https://nielsleenheer.com').encode();

it('should be [ 29, 40, 107, 4, 0, 49, 65, 50, 0, 29, 40, 107, 3, 0, ... ]', function () {
assert.deepEqual(new Uint8Array([ 29, 40, 107, 4, 0, 49, 65, 50, 0, 29, 40, 107, 3, 0, 49, 67, 6, 29, 40, 107, 3, 0, 49, 69, 49, 29, 40, 107, 28, 0, 49, 80, 48, 104, 116, 116, 112, 115, 58, 47, 47, 110, 105, 101, 108, 115, 108, 101, 101, 110, 104, 101, 101, 114, 46, 99, 111, 109, 29, 40, 107, 3, 0, 49, 81, 48 ]), result);
it('should be [ 10, 29, 40, 107, 4, 0, 49, 65, 50, 0, 29, 40, 107, 3, 0, ... ]', function () {
assert.deepEqual(new Uint8Array([ 10, 29, 40, 107, 4, 0, 49, 65, 50, 0, 29, 40, 107, 3, 0, 49, 67, 6, 29, 40, 107, 3, 0, 49, 69, 49, 29, 40, 107, 28, 0, 49, 80, 48, 104, 116, 116, 112, 115, 58, 47, 47, 110, 105, 101, 108, 115, 108, 101, 101, 110, 104, 101, 101, 114, 46, 99, 111, 109, 29, 40, 107, 3, 0, 49, 81, 48 ]), result);
});
});

describe('qrcode(https://nielsleenheer.com, 1, 8, h)', function () {
let result = encoder.qrcode('https://nielsleenheer.com', 1, 8, 'h').encode();

it('should be [ 10, 29, 40, 107, 4, 0, 49, 65, 49, 0, 29, 40, 107, 3, 0, ... ]', function () {
assert.deepEqual(new Uint8Array([ 10, 29, 40, 107, 4, 0, 49, 65, 49, 0, 29, 40, 107, 3, 0, 49, 67, 8, 29, 40, 107, 3, 0, 49, 69, 51, 29, 40, 107, 28, 0, 49, 80, 48, 104, 116, 116, 112, 115, 58, 47, 47, 110, 105, 101, 108, 115, 108, 101, 101, 110, 104, 101, 101, 114, 46, 99, 111, 109, 29, 40, 107, 3, 0, 49, 81, 48 ]), result);
});
});

Expand Down

0 comments on commit 55f5513

Please sign in to comment.