可以在JavaScript中比较两个图片吗? [英] Possible to compare two images in Javascript?

查看:95
本文介绍了可以在JavaScript中比较两个图片吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以比较两个文件名不同的图像文件?所以我想看看它们是否与javascript相等.这可能吗?

Is there a way to compare two image files that have different filenames? So I am looking to see if they are equal with javascript. Is this possible?

此用途: 我有一个需要在灯箱中弹出的图像库.灯箱打开时,我将挂接到回调中以检查是否有重复的图像,找到后将其从灯箱库中删除,以使用户看不到重复的图像.

The use for this: I have a gallery of images that needs to pop up in the lightbox. When the lightbox opens, I am hooking into the callback to check for the duplicate images and when found, remove them from the lightbox gallery so users are not seeing repeated images.

推荐答案

我想你可以.伪代码:

  • 检查图像的宽度和高度是否相同.如果不是,则它们不能相同.
  • 创建一个画布,使其同时适合两个图像.
  • 在画布上绘制图像.
  • 将它们逐像素进行比较.可以很容易地使用从从HTML Canvas获得的getPixel借来的代码来完成?
  • 请记住,一旦单个像素不匹配,就会中断循环.
  • 您的最坏情况下的运行时间将是O(wh),这在图像实际相等时发生.
  • Check if the images have the same width and height. If not, they can't be the same.
  • Create a canvas that will fit both images side-by-side.
  • Draw the images on the canvas.
  • Compare them pixel-for-pixel. This can be done fairly easily using code borrowed from getPixel from HTML Canvas?
  • Remember to break out of loops as soon as a single pixel doesn't match.
  • Your worst-case runtime will be O(wh), which occurs when the images are actually equal.

这篇关于可以在JavaScript中比较两个图片吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆