问题
I am trying to develop a program which would take the snapshot of webpage and will then compare it to an old one and highlight the changes if any.
I am using Selenium- WebDriver for taking snapshots. For image processing and compare, after a bit googling I found jMagick, a Java interface for ImageMagick, which I think would be best fit for my requirement.
But due to lack of proper documentation, I am not able to find anything relevant. If anyone could help me out with any code sample for image comparision that would be really helpful.
Again, the problem is to compare two images and highlight the changes or the differences between the two. Output could be a third image with differences highlighted.
Possible duplicates for this are : this and this . But again, no proper solution could be infer from these.
Please if somebody could throw some light on this or may be some other solution in java that would work out.
回答1:
boolean compareSuccess = compareImages("src/main/1.jpg","src/test/resources/bag_frame2.gif", "src/test/resources/ex.gif");
//(input1,input2,outputfile)
//(boolean=true if same image...false if changes in image)
compareImages (String data,String data1, String diff) {
CompareCmd compare = new CompareCmd();
// For metric-output
compare.setErrorConsumer(StandardStream.STDERR);
IMOperation cmpOp = new IMOperation();
// Set the compare metric
cmpOp.metric("mae");
// Add the expected image
cmpOp.addImage(data);
// Add the current image
cmpOp.addImage(data1);
// This stores the difference
cmpOp.addImage(diff);
try {
// Do the compare
compare.run(cmpOp);
return true;
}
catch (Exception ex) {
return false;
}
}
Try this.And dont forget to import im4java or maven
回答2:
If you are fine with doing system calls, you can use pdiff. http://pdiff.sourceforge.net/ The tool has been written to compare rendering engines and will output the differences between two files to a new file. It does not seem to have Java bindings though.
回答3:
In this link you can find an example of image comparison Class using jMagick. They used compare command line function to handle image comparison.
Code snippet:
String[] cmd = new String[] {"compare","-metric","MSE",file1,file2,"tempcompareImage.jpg"};
https://github.com/techblue/jmagick/blob/master/test/magicktest/MagickTesttools.java
Also, you can find the details of compare function here: http://www.imagemagick.org/Usage/compare/
来源:https://stackoverflow.com/questions/18760602/jmagick-image-comparision