const sharp = require('sharp'); const Tesseract = require('tesseract.js'); const path = require('path'); /** * 处理图像并进行验证码识别 * @param {string} inputImageName - 输入图像文件名(位于 .images 文件夹内) * @param {string} outputImageName - 输出处理后图像的文件名(位于 .images 文件夹内) * @returns {Promise} - 返回识别的文本结果 */ async function processAndRecognizeCaptcha(inputImagePath, outputImagePath) { try { // 图像处理 await sharp(inputImagePath) // .sharpen() .modulate({ brightness: 1.2, // 增加亮度 contrast: 1.5, // 增强对比度 }) .resize(800) // 调整图像宽度为800像素,保持纵横比 .grayscale() // 转换为灰度图 .threshold(128) // 二值化,阈值设定为128 .toFile(outputImagePath); console.log('图像处理完成:', outputImagePath); // 图像识别 const { data: { text }, } = await Tesseract.recognize(outputImagePath, 'eng', { langPath: './tessdata', }); console.log('识别结果:', text.trim()); return text.replace(/\s+/g, '').trim(); // 返回识别的文本结果 } catch (err) { console.error('处理或识别出错:', err); throw err; // 将错误抛出以供调用者处理 } } module.exports = { processAndRecognizeCaptcha };