簡體   English   中英

Node JS 和 JIMP:沒有匹配的構造函數重載

[英]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/badgemakerhttps://github.com/Ferdin

據我所知, finalCanvasCanvas的一個實例。 我認為Jimp不會采用Canvas的實例,但Canvas#toBuffer()可能是您想要的,因為Jimp會采用緩沖區: Jimp.read(finalCanvas.toBuffer())


http.getresponse.pipe(writeStream)都是異步的。 在您嘗試Jimp.read時,該文件尚未在文件系統中,或者尚未完全寫入磁盤。

要在文件寫入后執行此操作,請收聽finish事件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM