我在本地目录中安装了node-canvas和imagediff,并编写了一个比较两个已存在的图像文件的测试示例。它运行得很好,代码如下:
var Canvas = require('canvas')
, imagediff = require('../lib/imagediff.js')
, fs = require('fs')
, path = require('path');
var goodFilePath = path.join(__dirname + '/../diffs/single/test_1_good.png')
, newFilePath = path.join(__dirname + '/../diffs/single/test_1_new.png')
, diffFilePath = path.join(__dirname + '/../diffs/single/test_1_diff.png');
var test = function () {
var img_good = new Canvas.Image()
, img_new = new Canvas.Image()
, img_good_data = null
, img_new_data = null
, tolerance = 10
, equal = false
, diff_result = null;
fs.readFile(goodFilePath, function (err, squid_good) {
if (err) throw err;
img_good.src = squid_good;
});
fs.readFile(newFilePath, function (err, squid_new) {
if (err) throw err;
img_new.src = squid_new;
});
img_new.onload = function () {
// convert to image data
img_good_data = imagediff.toImageData(img_good);
img_new_data = imagediff.toImageData(img_new);
//compare and save diffences in new image file
equal = imagediff.equal(img_good, img_new, tolerance);
console.log('Image Good === Image New: ' + equal);
diff_result = imagediff.diff(img_good_data, img_new_data, tolerance);
imagediff.imageDataToPNG(diff_result, diffFilePath);
}
}
test();
但如果您注意到顶部的require语句,我会将实际的imagediff.js文件拉入我创建的lib文件夹中。我的问题是,当我尝试使用常规require(' imagediff')语句时,程序会给出以下Image或Canvas预期错误:
/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
context.drawImage(image, 0, 0);
^
TypeError: Image or Canvas expected
at toImageDataFromImage (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120:13)
at toImageData (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:108:35)
at Object.imagediff.toImageData (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:365:14)
at img_new.onload (/Users/willko/Desktop/DEBUGGING_IMGDIFF/specs/simple.js:30:45)
at Image.inspect [as src] (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/canvas/lib/image.js:29:17)
at /Users/willko/Desktop/DEBUGGING_IMGDIFF/specs/simple.js:25:29
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
我甚至尝试安装与imagediff使用相同版本的画布,导致以下seg错误错误:
/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
context.drawImage(image, 0, 0);
^
[1] 34636 segmentation fault node simple.js
为什么我不能按正常要求使用此模块?或者为什么它使用文件路径require语句但是否则失败?