使用自定义语言使用npm节点画布在图像上写文本 [英] writing text on image with npm node-canvas in custom language

查看:63
本文介绍了使用自定义语言使用npm节点画布在图像上写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write on image with hindi language. I am using node-canvas library. There is some problem with my output. Can someone help me ?

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() + '" />')
})

这是输出图像.您可以看到मिसिसिपी在语法上是错误的.(以防您熟悉北印度语.

This is the output image . You can see that मिसिसिपी is grammatically wrong. (In case u are familiar with Hindi.

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

I have also tried the very same thing with npm-gm. in that too I faced same issue. Can someone help me out in this issue ?

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

How can I write text on image with custom font ?

推荐答案

以下内容对我来说很好-

The following is working fine for me -

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像素.

My node version is 6.11.2. Canvas module is 2.0.0-alpha.16. My input image dimension is 7100*3500 pixels.

这篇关于使用自定义语言使用npm节点画布在图像上写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆