MCPcopy Create free account
hub / github.com/32comic/image2pixel-web / autoDetectAndSuggest

Method autoDetectAndSuggest

pixelizer.js:226–264  ·  view source on GitHub ↗

* 自动检测像素块大小并给出建议

(img)

Source from the content-addressed store, hash-verified

224 * 自动检测像素块大小并给出建议
225 */
226 async autoDetectAndSuggest(img) {
227 try {
228 // 创建临时canvas进行分析
229 const canvas = document.createElement('canvas');
230 const ctx = canvas.getContext('2d');
231
232 // 限制分析尺寸以提高性能
233 const maxAnalysisSize = 800;
234 const scale = Math.min(1, maxAnalysisSize / Math.max(img.width, img.height));
235
236 canvas.width = Math.floor(img.width * scale);
237 canvas.height = Math.floor(img.height * scale);
238
239 ctx.imageSmoothingEnabled = false;
240 ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
241
242 const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
243 const detection = this.detectPixelBlockSize(imageData);
244
245 console.log('Detection result:', detection);
246
247 // 根据检测结果设置建议值
248 if (detection.confidence > 0.7 && detection.blockSize > 1) {
249 // 高置信度,使用自动识别模式
250 document.querySelector('input[name="detectionMode"][value="auto"]').checked = true;
251 this.showDetectionInfo(detection);
252 } else {
253 // 低置信度,切换到手动模式并给出建议
254 document.querySelector('input[name="detectionMode"][value="manual"]').checked = true;
255 const suggestedWidth = this.calculateSuggestedWidth(imageData);
256 document.getElementById('targetWidth').value = suggestedWidth;
257 document.getElementById('targetWidthInput').value = suggestedWidth;
258 this.handleModeChange({ target: { value: 'manual' } });
259 }
260
261 } catch (error) {
262 console.error('Auto detection failed:', error);
263 }
264 }
265
266 /**
267 * 检测像素块大小

Callers 1

loadImageMethod · 0.95

Calls 4

detectPixelBlockSizeMethod · 0.95
showDetectionInfoMethod · 0.95
handleModeChangeMethod · 0.95

Tested by

no test coverage detected