This repository has been archived on 2025-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
playwright-demo/tests/utils/helper.js
2024-10-21 18:03:30 +08:00

43 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const sharp = require('sharp');
const Tesseract = require('tesseract.js');
const path = require('path');
/**
* 处理图像并进行验证码识别
* @param {string} inputImageName - 输入图像文件名(位于 .images 文件夹内)
* @param {string} outputImageName - 输出处理后图像的文件名(位于 .images 文件夹内)
* @returns {Promise<string>} - 返回识别的文本结果
*/
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 };