0

我正在尝试用印地语写在图像上。我正在使用节点画布库。我的输出有问题。有人能帮我吗 ?

const { createCanvas, loadImage, registerFont} = require('canvas')
const canvas = createCanvas(400, 400)
const ctx = canvas.getContext('2d')

var str= "यह.  मिसिसिपी   है";
console.log(str);
loadImage('missisippi.jpg').then((image) => {
  console.log(image);
  ctx.drawImage(image, 0 , 0, 400, 400);
  ctx.fillText(str,100,40);
  var body = canvas.toDataURL(),
  base64Data = body.replace(/^data:image\/png;base64,/,""),
  binaryData = new Buffer(base64Data, 'base64').toString('binary');

    require("fs").writeFile("out.png", binaryData, "binary", function(err) {
      console.log(err); // writes out file without error, but it's not a valid image
    })

    // console.log('<img src="' + canvas.toDataURL() + '" />')
})

这是输出图像。您可以看到 मिसिसिपी 在语法上是错误的。(如果你熟悉印地语。

我也用 npm-gm 尝试过同样的事情。在那我也面临同样的问题。有人可以帮我解决这个问题吗?

如何使用自定义字体在图像上书写文字?

4

1 回答 1

4

以下对我来说很好 -

var fs = require('fs')
var path = require('path')
var Canvas = require('canvas')

function fontFile (name) {
  return path.join(__dirname, '/fonts/', name)
}

Canvas.registerFont(fontFile('ARBLI___0.ttf'), {family: 'ARBLI___0'})

var canvas = Canvas.createCanvas(7100, 3500)
var ctx = canvas.getContext('2d')

var Image = Canvas.Image;
var img = new Image();
img.src = 'input/DOIT_Art_Size.jpg';

ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

ctx.fillStyle = 'white';
ctx.textAlign = 'center';

ctx.font = '150pt ARBLI___0'
ctx.fillText('यह मिसिसिपी है',3900, 1700)

canvas.createPNGStream().pipe(fs.createWriteStream(path.join(__dirname, 'output/font.png')))
canvas.createJPEGStream().pipe(fs.createWriteStream(path.join(__dirname, 'output/font.jpg')))

我的节点版本是 6.11.2。画布模块是 2.0.0-alpha.16。我的输入图像尺寸是 7100*3500 像素。

于 2018-12-03T10:18:51.723 回答