-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
198 lines (179 loc) · 9.27 KB
/
index.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<html>
<head>
<title>WebUSB Serial Sample Application</title>
<script src="bower_components/rgbcolor/index.js"></script>
<script src="bower_components/StackBlur/StackBlur.js"></script>
<script src="bower_components/canvg/dist/canvg.js"></script>
<script src="JsBarcode.all.js"></script>
</head>
<body>
<script>
var device;
var vendorId = 0x0922;
var productId = 0x0021;
// var vendorId = 0x04f9;
// var productId = 0x2043;
var ESC = 0x1B;
var GS = 0x1D;
var SYN = 0x16;
var pageWidth = 560; // pixels (must be multiple of 8)
var pageHeight = 600; // pixels
var $ = function (id) {
return document.getElementById(id);
};
var $$ = function (selector) {
return document.querySelector(selector);
};
var resetSequence = // 156 times ESC
[ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC,
ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC
];
// should set the resolution to 204x204ppi, but the resolution I get is more
// like 290x580, not sure what's up with that.
var setResolution = [ESC, 0x76];
// some more seemingly required setup stuff
var tabData = [ESC, 0x51, 0, 0, ESC, 42, 0];
var qualityData = [ESC, 0x69]; // images
var densityData = [ESC, 0x65]; // normal
var orienation = [GS, 0x56, 0x01];
var lengthData = [ESC, 0x4C, 0x40, 0x00]; // not sure what this is about...
var startDoc = resetSequence.concat(setResolution, orienation, tabData, qualityData,
densityData, lengthData);
var endDoc = [ESC, 0x45]; // Form feed
function createContext(width, height) {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
}
window.onload = function () {
document.querySelector("#connect").onclick = function () {
navigator.usb.requestDevice({
filters: [{
vendorId: 0x0922
}]
})
.then(selectedDevice => {
device = selectedDevice;
console.log('Device', device);
return device.open(); // Begin a session.
})
.then(() => {
// Select configuration #1 for the device.
if (device.configuration === null) {
return this.device.selectConfiguration(0);
}
})
.then(() => device.claimInterface(0)) // Request exclusive control over interface #2.
.then(() => {
var canvas = createContext(pageWidth, pageHeight);
var ctx = canvas.getContext("2d");
// var ctx = document.getElementById('ditheredCanvas').getContext('2d');
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
JsBarcode(svg, "ORD-3464", {
format: "CODE39",
lineColor: "#000",
width: 3,
height: 40,
displayValue: false
});
var mySrc = 'data:image/svg+xml;base64,' + window.btoa(svg);
// Load up our image.
var source = new Image();
source.src = mySrc;
//ctx.rotate(Math.PI / 2);
//ctx.fillStyle = "#FFF";
// ctx.fillRect(0, 0, 219, 121);
// ctx.fillStyle = "#000";
// font = '20 px sans-serif';
// ctx.fillText('Hello world', 3, 15, 219);
// // ctx.drawSvg(SVG_XML_OR_PATH_TO_SVG, dx, dy, dw, dh);
// ctx.drawSvg(svg.outerHTML, 0, 80, 219, 40);
// // ctx.restore();
ctx.font = '48pt Arial';
ctx.save();
ctx.translate(100, 300);
ctx.rotate(-0.5 * Math.PI);
ctx.fillText('Date Ordered : 31-03-2017', -300, -50, 600);
ctx.fillText('Test : HIV VIRAL LOAD, QUANTITATIVE', -300, 45, 600);
ctx.fillText('Patient ID : 743942052-7', -300, 150, 600);
ctx.fillText('Order Number : ORD-34645', -300, 250, 600);
ctx.drawSvg(svg.outerHTML, -300, 280, 600, 210);
ctx.restore();
// window.open(canvas.toDataURL());
var img = ctx.getImageData(0, 0, pageWidth, pageHeight);
var dataBytesPerLine = pageWidth / 8;
// every row of the image results in 2 rows of pixels to be printed, both
// with one extra byte in front of it
var bytesPerRow = (dataBytesPerLine + 1) * 2;
var bytesForImage = bytesPerRow * pageHeight;
// Total size is the size of all the data + the prefix and suffix
// and 3 bytes to set the line size
var totalDataSize = bytesForImage + startDoc.length + endDoc.length + 3;
var data = new ArrayBuffer(totalDataSize);
var dataView = new Uint8Array(data, 0, totalDataSize);
// Set beginning data
dataView.set(startDoc, 0);
var offset = startDoc.length;
// Set Bytes Per Line
dataView.set([ESC, 0x44, 75], offset);
offset += 3;
for (var x = 0; x < img.width; x++) {
var off1 = offset;
var off2 = offset + dataBytesPerLine + 1;
dataView[off1++] = SYN;
dataView[off2++] = SYN;
for (var y = 0; y < img.height; y += 8) {
var cur1 = 0;
var cur2 = 0;
for (var bit = 0; bit < 8; bit++) {
cur1 = cur1 << 1;
cur2 = cur2 << 1;
var i = ((img.height - (y + bit) - 1) * img.width + x) * 4;
// convert color to greyscale
var color = (0.2126 * img.data[i] + 0.7152 * img.data[i + 1] + 0.0722 *
img.data[i + 2]);
// we want higher numbers to be darker colors
color = 255 - color;
// multiple color by alpha channel
color = color * img.data[i + 3] / 255;
// set 0 1 or both bits depending on color
if (color > 170) {
cur1 |= 1;
cur2 |= 1;
} else if (color > 85) {
// for grey, alternate which of the two bits we set
if (bit & 1) cur1 |= 1;
else cur2 |= 1;
}
}
dataView[off1++] = cur1;
dataView[off2++] = cur2;
}
offset = off2;
}
dataView.set(endDoc, offset);
return device.transferOut(2, data)
}) // Ready to receive data
.then(result => {
console.log('Received: ', result);
})
.catch(error => {
console.log(error);
});
}
}
</script>
<button id="connect">Print Sample</button>
</body>