import { Injectable } from '@angular/core'; // import { QRCodeSegment, QRCodeOptions, QRCode } from 'qrcode'; import QRCode, { QRCodeSegment, QRCodeOptions, QRCodeToDataURLOptions, QRCodeToStringOptions, QRCodeToFileOptions } from 'qrcode'; import * as stream from 'stream'; @Injectable() export class NaQRCodeService { constructor() { } /** * Creates QR Code symbol and returns a qrcode object. */ create(text: string | QRCodeSegment[], options: QRCodeOptions): QRCode.QRCode { return QRCode.create(text, options); } /** * Draws qr code symbol to canvas. */ toCanvas(text: string | QRCodeSegment[], options?: QRCodeOptions, canvas?: HTMLCanvasElement | any): Promise { if (canvas) { return QRCode.toCanvas(canvas, text, options); } return QRCode.toCanvas(text, options); } /** * Returns a Data URI containing a representation of the QR Code image. */ toDataURL(text: string | QRCodeSegment[], options?: QRCodeToDataURLOptions, canvasElement?: HTMLCanvasElement): Promise { if (canvasElement) { return QRCode.toDataURL(canvasElement, text, options); } return QRCode.toDataURL(text, options); } /** * Returns a string representation of the QR Code. * If choosen output format is svg it will returns a string containing xml code. */ toString(text: string | QRCodeSegment[], options?: QRCodeToStringOptions): Promise { return QRCode.toString(text, options); } /** * Saves QR Code to image file. * If options.type is not specified, the format will be guessed from file extension. * Recognized extensions are png, svg, txt. */ toFile(path: string, text: string | QRCodeSegment[], options?: QRCodeToFileOptions): Promise { return QRCode.toFile(path, text, options); } /** * Writes QR Code image to stream. Only works with png format for now. */ toFileStream(streamW: stream.Writable, text: string | QRCodeSegment[], options?: QRCodeOptions): Promise { return QRCode.toFileStream(streamW, text, options); } }