Skip to content

Commit 1b52c56

Browse files
committed
add toStream() to get a stream from canvas back
add toStream() to get a stream from canvas back #23
1 parent e6ae474 commit 1b52c56

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

demo/demo_node.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,33 @@ qrcode5.toSVGText().then(data=>{
230230

231231
qrcode8.saveSVG({
232232
path: 'qrcode.svg'
233-
})
233+
})
234+
235+
236+
237+
var streamConfig = {
238+
// ====== Basic
239+
text: "https://github.com/ushelp/EasyQRCodeJS-NodeJS",
240+
colorLight: 'transparent',
241+
width: 150,
242+
height: 150,
243+
quietZone: 10,
244+
quietZoneColor: 'transparent',
245+
}
246+
247+
248+
async function generate() {
249+
var streamQrcode = new QRCode(streamConfig);
250+
251+
const out = fs.createWriteStream(`qrcode-stream.png`);
252+
// const stream = await streamQrcode.toStream();
253+
// stream.pipe(out);
254+
// out.on('finish', () => console.log('Finsihed'));
255+
256+
streamQrcode.toStream().then(res=>{
257+
res.pipe(out).on('finish', () => console.log('Stream Finsihed'));
258+
})
259+
260+
}
261+
262+
generate();

index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ declare class QRCode {
1414
toDataURL(format?: any): any;
1515

1616
toSVGText(format?: any): any;
17+
18+
toStream(format?: any): any;
1719

1820
static CorrectLevel: {
1921
H: number;

index.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)
55
*
6-
* Version 4.3.5
6+
* Version 4.4.0
77
*
88
* @author [ [email protected] ]
99
*
@@ -1543,6 +1543,19 @@ Drawing.prototype.makeImage = function() {
15431543
})
15441544
}
15451545
}
1546+
} else if (makeOptions.makeType == 'STREAM') {
1547+
1548+
if (this._htOption.onRenderingStart) {
1549+
this._htOption.onRenderingStart(this._htOption);
1550+
}
1551+
1552+
if (this._htOption.format == 'PNG') {
1553+
// dataUrl = this._canvas.toDataURL()
1554+
t.resolve(this._canvas.createPNGStream());
1555+
} else {
1556+
t.resolve(this._canvas.createJPEGStream());
1557+
}
1558+
15461559
}
15471560
};
15481561

@@ -1813,9 +1826,9 @@ QRCode.prototype.saveSVG = function(saveOptions) {
18131826
};
18141827

18151828
// Get Base64 or SVG text
1816-
QRCode.prototype._toData = function(drawer) {
1829+
QRCode.prototype._toData = function(drawer, makeType) {
18171830
var defOptions = {
1818-
makeType: 'URL'
1831+
makeType: makeType?makeType:'URL'
18191832
}
18201833
this._htOption._drawer = drawer;
18211834

@@ -1847,7 +1860,12 @@ QRCode.prototype.toDataURL = function() {
18471860
QRCode.prototype.toSVGText = function() {
18481861
return this._toData('svg');
18491862
};
1850-
1863+
/**
1864+
* Get SVG data text: '<svg xmlns:xlink="http://www.w3.org/1999/xlink" ...'
1865+
*/
1866+
QRCode.prototype.toStream = function() {
1867+
return this._toData('canvas', 'STREAM');
1868+
};
18511869

18521870
/**
18531871
* @name QRCode.CorrectLevel

index.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "easyqrcodejs-nodejs",
3-
"version": "4.3.5",
3+
"version": "4.4.0",
44
"description": "NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)",
55
"main": "index.min.js",
66
"scripts": {},

readme.md

+17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ EasyQRCodeJS-NodeJS 是一个 NodeJS 环境下的服务端 JavaScript QRCode 图
4242
- Support get standard base64 image data url text: `data:image/png;base64, ...`
4343

4444
- Support get SVG data text: `<svg xmlns:xlink="http://www.w3.org/1999/xlink" ...`
45+
46+
- Support get a stream from Canvas
4547

4648
- Required Patterns that support dot style
4749

@@ -72,6 +74,8 @@ EasyQRCodeJS-NodeJS 是一个 NodeJS 环境下的服务端 JavaScript QRCode 图
7274
- 支持获得 Base64 编码的标准图形 URL 字符串:`data:image/png;base64, ...`
7375

7476
- 支持获得 SVG 图形文本: `<svg xmlns:xlink="http://www.w3.org/1999/xlink" ...`
77+
78+
- 支持从 Canvas 获得图片 Stream
7579

7680
- 支持点形风格的 Required Patterns
7781

@@ -377,6 +381,19 @@ var qrcode = new QRCode(options);
377381
});
378382
```
379383

384+
- **toStream()**
385+
386+
```JS
387+
const out = fs.createWriteStream(`qrcode-stream.png`);
388+
// const stream = await qrcode.toStream();
389+
// stream.pipe(out);
390+
// out.on('finish', () => console.log('Finsihed'));
391+
392+
qrcode.toStream().then(res=>{
393+
res.pipe(out).on('finish', () => console.log('Finsihed'));
394+
})
395+
```
396+
380397
## TypeScript Support
381398

382399
Update to version `3.7.1+`.

0 commit comments

Comments
 (0)