1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class ScreenShotFactory {
public final static String IMAGE_TYPE_JPEG = "jpeg";
public final static String IMAGE_TYPE_GIF = "gif";
public final static String IMAGE_TYPE_PNG = "png";
public static void screenShot(Rectangle screenArea,
Dimension screenshotFinalDimension, String pictureName,
String compressionType) {
BufferedImage buf = null; // Notre capture d'écran originale
BufferedImage bufFinal = null; // Notre capture d'écran redimensionnée
try {
// Création de notre capture d'écran
buf = new Robot().createScreenCapture(screenArea);
} catch (AWTException e) {
e.printStackTrace();
}
// Création de la capture finale
bufFinal = new BufferedImage(screenshotFinalDimension.width,
screenshotFinalDimension.height, BufferedImage.TYPE_INT_RGB);
// Redimensionnement de la capture originale
Graphics2D g = (Graphics2D) bufFinal.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(buf, 0, 0, screenshotFinalDimension.width,
screenshotFinalDimension.height, null);
g.dispose();
// Ecriture de notre capture d'écran redimensionnée
try {
ImageIO.write(bufFinal, compressionType, new File(pictureName));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ScreenShotFactory.screenShot(new Rectangle(0, 0, 100, 100),
new Dimension(50, 50), "test.png",
ScreenShotFactory.IMAGE_TYPE_PNG);
}
} |
Partager