import Tesseract from 'tesseract.js' import path from 'path' export default defineEventHandler(async (event) => { const body = await readBody(event) const imageData = body?.image if (!imageData) { throw createError({ statusCode: 400, statusMessage: 'Image data is required' }) } // Validate base64 image format if (!imageData.startsWith('data:image/')) { throw createError({ statusCode: 400, statusMessage: 'Invalid image format. Expected base64 data URL.' }) } try { // Configure tesseract to use node_modules directly (no copying needed) const workerPath = path.resolve(process.cwd(), 'node_modules/tesseract.js/dist/worker.min.js') const corePath = path.resolve(process.cwd(), 'node_modules/tesseract.js-core') // Process OCR on server with better performance const result = await Tesseract.recognize(imageData, 'deu+eng', { workerPath, corePath, // Server-side processing - no logger needed }) const text = result.data.text || '' // Split into lines and filter empty/short ones const lines = text.split('\n') .map(line => line.trim()) .filter(line => line.length > 2) if (lines.length === 0) { return { success: false, message: 'No text detected in image', lines: [] } } return { success: true, lines: lines, fullText: text, confidence: result.data.confidence || 0 } } catch (error: any) { console.error('OCR processing error:', error) throw createError({ statusCode: 500, statusMessage: 'OCR processing failed: ' + (error?.message || 'Unknown error') }) } })