微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node JS 和 JIMP:没有匹配的构造函数重载

如何解决Node JS 和 JIMP:没有匹配的构造函数重载

我正在尝试使用 nodejs 和 JIMP 在简单的 jpg 或 png 上写一些文本,但我在使其工作时遇到问题。 图片来自电报机器人,它与另一张带有画布的图片合并,然后我必须在上面写一些简单的文字

这是我的代码

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,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,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,772)
      finalCtx.drawImage(frame,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,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 上的源代码https://github.com/pedrofracassi/badgemakerhttps://github.com/FerdinandoLM/IngressFSBadgeMaker

解决方法

据我所知,finalCanvasCanvas 的一个实例。我不认为 Jimp 会采用 Canvas 的实例,但 Canvas#toBuffer() 可能是您想要的东西,因为 Jimp 会采用缓冲区:Jimp.read(finalCanvas.toBuffer())


http.getresponse.pipe(writeStream) 都是异步的。在您尝试 Jimp.read 时,该文件尚未在文件系统中或尚未完全写入磁盘。

要在写入文件后执行此操作,请收听 finish 事件。

file.on('finish',async () => {
  await Jump.read(...);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。