1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| const { createWorker } = require('tesseract.js'); const fs = require('fs');
async function ocrImage(imagePath) { const worker = createWorker();
await worker.load(); await worker.loadLanguage('eng'); await worker.initialize('eng');
const { data: { text } } = await worker.recognize(imagePath); await worker.terminate();
return text; }
function saveToTxt(text, outputPath) { fs.writeFileSync(outputPath, text); console.log('OCR 结果已保存到:', outputPath); }
const imagePath = 'path/to/image.jpg';
const outputPath = 'path/to/output.txt';
ocrImage(imagePath) .then(text => saveToTxt(text, outputPath)) .catch(error => console.log('发生错误:', error));
|