-
Notifications
You must be signed in to change notification settings - Fork 10
/
qrcode.ts
38 lines (32 loc) · 1.04 KB
/
qrcode.ts
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
import {
PassThrough,
Readable,
} from 'stream'
// The npm package of my best choice for QR code decoding on Angular SPA
// https://dev.to/j_sakamoto/the-npm-package-of-my-best-choice-for-qr-code-decoding-on-angular-spa-4747?returning-user=true
import Jimp from 'jimp'
import jsQR from 'jsqr'
/**
* https://www.npmjs.com/package/qrcode
* Huan(202002): This module is encode only.
*/
import { toFileStream } from 'qrcode'
export async function bufferToQrValue (buf: Buffer): Promise<string> {
const image = await Jimp.read(buf)
const qrCodeImageArray = new Uint8ClampedArray(image.bitmap.data.buffer)
const qrCodeResult = jsQR(
qrCodeImageArray,
image.bitmap.width,
image.bitmap.height,
)
if (qrCodeResult) {
return qrCodeResult.data
} else {
throw new Error('bufferToQrcode(buf) fail!')
}
}
export async function qrValueToStream (value: string): Promise<Readable> {
const stream = new PassThrough()
await toFileStream(stream, value) // only support .png for now
return stream
}