nv-img-barcode-cmmn 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/index.js +0 -129
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -80,138 +80,9 @@ const normalize_runs_to_modules = (runs) => {
80
80
  };
81
81
 
82
82
 
83
- /**
84
- * transfer_ab_to_png_ab
85
- * @param {ArrayBuffer} ab - 输入数据
86
- * @param {number} moduleWidth - 每个最小 module 的像素宽度,默认 2
87
- * @param {number} height - 条形码高度,默认 50px
88
- * @returns {Promise<ArrayBuffer>} - PNG 的 ArrayBuffer
89
- */
90
- const transfer_ab_to_png_ab = async (ab, moduleWidth = 2, height = 50) => {
91
- const bytes = new Uint8Array(ab);
92
-
93
- // 1️⃣ bytes -> Code128 modules
94
- const modules = bytes_to_code128(bytes);
95
-
96
- // 2️⃣ Canvas 尺寸
97
- const canvasWidth = modules.reduce((acc, m) => acc + m * moduleWidth, 0);
98
- const canvas = document.createElement("canvas");
99
- canvas.width = canvasWidth;
100
- canvas.height = height;
101
- const ctx = canvas.getContext("2d");
102
-
103
- // 3️⃣ 绘制条形码
104
- let x = 0;
105
- modules.forEach((m, idx) => {
106
- ctx.fillStyle = idx % 2 === 0 ? "black" : "white"; // 黑白交替
107
- ctx.fillRect(x, 0, m * moduleWidth, height);
108
- x += m * moduleWidth;
109
- });
110
-
111
- // 4️⃣ Canvas -> Blob -> ArrayBuffer
112
- return new Promise((resolve, reject) => {
113
- canvas.toBlob((blob) => {
114
- if (!blob) return reject(new Error("Canvas to Blob failed"));
115
- const reader = new FileReader();
116
- reader.onload = () => resolve(reader.result);
117
- reader.onerror = reject;
118
- reader.readAsArrayBuffer(blob);
119
- }, "image/png");
120
- });
121
- };
122
-
123
- /**
124
- * png_ab_to_transfer_ab
125
- * @param {ArrayBuffer} pngAb - PNG 数据
126
- * @param {number} moduleWidthEstimate - 用于初步估算模块宽度,默认 2
127
- * @returns {Promise<ArrayBuffer>} - 原始 ArrayBuffer
128
- */
129
- const png_ab_to_transfer_ab = async (pngAb, moduleWidthEstimate = 2) => {
130
- // 1️⃣ ArrayBuffer -> Blob -> Image
131
- const blob = new Blob([pngAb], { type: "image/png" });
132
- const url = URL.createObjectURL(blob);
133
-
134
- const img = new Image();
135
- img.src = url;
136
- await img.decode();
137
-
138
- // 2️⃣ Image -> Canvas
139
- const canvas = document.createElement("canvas");
140
- canvas.width = img.width;
141
- canvas.height = img.height;
142
- const ctx = canvas.getContext("2d");
143
- ctx.drawImage(img, 0, 0);
144
-
145
- // 3️⃣ 读取顶部一行像素
146
- const imageData = ctx.getImageData(0, 0, canvas.width, 1).data;
147
- const runs = [];
148
- let lastBW = null;
149
- let count = 0;
150
-
151
- for (let x = 0; x < canvas.width; x++) {
152
- const i = x * 4;
153
- const r = imageData[i];
154
- const g = imageData[i + 1];
155
- const b = imageData[i + 2];
156
- const bw = (r + g + b) / 3 < 128 ? 1 : 0; // 黑 = 1, 白 = 0
157
-
158
- if (lastBW === null || bw === lastBW) {
159
- count++;
160
- } else {
161
- runs.push(count);
162
- count = 1;
163
- }
164
- lastBW = bw;
165
- }
166
- runs.push(count);
167
-
168
- // 4️⃣ run-length -> modules
169
- const normalize_runs_to_modules = (runs) => {
170
- const min = Math.min(...runs);
171
- return runs.map(r => Math.max(1, Math.round(r / min)));
172
- };
173
- const modules = normalize_runs_to_modules(runs);
174
-
175
- // 5️⃣ modules -> bytes -> ArrayBuffer
176
- const bytes = code128_to_bytes(modules);
177
-
178
- URL.revokeObjectURL(url); // 清理
179
-
180
- return bytes.buffer;
181
- };
182
-
183
- /**
184
- * str2png
185
- * @param {string} s - 可读文本
186
- * @param {number} moduleWidth - 每个 module 的像素宽度
187
- * @param {number} height - 条形码高度
188
- * @returns {Promise<ArrayBuffer>} - PNG 数据
189
- */
190
- const str2png = async (s, moduleWidth = 2, height = 50) => {
191
- // 将字符串编码为 UTF-8 bytes
192
- const encoder = new TextEncoder(); // 浏览器内置 API
193
- const ab = encoder.encode(s).buffer; // Uint8Array -> ArrayBuffer
194
- return transfer_ab_to_png_ab(ab, moduleWidth, height);
195
- };
196
-
197
- /**
198
- * png2str
199
- * @param {ArrayBuffer} pngAb - PNG 数据
200
- * @param {number} moduleWidthEstimate - 用于初步估算模块宽度
201
- * @returns {Promise<string>} - 解码后的文本
202
- */
203
- const png2str = async (pngAb, moduleWidthEstimate = 2) => {
204
- const ab = await png_ab_to_transfer_ab(pngAb, moduleWidthEstimate); // PNG -> ArrayBuffer
205
- const decoder = new TextDecoder(); // 默认 utf-8
206
- return decoder.decode(ab);
207
- };
208
83
 
209
84
  module.exports = {
210
85
  bytes_to_code128,
211
86
  code128_to_bytes,
212
87
  normalize_runs_to_modules,
213
- transfer_ab_to_png_ab,
214
- png_ab_to_transfer_ab,
215
- str2png,
216
- png2str,
217
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-img-barcode-cmmn",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"