Friday, February 7, 2014

Selenium : Compare 2 Images in Java

Compare 2 Images in Java

CODE HERE  (Simple method to compare 2 images )


 public static boolean Compareimages(String file1,String file2) {
           
            try{
                    File file_1 = new File(file1);
                    File file_2 = new File(file2);
       
                    BufferedImage bufile_1 = ImageIO.read(file_1);
                    DataBuffer dafile_1 = bufile_1.getData().getDataBuffer();
                    int sizefile_1 = dafile_1.getSize();           
                   
                   
                    BufferedImage bufile_2 = ImageIO.read(file_2);
                    DataBuffer dafile_2 = bufile_2.getData().getDataBuffer();
                    int sizefile_2 = dafile_2.getSize();
                   
                    Boolean bFlag = true;
                    System.out.println(bFlag);
                    if(sizefile_1 == sizefile_2) {
                        System.out.println("Size match successfull");
                       for(int j=0; j<sizefile_1; j++) {
                             if(dafile_1.getElem(j) != dafile_2.getElem(j)) {
                                   bFlag = false;
                                   System.out.println("Image comparsion fail !!  :-(");
                                   break;
                             }
                        }
                    }
                    if (bFlag==true)
                        System.out.println("Image comparsion success !! :-) ");      
                        return bFlag;
                       
            }catch(Throwable t){
                    System.out.println("File not found\n"+t.getMessage());
                    Assert.assertTrue(false);
                    return false;
            }
           
        }
    

No comments:

Post a Comment