我正在使用以下代码搜索如何比较两个使用Selenium Java 编写的图像的代码。但是,我需要比较图像文件,如下面的方式,但在 Ruby Selenium。请引导我在 Ruby Selenium 中对getData(),getNumBands(),getWidth(),getHeight(),getSample()同等的方法?非常感谢。
try {
original = ImageIO.read(new File(
"originalFile"));
copy = ImageIO.read(new File("copyFile"));
ras1 = original.getData();
ras2 = copy.getData();
//Comparing the the two images for number of bands,width & height.
if (ras1.getNumBands() != ras2.getNumBands()
|| ras1.getWidth() != ras2.getWidth()
|| ras1.getHeight() != ras2.getHeight()) {
ret=false;
}else{
// Once the band ,width & height matches, comparing the images.
search: for (int i = 0; i < ras1.getNumBands(); ++i) {
for (int x = 0; x < ras1.getWidth(); ++x) {
for (int y = 0; y < ras1.getHeight(); ++y) {
if (ras1.getSample(x, y, i) != ras2.getSample(x, y, i)) {
// If one of the result is false setting the result as false and breaking the loop.
ret = false;
break search;
}
答案 0 :(得分:1)
您可以尝试rjb。安装带有JAVA_HOME
和LD_LIBRARY_PATH
的gem,如其主页中所述,然后您可以调用Java方法,如:
require 'rjb'
Rjb::load(classpath = '.', jvmargs=[])
JImageIO = Rjb::import('javax.imageio.ImageIO')
JFile = Rjb::import('java.io.File')
original = JImageIO.read(JFile.new('a.jpg'))
ras1 = original.getData
puts ras1.getNumBands #=> 3
puts ras1.getWidth #=> 440
puts ras1.getHeight #=> 322
puts ras1.getSample(0, 0, 0) #=> 255
您可以将脚本编写为守护程序,并通过进程通信查询图像比较结果,以避免频繁加载/卸载JVM。
或者您可以使用某些Ruby库,例如RMagick。请参阅documentation,尤其是RMagick::ImageList,RMagick::Image和RMagick::Pixel的文档。
代码可能类似于以下内容(我没有进行测试):
require 'RMagick'
original = RMagick::ImageList.new('a.jpg') # ImageList
ras1 = original[0] # Image
ras1.rows # Height in pixels
ras1.columns # Width in pixels
ras1.colorspace
ras1.matte # colorspace and matte => getNumBands
ras1[0][1].red # ras1.getSample(0, 1, 0)
ras1[0][1].green # ras1.getSample(0, 1, 1)
ras1[0][1].blue # ras1.getSample(0, 1, 2)
ras1[0][1].opacity # ras1.getSample(0, 1, 3) useless when matte is false