Java中的一些方法在Selenium Ruby中同样适用

Java中的一些方法在Selenium Ruby中同样适用,ruby,selenium,webdriver,Ruby,Selenium,Webdriver,我只是在谷歌上搜索下面的代码,了解如何比较为使用SeleniumJava编写的两个图像。但是,我需要像下面那样比较图像文件,但是在RubySelenium中。请给我介绍一些方法,这些方法同样适用于Ruby Selenium中的getData()、GetNumands()、getWidth()、getHeight()、getSample()?多谢 try { original = ImageIO.read(new File( "originalFile")); copy =

我只是在谷歌上搜索下面的代码,了解如何比较为使用SeleniumJava编写的两个图像。但是,我需要像下面那样比较图像文件,但是在RubySelenium中。请给我介绍一些方法,这些方法同样适用于Ruby Selenium中的getData()、GetNumands()、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;
      }
试试看{
原始=ImageIO.read(新文件(
“原始文件”);
copy=ImageIO.read(新文件(“copyFile”);
ras1=原始的.getData();
ras2=copy.getData();
//比较两幅图像的频带数、宽度和高度。
if(ras1.getNumBands()!=ras2.getNumBands()
||ras1.getWidth()!=ras2.getWidth()
||ras1.getHeight()!=ras2.getHeight()){
ret=假;
}否则{
//一旦波段、宽度和高度匹配,比较图像。
搜索:查找(int i=0;i
您可以试一试。安装gem时,按照主页中的说明设置
JAVA\u HOME
LD\u LIBRARY\u PATH
,然后您可以调用如下JAVA方法:

需要“rjb”
Rjb::load(类路径='.',jvmargs=[])
JImageIO=Rjb::import('javax.imageio.imageio')
JFile=Rjb::import('java.io.File')
original=JImageIO.read(JFile.new('a.jpg'))
ras1=original.getData
放置ras1.GetNumberands#=>3
放置ras1.getWidth#=>440
放置ras1.getHeight#=>322
放置ras1.getSample(0,0,0)#=>255
您可以将脚本作为守护进程编写,并通过进程通信查询图像比较结果,以避免频繁加载/卸载JVM

或者您可以使用一些Ruby库,例如。请参阅,尤其是和的文档

代码可能如下所示(我没有进行测试):

需要“RMagick”
original=RMagick::ImageList.new('a.jpg')#ImageList
ras1=原始[0]#图像
ras1.rows#像素高度
ras1.1列#像素宽度
ras1.1色空间
ras1.matte#颜色空间和matte=>GetNumberands
ras1[0][1]。红色#ras1.getSample(0,1,0)
ras1[0][1]。绿色#ras1.getSample(0,1,1)
ras1[0][1]。蓝色#ras1.getSample(0,1,2)
ras1[0][1]。不透明度#ras1.getSample(0,1,3)在蒙版为false时无效

您的回答非常充分,帮助我获得更多有用的知识。非常感谢。