feat: 一些细枝末节的优化

This commit is contained in:
539943419 committed 2025-01-28 10:16:30 +08:00
1 parent 0ba17ace6f
commit 89c138cedf
2 files changed
+21 -22

No files matched your search

+1 -7
View File
@@ -30,13 +30,7 @@ const generateQRCodeCanvasData = (data: Uint8Array) => {
} }
const paddingData = (data: Uint8Array) => { const paddingData = (data: Uint8Array) => {
if(data.length % 3 === 0) { return new Uint8Array([...data, ...new Array(3 - data.length % 3).fill(0).map((_, i) => i)])
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])
}
} }
onMounted(() => { onMounted(() => {
+20 -15
View File
@@ -35,26 +35,30 @@ const u8iEndWith = (data: Uint8Array, target: number[]) => {
} }
const unpaddingData = (data: Uint8Array): Uint8Array => { const unpaddingData = (data: Uint8Array): Uint8Array => {
if(u8iEndWith(data, [0, 1, 2])) { return data.slice(0, data.length - data[data.length - 1])
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)
}
} }
const binarization = ( class Binarization {
value: number, options?: { constructor(options?: {
threshold?: number threshold?: number
min?: number, min?: number,
max?: number max?: number
}) => { }) {
options === undefined || (options = {}) options === undefined || (options = {})
const threshold = options?.threshold ?? 150 this.threshold = options?.threshold ?? 150
const min = options?.min ?? 0 this.min = options?.min ?? 0
const max = options?.max ?? 255 this.max = options?.max ?? 255
return value >= threshold ? max : min }
private threshold: number
private min: number
private max: number
private cache = new Map<number, number>()
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() // const scannerResult = new Set()
@@ -73,6 +77,7 @@ const getQRDataFromVideoFrame = (videoFrame: VideoFrame): Uint8Array | undefined
g: new ImageData(canvas.width, canvas.height), g: new ImageData(canvas.width, canvas.height),
b: 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) { for (let i = 0; i < imageData.data.length; i += 4) {
const rgba = { const rgba = {
r: binarization(imageData.data[i]), r: binarization(imageData.data[i]),