id-scanner-lib 1.6.6 → 1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1722,17 +1722,32 @@ class ImageProcessor {
|
|
|
1722
1722
|
outputData[pos + 3] = data[pos + 3]; // 保持透明度不变
|
|
1723
1723
|
}
|
|
1724
1724
|
}
|
|
1725
|
-
//
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1725
|
+
// 处理边缘像素(仅遍历四条边,而非全图 O(width×height) → O(width+height))
|
|
1726
|
+
// 上边 + 下边
|
|
1727
|
+
for (let x = 0; x < width; x++) {
|
|
1728
|
+
const topPos = x * 4;
|
|
1729
|
+
const bottomPos = ((height - 1) * width + x) * 4;
|
|
1730
|
+
outputData[topPos] = data[topPos];
|
|
1731
|
+
outputData[topPos + 1] = data[topPos + 1];
|
|
1732
|
+
outputData[topPos + 2] = data[topPos + 2];
|
|
1733
|
+
outputData[topPos + 3] = data[topPos + 3];
|
|
1734
|
+
outputData[bottomPos] = data[bottomPos];
|
|
1735
|
+
outputData[bottomPos + 1] = data[bottomPos + 1];
|
|
1736
|
+
outputData[bottomPos + 2] = data[bottomPos + 2];
|
|
1737
|
+
outputData[bottomPos + 3] = data[bottomPos + 3];
|
|
1738
|
+
}
|
|
1739
|
+
// 左边 + 右边(排除四角,它们已在上下一行处理)
|
|
1740
|
+
for (let y = 1; y < height - 1; y++) {
|
|
1741
|
+
const leftPos = y * width * 4;
|
|
1742
|
+
const rightPos = (y * width + width - 1) * 4;
|
|
1743
|
+
outputData[leftPos] = data[leftPos];
|
|
1744
|
+
outputData[leftPos + 1] = data[leftPos + 1];
|
|
1745
|
+
outputData[leftPos + 2] = data[leftPos + 2];
|
|
1746
|
+
outputData[leftPos + 3] = data[leftPos + 3];
|
|
1747
|
+
outputData[rightPos] = data[rightPos];
|
|
1748
|
+
outputData[rightPos + 1] = data[rightPos + 1];
|
|
1749
|
+
outputData[rightPos + 2] = data[rightPos + 2];
|
|
1750
|
+
outputData[rightPos + 3] = data[rightPos + 3];
|
|
1736
1751
|
}
|
|
1737
1752
|
// 创建新的ImageData对象
|
|
1738
1753
|
return new ImageData(outputData, width, height);
|
|
@@ -1745,11 +1760,10 @@ class ImageProcessor {
|
|
|
1745
1760
|
* @returns 处理后的图像数据
|
|
1746
1761
|
*/
|
|
1747
1762
|
static threshold(imageData, threshold = 128) {
|
|
1748
|
-
//
|
|
1749
|
-
const grayscaleImage = this.toGrayscale(
|
|
1763
|
+
// 先转换为灰度图(toGrayscale 内部已创建新 ImageData,无需外部拷贝)
|
|
1764
|
+
const grayscaleImage = this.toGrayscale(imageData);
|
|
1750
1765
|
const data = grayscaleImage.data;
|
|
1751
|
-
|
|
1752
|
-
for (let i = 0; i < length; i += 4) {
|
|
1766
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
1753
1767
|
// 二值化处理
|
|
1754
1768
|
const value = data[i] < threshold ? 0 : 255;
|
|
1755
1769
|
data[i] = data[i + 1] = data[i + 2] = value;
|
|
@@ -1763,8 +1777,8 @@ class ImageProcessor {
|
|
|
1763
1777
|
* @returns 二值化后的图像数据
|
|
1764
1778
|
*/
|
|
1765
1779
|
static toBinaryImage(imageData) {
|
|
1766
|
-
//
|
|
1767
|
-
const grayscaleImage = this.toGrayscale(
|
|
1780
|
+
// 先转换为灰度图(toGrayscale 内部已创建新 ImageData,无需外部拷贝)
|
|
1781
|
+
const grayscaleImage = this.toGrayscale(imageData);
|
|
1768
1782
|
// 使用OTSU算法自动确定阈值
|
|
1769
1783
|
const threshold = this.getOtsuThreshold(grayscaleImage);
|
|
1770
1784
|
return this.threshold(grayscaleImage, threshold);
|
|
@@ -1777,8 +1791,9 @@ class ImageProcessor {
|
|
|
1777
1791
|
*/
|
|
1778
1792
|
static getOtsuThreshold(imageData) {
|
|
1779
1793
|
const data = imageData.data;
|
|
1780
|
-
|
|
1781
|
-
|
|
1794
|
+
// 使用 Uint8Array 替代 Array<number>,避免 boxing 开销,提升直方图统计性能
|
|
1795
|
+
const histogram = new Uint32Array(256);
|
|
1796
|
+
// 统计灰度直方图(每4字节取R通道,即灰度值)
|
|
1782
1797
|
for (let i = 0; i < data.length; i += 4) {
|
|
1783
1798
|
histogram[data[i]]++;
|
|
1784
1799
|
}
|