mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-24 08:46:15 -04:00
feat(tool): qr-code generator
This commit is contained in:
parent
203b6a9d73
commit
5582d75927
8 changed files with 527 additions and 21 deletions
11
src/tools/qr-code-generator/index.ts
Normal file
11
src/tools/qr-code-generator/index.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { Qrcode } from '@vicons/tabler';
|
||||
import type { ITool } from './../Tool';
|
||||
|
||||
export const tool: ITool = {
|
||||
name: 'QR Code generator',
|
||||
path: '/qr-code-generator',
|
||||
description: 'Generate and download QR-code for an url or just a text and customize the background and foreground colors.',
|
||||
keywords: ['qr', 'code', 'generator', 'square', 'color', 'link', 'low', 'medium', 'quartile', 'high', 'transparent'],
|
||||
component: () => import('./qr-code-generator.vue'),
|
||||
icon: Qrcode,
|
||||
};
|
60
src/tools/qr-code-generator/qr-code-generator.vue
Normal file
60
src/tools/qr-code-generator/qr-code-generator.vue
Normal file
|
@ -0,0 +1,60 @@
|
|||
<template>
|
||||
<n-card>
|
||||
<n-grid cols="3" x-gap="12">
|
||||
<n-gi span="2">
|
||||
<n-form label-width="130" label-placement="left">
|
||||
<n-form-item label="Text:">
|
||||
<n-input v-model:value="text" placeholder="Your link or text..." />
|
||||
</n-form-item>
|
||||
<n-form-item label="Foreground color:">
|
||||
<n-color-picker v-model:value="foreground" :modes="['hex']" />
|
||||
</n-form-item>
|
||||
<n-form-item label="Background color:">
|
||||
<n-color-picker v-model:value="background" :modes="['hex']" />
|
||||
</n-form-item>
|
||||
<n-form-item label="Error resistance:">
|
||||
<n-select v-model:value="errorCorrectionLevel"
|
||||
:options="errorCorrectionLevels.map((value) => ({ label: value, value }))" />
|
||||
</n-form-item>
|
||||
</n-form>
|
||||
</n-gi>
|
||||
<n-gi>
|
||||
<n-space justify="center" align="center" vertical>
|
||||
<n-image :src="qrcode" width="200" />
|
||||
<n-button @click="download" secondary>Download qr-code</n-button>
|
||||
</n-space>
|
||||
</n-gi>
|
||||
</n-grid>
|
||||
</n-card>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
|
||||
import { useQRCode } from './useQRCode'
|
||||
import { ref } from 'vue';
|
||||
import type { QRCodeErrorCorrectionLevel } from 'qrcode';
|
||||
|
||||
|
||||
const foreground = ref('#000000ff')
|
||||
const background = ref('#ffffffff')
|
||||
const errorCorrectionLevel = ref<QRCodeErrorCorrectionLevel>('medium')
|
||||
|
||||
const errorCorrectionLevels = ['low', 'medium', 'quartile', 'high']
|
||||
|
||||
const text = ref('https://it-tools.tech')
|
||||
const { qrcode } = useQRCode({
|
||||
text,
|
||||
color: {
|
||||
background,
|
||||
foreground
|
||||
},
|
||||
errorCorrectionLevel,
|
||||
options: { width: 1024 }
|
||||
})
|
||||
|
||||
const { download } = useDownloadFileFromBase64({ source: qrcode, filename: 'qr-code.png' })
|
||||
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
</style>
|
35
src/tools/qr-code-generator/useQRCode.ts
Normal file
35
src/tools/qr-code-generator/useQRCode.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import QRCode, { type QRCodeErrorCorrectionLevel, type QRCodeToDataURLOptions } from 'qrcode';
|
||||
import { ref, watch, type Ref } from 'vue';
|
||||
|
||||
export function useQRCode({
|
||||
text,
|
||||
color: { background, foreground },
|
||||
errorCorrectionLevel,
|
||||
options,
|
||||
}: {
|
||||
text: Ref<string>;
|
||||
color: { foreground: Ref<string>; background: Ref<string> };
|
||||
errorCorrectionLevel: Ref<QRCodeErrorCorrectionLevel>;
|
||||
options?: QRCodeToDataURLOptions;
|
||||
}) {
|
||||
const qrcode = ref('');
|
||||
|
||||
watch(
|
||||
[text, background, foreground, errorCorrectionLevel],
|
||||
async () => {
|
||||
if (text.value)
|
||||
qrcode.value = await QRCode.toDataURL(text.value, {
|
||||
color: {
|
||||
dark: foreground.value,
|
||||
light: background.value,
|
||||
...options?.color,
|
||||
},
|
||||
errorCorrectionLevel: errorCorrectionLevel.value,
|
||||
...options,
|
||||
});
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
return { qrcode };
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue