图片没有方法' getContext'

时间:2014-06-03 21:13:14

标签: node.js canvas

我遇到了问题,我想可以从一些丢失的库中制作出来。我构建了一个能够使用https://github.com/orls/ccv-purejs

检测面部的简单函数

当我尝试处理照片时出现此错误:

/home/admuser/foodstagram/node_modules/face-detect/lib/ccv.js:20
    var ctx = canvas.getContext("2d");
                     ^
TypeError: Object #<Image> has no method 'getContext'
    at Object.module.exports.exports.grayscale (/home/admuser/foodstagram/node_m
odules/face-detect/lib/ccv.js:20:22)
    at Object.module.exports.exports.detect_objects (/home/admuser/foodstagram/n
ode_modules/face-detect/lib/ccv.js:108:27)
    at findFaces (/home/admuser/foodstagram/index.js:501:30)
    at null._onTimeout (/home/admuser/foodstagram/index.js:466:30)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

这是我的代码

var Image = require('canvas').Image;
function detectFace(){
  urlPhoto = "http://origincache-prn.fbcdn.net/10358219_569704703128581_477566036_a.jpg"
  request.get({ url: urlPhoto, encoding: null}, function(err, res, body) {
    if (err) throw err;
    var image = new Image();
    image.onerror = function() {
        console.error(arguments);
    };

    image.onload = function(){
      console.log('loaded image');
      findFaces(image);
    };

    image.src = new Buffer(body, 'base64');
  });

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要创建一个canvas元素,然后将该图像添加到该画布并将画布传递给库。

<强>供参考:
此链接是否创建画布
https://www.npmjs.org/package/canvas

另一个是将图像添加到画布上http://www.w3schools.com/tags/canvas_drawimage.asp