34 lines
864 B
TypeScript
34 lines
864 B
TypeScript
import QRCode from 'qrcode'
|
|
|
|
/**
|
|
* 将 URL 转换为二维码图片的 data URL
|
|
*/
|
|
export async function generateQrCodeDataUrl(url: string, options?: QRCode.QRCodeToDataURLOptions): Promise<string> {
|
|
const defaultOptions: QRCode.QRCodeToDataURLOptions = {
|
|
width: 280,
|
|
margin: 2,
|
|
color: {
|
|
dark: '#000000',
|
|
light: '#ffffff'
|
|
}
|
|
}
|
|
|
|
return QRCode.toDataURL(url, { ...defaultOptions, ...options })
|
|
}
|
|
|
|
/**
|
|
* 将 URL 转换为二维码图片的 Canvas 元素
|
|
*/
|
|
export async function generateQrCodeCanvas(url: string, canvas: HTMLCanvasElement, options?: QRCode.QRCodeToCanvasOptions): Promise<void> {
|
|
const defaultOptions: QRCode.QRCodeToCanvasOptions = {
|
|
width: 280,
|
|
margin: 2,
|
|
color: {
|
|
dark: '#000000',
|
|
light: '#ffffff'
|
|
}
|
|
}
|
|
|
|
return QRCode.toCanvas(canvas, url, { ...defaultOptions, ...options })
|
|
}
|