我在本地目录中安装了 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 使用的相同版本的画布,结果导致以下段错误错误:
/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
context.drawImage(image, 0, 0);
^
[1] 34636 segmentation fault node simple.js
我的问题:
为什么我不能将此模块与正常要求一起使用?或者为什么它使用文件路径 require 语句但否则失败?