From 89c138cedf6590e6a9dd32837835c6c500b1f3ee Mon Sep 17 00:00:00 2001 From: Miao_Spring <539943419@qq.com> Date: Tue, 28 Jan 2025 10:16:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=80=E4=BA=9B=E7=BB=86=E6=9E=9D?= =?UTF-8?q?=E6=9C=AB=E8=8A=82=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/tools/miaoQRGBCode.vue | 8 +------ pages/tools/QRGB/scanner.vue | 35 ++++++++++++++++++------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/components/tools/miaoQRGBCode.vue b/components/tools/miaoQRGBCode.vue index 745816d..47b9a86 100644 --- a/components/tools/miaoQRGBCode.vue +++ b/components/tools/miaoQRGBCode.vue @@ -30,13 +30,7 @@ const generateQRCodeCanvasData = (data: Uint8Array) => { } const paddingData = (data: Uint8Array) => { - if(data.length % 3 === 0) { - return new Uint8Array([...data, 0, 1, 2]) - } else if(data.length % 3 === 1) { - return new Uint8Array([...data, 0, 1]) - } else { - return new Uint8Array([...data, 0]) - } + return new Uint8Array([...data, ...new Array(3 - data.length % 3).fill(0).map((_, i) => i)]) } onMounted(() => { diff --git a/pages/tools/QRGB/scanner.vue b/pages/tools/QRGB/scanner.vue index c2c231f..2ad8488 100644 --- a/pages/tools/QRGB/scanner.vue +++ b/pages/tools/QRGB/scanner.vue @@ -35,26 +35,30 @@ const u8iEndWith = (data: Uint8Array, target: number[]) => { } const unpaddingData = (data: Uint8Array): Uint8Array => { - if(u8iEndWith(data, [0, 1, 2])) { - return data.slice(0, data.length - 3) - } else if (u8iEndWith(data, [0, 1])) { - return data.slice(0, data.length - 2) - } else { - return data.slice(0, data.length - 1) - } + return data.slice(0, data.length - data[data.length - 1]) } -const binarization = ( - value: number, options?: { +class Binarization { + constructor(options?: { threshold?: number min?: number, max?: number - }) => { - options === undefined || (options = {}) - const threshold = options?.threshold ?? 150 - const min = options?.min ?? 0 - const max = options?.max ?? 255 - return value >= threshold ? max : min + }) { + options === undefined || (options = {}) + this.threshold = options?.threshold ?? 150 + this.min = options?.min ?? 0 + this.max = options?.max ?? 255 + } + private threshold: number + private min: number + private max: number + private cache = new Map() + public binarization = (value: number) => { + if (this.cache.has(value)) return this.cache.get(value)! + const res = value >= this.threshold? this.max : this.min + this.cache.set(value, res) + return res + } } // const scannerResult = new Set() @@ -73,6 +77,7 @@ const getQRDataFromVideoFrame = (videoFrame: VideoFrame): Uint8Array | undefined g: new ImageData(canvas.width, canvas.height), b: new ImageData(canvas.width, canvas.height), } + const binarization = new Binarization().binarization for (let i = 0; i < imageData.data.length; i += 4) { const rgba = { r: binarization(imageData.data[i]),