-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-code39.js
55 lines (47 loc) · 1.65 KB
/
app-code39.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { createServer } from 'http';
import { parse } from 'querystring';
import { BCGColor, BCGDrawing, BCGFont } from '@barcode-bakery/barcode-common';
import { BCGcode39 } from '@barcode-bakery/barcode-1d';
let defaultText = 'A123';
// Loading Font
let font = new BCGFont('Arial', 18);
// The arguments are R, G, B for color.
let colorBlack = new BCGColor(0, 0, 0);
let colorWhite = new BCGColor(255, 255, 255);
let getDrawing = function (text) {
let drawException = null,
barcode = null;
try {
let code = new BCGcode39();
code.setScale(2); // Resolution
code.setThickness(30); // Thickness
code.setBackgroundColor(colorWhite); // Color of spaces
code.setForegroundColor(colorBlack); // Color of bars
code.setFont(font); // Font (or 0)
code.setChecksum(false);
code.parse(text); // Text
barcode = code;
} catch (exception) {
drawException = exception;
}
let drawing = new BCGDrawing(barcode, colorWhite);
if (drawException) {
drawing.drawException(drawException);
}
return drawing;
};
/*
// This is how you would save to a file.
let drawing = getDrawing(defaultText);
drawing.save("image.png", BCGDrawing.ImageFormat.Png, function () {
console.log("Done.");
});
*/
createServer(function (request, response) {
let drawing = getDrawing(parse(request.url).query?.toString() || defaultText);
drawing.toBuffer(BCGDrawing.ImageFormat.Png, function (err, buffer) {
response.writeHead(200, { 'Content-Type': 'image/png' });
response.end(buffer);
});
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');