Bitmap checkpoint in Selenium
Compare Image Files Using Selenium
Step 1: Import
Java.io.* package
We need to create File type of object, where we will open
jpg file.
Step 2: Create a
file object for both input files.
File f1 = new File("C:\\File\\168022.JPG");
File f2 = new
File("C:\\File\\168024.JPG");
Step 3: Import
java.awt.image.* package
Step 4: Create a buferedImage object and pass file object in that
File f1 = new File("C:\\File\\168022.JPG");
File
f2 = new File("C:\\File\\168024.JPG");
BufferedImage
b1 = ImageIO.read(f1);
BufferedImage
b2 = ImageIO.read(f2);
Step 5 : For the
high level of testing, we can compare height and width of both image
Use getWidth and getHeight method of bufferedImage object
file1_height=b1.getHeight();
file1_width=b1.getWidth();
file2_height=b2.getHeight();
file2_width=b2.getWidth();
if(file1_height==file2_height
&& file2_width==file1_width)
{
System.out.println("File
dimenstions are same");
}
Step 6: Now we
can find RGB value of each pixel of the file and compare with the each pixel
value of other file
getRGB will return an integer reflect t particular RGB value
if(file1_height==file2_height
&& file2_width==file1_width)
{
int i,j;
for(i=1;i<=file1_height;i++)
{
for(j=1;j<=file1_width;j++)
{
if(b1.getRGB(i,
j)!= b2.getRGB(i, j))
{
System.out.println("NOT
SAME");
break;
}
}
}
package org.nitin;
import java.io.*;
import
java.awt.image.*;
import
javax.imageio.ImageIO;
public class LearnString {
public static void main(String
aa[])
{
int file1_height,
file1_width,file2_height, file2_width;
try
{
File
f1 = new File("C:\\File\\168022.JPG");
File
f2 = new File("C:\\File\\168023.JPG");
BufferedImage
b1 = ImageIO.read(f1);
BufferedImage
b2 = ImageIO.read(f2);
file1_height=b1.getHeight();
file1_width=b1.getWidth();
file2_height=b2.getHeight();
file2_width=b2.getWidth();
if(file1_height==file2_height
&& file2_width==file1_width)
{
int i,j;
for(i=1;i<=file1_height;i++)
{
for(j=1;j<=file1_width;j++)
{
if(b1.getRGB(i,
j)!= b2.getRGB(i, j))
{
System.out.println("NOT
SAME");
break;
}
}
}
}
} catch(Exception ex){}
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment