[英]Node JS and JIMP: No matching constructor overloading was
我正在尝试使用 nodejs 和 JIMP 在简单的 jpg 或 png 上写一些文本,但我在使其工作时遇到问题。 图片来自电报机器人,它与另一张带有 canvas 的图片合并,然后我必须在上面写一些简单的文字。
这是我的代码:
const Jimp = require("jimp");
var imageCaption = 'WRITE THIS ON PICTURE';
var loadedImage;
const image = await Jimp.read(finalCanvas)
.then(function (image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
})
.then(function (font) {
loadedImage.print(font, 10, 10, imageCaption)
.write(finalCanvas);
})
.catch(function (err) {
console.error(err);
});
我不断收到有关未找到匹配的构造函数重载的错误。 让 JIMP 读取我的本地文件也遇到了麻烦。
我得到完整的错误:
Error: No matching constructor overloading was found. Please see the docs for how to call the Jimp constructor.
at Jimp.throwError (/home/smp0/ifsbadge/node_modules/@jimp/utils/dist/index.js:33:13)
at new Jimp (/home/smp0/ifsbadge/node_modules/@jimp/core/dist/index.js:412:85)
at _construct (/home/smp0/ifsbadge/node_modules/@babel/runtime/helpers/construct.js:19:21)
at /home/smp0/ifsbadge/node_modules/@jimp/core/dist/index.js:926:32
at new Promise (<anonymous>)
at Function.Jimp.read (/home/smp0/ifsbadge/node_modules/@jimp/core/dist/index.js:925:10)
at TelegramBot.<anonymous> (/home/smp0/ifsbadge/index.js:51:32)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
methodName: 'constructor'
}
完整的上下文:
var needle = require('needle');
const Telegram = require('node-telegram-bot-api')
const { createCanvas, loadImage, ImageData } = require('canvas')
var Jimp = require("jimp");
var fs = require('fs');
const factions = {}
token="1234:BLABLA"
const bot = new Telegram(token, { polling: true })
bot.on('message', async (msg) => {
if (msg.photo) {
if (factions[msg.chat.id]) {
console.log(`Generating badge for ${msg.from.first_name} (${msg.from.username})...`)
bot.sendChatAction(msg.chat.id, 'upload_photo').catch(console.error)
const pictureCanvas = createCanvas(559, 772)
const pictureCtx = pictureCanvas.getContext('2d')
const { file_path } = await bot.getFile(msg.photo[msg.photo.length - 1].file_id)
const picture = await loadImage(`https://api.telegram.org/file/bot${token}/${file_path}`)
// PICTURE CALCULATIONS
pheight = picture.height
pwidth = picture.width
aspectratiow = (pwidth/pheight)
aspectratioh = (pheight/pwidth)
oheight = pheight*aspectratioh
owidth = (pwidth) / (pwidth/559)
newheight = 559*pheight/pwidth
var scale = Math.min(559/pwidth, 772/pheight);
var posx = (559 / 2) - (559 / 2) * scale;
var posy = (772 / 2) - (pheight / 2) * scale;
// END OF CALCULATIONS
// MERGING TWO PICTURES
pictureCtx.drawImage(picture, 10 , posy, 559, newheight)
const finalCanvas = createCanvas(559, 772)
const finalCtx = finalCanvas.getContext('2d')
const frame = await loadImage(`./frames/${factions[msg.chat.id]}.png`)
finalCtx.drawImage(pictureCanvas, 0, 0, 559, 772)
finalCtx.drawImage(frame, 0, 0, 559, 772)
factions[msg.chat.id] = null
// END OF MERGING PICTURES
//APPLYING TEXT ON PICTURE
const Jimp = require("jimp");
var imageCaption = 'WRITE THIS ON PICTURE';
var loadedImage;
const image = await Jimp.read(finalCanvas)
.then(function (image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
})
.then(function (font) {
loadedImage.print(font, 10, 10, imageCaption)
.write(finalCanvas);
})
.catch(function (err) {
console.error(err);
});
//END OF APPLYING TEXT ON PICTURE
bot.sendPhoto(msg.chat.id, finalCanvas.toBuffer('image/jpeg', { quality: 1 }))
} else {
bot.sendMessage(msg.chat.id, 'Write /enl1 /enl2 /enl3 o /res1 /res2 /res3 o /xf1 /xf2 !').catch(console.log)
}
}
})
bot.onText(/\/start/, async (msg) => {
bot.sendMessage(msg.chat.id, "Welcome! Select your badge Write /enl1 /enl2 /enl3 o /res1 /res2 /res3 o /xf1 /xf2 !").catch(console.log)
})
bot.onText(/\/(enl1|enl2|enl3|res1|res2|res3|xf1|xf2)/, async (msg, match) => {
factions[msg.chat.id] = match[1]
bot.sendMessage(msg.chat.id, 'Good! Now send me your picture').catch(console.log)
})
github 上的 srcs: https://github.com/pedrofracassi/badgemaker和https://github.com/Ferdin
据我所知, finalCanvas
是Canvas
的一个实例。 我认为Jimp
不会采用Canvas
的实例,但Canvas#toBuffer()
可能是您想要的,因为Jimp
会采用缓冲区: Jimp.read(finalCanvas.toBuffer())
http.get
和response.pipe(writeStream)
都是异步的。 在您尝试Jimp.read
时,该文件尚未在文件系统中,或者尚未完全写入磁盘。
要在文件写入后执行此操作,请收听finish
事件。
file.on('finish', async () => {
await Jump.read(...);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.