【发布时间】:2017-05-09 00:50:08
【问题描述】:
我想将两个图像相互匹配,如果它们匹配,那么结果将是true。如果不是,那么它将返回false。但我想要它在 JavaScript 中。
【问题讨论】:
-
你想匹配什么?名字?内容?
标签: javascript html image cordova
我想将两个图像相互匹配,如果它们匹配,那么结果将是true。如果不是,那么它将返回false。但我想要它在 JavaScript 中。
【问题讨论】:
标签: javascript html image cordova
您可以通过将图像转换为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了解更多信息。
【讨论】: