export const GENERAL_IMAGE_LIMITS = { maxFileSizeBytes: 5 * 1024 * 1024, acceptedTypes: ['image/jpeg', 'image/png', 'image/webp'], acceptedExtensions: ['jpg', 'jpeg', 'png', 'webp'], }; export function isGeneralImageFile(file: File): boolean { if (file.type && GENERAL_IMAGE_LIMITS.acceptedTypes.includes(file.type)) return true; const ext = file.name.split('.').pop()?.toLowerCase(); return Boolean(ext && GENERAL_IMAGE_LIMITS.acceptedExtensions.includes(ext)); } import { pickFileFromDataTransfer } from './fileDrop'; export function getImageFileFromDataTransfer(dataTransfer: DataTransfer | null): File | null { return pickFileFromDataTransfer(dataTransfer, isGeneralImageFile); } export function dataTransferHasImageFiles(dataTransfer: DataTransfer | null): boolean { if (!dataTransfer) return false; return Array.from(dataTransfer.types || []).some( (type) => type === 'Files' || type === 'application/x-moz-file' || type === 'public.file-url' || type.startsWith('image/') ); } export const ORDER_RECOGNITION_IMAGE_LIMITS = { maxFileSizeBytes: 2 * 1024 * 1024, maxDimension: 1600, quality: 0.82, acceptedTypes: ['image/jpeg', 'image/png', 'image/webp'], }; export function validateGeneralImageFile(file: File): string | null { if (!isGeneralImageFile(file)) { return '仅支持 JPG、PNG、WEBP 图片'; } if (file.size > GENERAL_IMAGE_LIMITS.maxFileSizeBytes) { return '图片不能超过 5MB'; } return null; } function loadImageFromDataUrl(dataUrl: string): Promise { return new Promise((resolve, reject) => { const image = new Image(); image.onload = () => resolve(image); image.onerror = () => reject(new Error('图片读取失败,请重试')); image.src = dataUrl; }); } function readFileAsDataUrl(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onloadend = () => resolve(String(reader.result || '')); reader.onerror = () => reject(new Error('图片读取失败,请重试')); reader.readAsDataURL(file); }); } const INSPECTION_PHOTO_JPEG_QUALITY = 0.92; export async function loadOrientedCanvasFromFile(file: File): Promise { if (typeof createImageBitmap === 'function') { try { const bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' }); try { const canvas = document.createElement('canvas'); canvas.width = bitmap.width; canvas.height = bitmap.height; const context = canvas.getContext('2d'); if (!context) throw new Error('浏览器不支持图片处理'); context.drawImage(bitmap, 0, 0); return canvas; } finally { bitmap.close(); } } catch { // fall back to Image() below } } const originalDataUrl = await readFileAsDataUrl(file); const image = await loadImageFromDataUrl(originalDataUrl); const canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; const context = canvas.getContext('2d'); if (!context) { throw new Error('浏览器不支持图片处理'); } context.drawImage(image, 0, 0); return canvas; } export async function renderInspectionPhotoWithWatermark( file: File, meta: { orderCode?: string; branchName?: string } ): Promise { const canvas = await loadOrientedCanvasFromFile(file); const context = canvas.getContext('2d'); if (!context) { throw new Error('浏览器不支持图片处理'); } const timestamp = new Date().toLocaleString(); const orderCode = String(meta.orderCode || '').trim(); const branchName = String(meta.branchName || '分公司').trim() || '分公司'; context.font = 'bold 24px Arial'; context.fillStyle = 'rgba(255, 255, 255, 0.85)'; context.fillText(`验收拍照 ${timestamp}`, 20, canvas.height - 40); if (orderCode) { context.fillText(`订单: ${orderCode}`, 20, canvas.height - 15); } context.fillText(branchName, 20, canvas.height - 65); return canvas.toDataURL('image/jpeg', INSPECTION_PHOTO_JPEG_QUALITY); } export async function compressOrderRecognitionImage(file: File): Promise { if (!isGeneralImageFile(file)) { throw new Error('仅支持 JPG、PNG、WEBP 图片'); } const originalDataUrl = await readFileAsDataUrl(file); const image = await loadImageFromDataUrl(originalDataUrl); const scale = Math.min( 1, ORDER_RECOGNITION_IMAGE_LIMITS.maxDimension / Math.max(image.width, image.height) ); const width = Math.max(1, Math.round(image.width * scale)); const height = Math.max(1, Math.round(image.height * scale)); const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; const context = canvas.getContext('2d'); if (!context) { throw new Error('浏览器不支持图片压缩,请更换浏览器或上传更小图片'); } context.drawImage(image, 0, 0, width, height); const compressed = canvas.toDataURL('image/jpeg', ORDER_RECOGNITION_IMAGE_LIMITS.quality); const estimatedBytes = Math.ceil((compressed.split(',')[1]?.length || 0) * 0.75); if (estimatedBytes > ORDER_RECOGNITION_IMAGE_LIMITS.maxFileSizeBytes) { throw new Error('图片压缩后仍超过2MB,请重新拍照或裁剪后上传'); } return compressed; }