'use strict'; import express from 'express'; import fetch from 'node-fetch'; import sharp from 'sharp'; import Lens from 'chrome-lens-ocr'; // Constants const PORT = 7860; const HOST = '0.0.0.0'; // App const app = express(); const lens = new Lens(); app.get('/', (req, res) => { res.send('Hello World from ExpressJS! This example is from the NodeJS Docs: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/'); }); app.get('/scanByUrl', async (req, res) => { const { url } = req.query; if (!url) { return res.status(400).json({ error: 'Image URL is required' }); } try { // Fetch the image const response = await fetch(url); if (!response.ok) throw new Error('Failed to fetch image'); const buffer = await response.arrayBuffer(); const imageBuffer = Buffer.from(buffer); // Get image metadata const metadata = await sharp(imageBuffer).metadata(); // Resize if necessary let processedImage = imageBuffer; if (metadata.width > 1000 || metadata.height > 1000) { processedImage = await sharp(imageBuffer) .resize(1000, 1000, { fit: 'inside' }) .toBuffer(); } // Scan with Chrome Lens OCR const data = await lens.scanByBuffer(processedImage); const combinedText = data.segments.map(segment => segment.text).join('\n\n'); res.json({ combinedText, detailedData: data }); } catch (error) { res.status(500).json({ error: error.message }); } }); app.listen(PORT, HOST, () => { console.log(`Running on http://${HOST}:${PORT}`); });