ru.yandex.qatools.ashot.cropper.DefaultCropper Maven / Gradle / Ivy
The newest version!
package ru.yandex.qatools.ashot.cropper;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.coordinates.Coords;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Set;
import static ru.yandex.qatools.ashot.coordinates.Coords.setReferenceCoords;
/**
* @author Pavel Zorin
*/
public class DefaultCropper extends ImageCropper {
@Override
public Screenshot cropScreenshot(BufferedImage image, Set coordsToCompare) {
Coords cropArea = Coords.unity(coordsToCompare);
Coords imageIntersection = Coords.ofImage(image).intersection(cropArea);
if (imageIntersection.isEmpty()) {
return new Screenshot(image);
}
BufferedImage cropped = new BufferedImage(imageIntersection.width, imageIntersection.height, image.getType());
Graphics g = cropped.getGraphics();
g.drawImage(
image,
0, 0,
imageIntersection.width, imageIntersection.height,
cropArea.x, cropArea.y,
cropArea.x + imageIntersection.width, cropArea.y + imageIntersection.height,
null
);
g.dispose();
Screenshot screenshot = new Screenshot(cropped);
screenshot.setOriginShift(cropArea);
screenshot.setCoordsToCompare(setReferenceCoords(screenshot.getOriginShift(), coordsToCompare));
return screenshot;
}
protected Coords createCropArea(Set coordsToCompare) {
return Coords.unity(coordsToCompare);
}
}