Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added Checksum functionality to 2of5 code. Increased to version 0.0.4. #3

Merged
merged 1 commit into from

2 participants

@sja
sja commented

I needed a checksum, so i implemented it.
The signatures are all the same, but the code implementations can check this.checksum if they should build one or not.

@kmpm kmpm merged commit 8967043 into from
@sja sja deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 3 deletions.
  1. +1 −0  README.md
  2. +5 −0 lib/barc.js
  3. +14 −2 lib/code2of5.js
  4. +1 −1  package.json
View
1  README.md
@@ -58,6 +58,7 @@ Constructor which enables you to set common options for all generated barcodes.
- padding Defaults to 0. Padding on the left and right edges. Can be a function with the signature (width, height).
- font - Defaults to 'FreeMono'. That font must be available to the graphics library
- fontsize - Defaults to border width or min 12px. Number in pixels of the font size for hri.
+ - checksum - Defaults to false. If true, the checksum is appended, if the code varaiant supports it. __Note__: In case of 2of5, the input length increases by one. As a result, the input length switches from even to odd and vice versa.
View
5 lib/barc.js
@@ -51,6 +51,11 @@ var Barc = module.exports = function Barc(options){
this.border = b;
}
}
+
+ this.checksum = false;
+ if ('checksum' in options){
+ this.checksum = options.checksum;
+ }
}
Barc.prototype.code128 = require('./code128').code128;
View
16 lib/code2of5.js
@@ -38,7 +38,7 @@ exports.code2of5 = function(code, width, height, angle, callback){
'STOP':"WnNn",
};
- var c = makeCode(code);
+ var c = makeCode(code, this.checksum);
code = c[0];
var bits = c[1];
@@ -92,7 +92,10 @@ exports.code2of5 = function(code, width, height, angle, callback){
return buf;
}
- function makeCode(code){
+ function makeCode(code, checksum){
+ if (checksum) {
+ code += getChecksum(code);
+ }
if((code.length % 2) != 0){
//the length of an 2of5 code must be even
code = "0" +code;
@@ -134,4 +137,13 @@ exports.code2of5 = function(code, width, height, angle, callback){
narrow = code.length * (2.0*MULTIPLE+3.0)+6.0+MULTIPLE
return width/narrow
}
+
+ function getChecksum(code) {
+ var idx, weight, sum = 0;
+ for (idx = 0; idx < code.length; idx++) {
+ weight = idx % 2 ? 1 : 3;
+ sum += weight * parseInt(code.charAt(idx));
+ }
+ return sum % 10;
+ }
}
View
2  package.json
@@ -3,7 +3,7 @@
"name": "barc",
"description": "Library for creating 1D barcodes with node.js. Backed by node-canvas.",
"keywords": ["barcode", "canvas", "code 128", "code 2of5"],
- "version": "0.0.3",
+ "version": "0.0.4",
"repository": {
"type": "git",
"url": "git://github.com/birchroad/node-barc"
Something went wrong with that request. Please try again.