【问题标题】:How to match two images in JavaScript? [duplicate]如何在 JavaScript 中匹配两个图像? [复制]
【发布时间】:2017-05-09 00:50:08
【问题描述】:

我想将两个图像相互匹配,如果它们匹配,那么结果将是true。如果不是,那么它将返回false。但我想要它在 JavaScript 中。

【问题讨论】:

  • 你想匹配什么?名字?内容?

标签: javascript html image cordova


【解决方案1】:

您可以通过将图像转换为base64字符串来检查

function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;

// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);

// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to
// guess the original format, but be aware the using "image/jpg"
// will re-encode the image.
var dataURL = canvas.toDataURL("image/png");

return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

然后

var a = new Image(),
    b = new Image();
a.src = url_a;
b.src = url_b;

var a_base64 = getBase64Image(a),
    b_base64 = getBase64Image(b);

if (a_base64 === b_base64)
{
    // they are identical
}
else
{
    // you can probably guess what this means
}

您可以查看this link了解更多信息。

【讨论】:

  • 这些链接已经作为参考给出。 @NoDownvotesPlz
  • 结合两个答案没有错,但你应该添加一些自己的东西
  • 你的代码也不起作用..jsfiddle.net/naeemshaikh27/hgs41ctd
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 2019-06-18
  • 2014-05-09
  • 2014-07-19
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
相关资源
最近更新 更多