Printscreen (screen shot) in java

مسا الخير

اليوم جايبلكون كود جافا بيقوم باخد صورة للشاشة ويحفظها على الهارد ديسك

يمكن انا نحدد حجم الصورة – مكان الصورة – نوع الصورة

الكود مو صعب ابدا وهو بالاعتماد بشكل اساسي على الصف

java.aw.Robot

واللي هوي بشكل اساسي عبارة عن صف للتعامل مع اجهزة الادخال مثل لوحة المفاتيح – الفارة بشكل تلقائي وذلك كما في العروض التقديمية …. وحاطلكون بعد الكود شرح كامل عنو

package eadoking_com;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
*
* @author  Eadoking
* @website http://eadoking.com
* @email   eadoking@yahoo.com
* @email   info@eadoking.com
* @mobile  +963 932728084
*/

// This is the main class
public class eadoking_com_ScreenShot {

// main function
public static void main(String[] args) {

try {

// this constants you can change it as you want the screenshot
int HEIGHT=1280; // screenshot height
int WIDTH =1024; // screenshot width

Robot robot = new Robot();

// Capture the screen shot of the area of the screen defined by the rectangle
BufferedImage bi=robot.createScreenCapture(new Rectangle(1280,1024));

// write the image in d:\imagetest.bmp"
// you can select jpg too but bmp more clear
ImageIO.write(bi, "bmp", new File("d:/eadoking.com_ScreenShot.bmp"));

JOptionPane.showMessageDialog(null, "Screenshot saved correctly", "Eadoking.com screenshot test",JOptionPane.INFORMATION_MESSAGE);

} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}


English form java docs
public class Robot
extends Object

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform’s native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server.

Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully.


Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

About eadoking

Ead AlDoukanje - EADOKING - 3edoOo ( عيد الدكنجي Studied Informatics Engineering at Tishreen University ( Software Engineering Department ) Work as freelancer Web Developer / Software Developer You can contact me to me@eadoking.com mobile : ( +963 ) 0932728084 Programming Skills : Java, C# , C++ , Java Server Pages (JSP ) with SERVLETS, PHP ,HTML , CSS ,JAVA SCRIPT with Ajax , ASP.NET , Web Services .

Stay in touch

subscribe with us using social media or email to stay updated
you can also contact us if you need some request or any thing to ,
http://me.eadoking.com/contact-me

Discuss: “Printscreen (screen shot) in java”

  1. June 19, 2010 at 1:56 pm #

    اهلا وسهلا
    الصورة حلها كتير بسيط
    شفت الكود اللي عامل فيه انا
    ImageIO.write(bi, “bmp”, new File(“d:/eadoking.com_ScreenShot.bmp”));
    هون نوع الصورة BMP
    وهيي حجما شوي كبير
    لنصغر الحجم فينا نغير نوع الصورة اللي عم نحفظها ومنحطها مثلا JPG وبصير الكود كما يلي
    ImageIO.write(bi, “jpg”, new File(“d:/eadoking.com_ScreenShot.jpg”));

    واذا لسا رح نصغر الحجم فينا نعمل الصورة GIF
    وطبعا بتخف الدقة كلما صغر الحجم ,… وبصير الكود كما يلي
    ImageIO.write(bi, “gif”, new File(“d:/eadoking.com_ScreenShot.gif”));

    تمنى انو اكون اوصلت الفكرة بشكل جيد :)

  2. June 19, 2010 at 9:07 am #

    جزاكم الله خيرا انا استفدت من الكود بتاعك بس مساحة الصورة كبيرة ياترى ازاي اقلل المساحة عن 100 كيلو بايت

    Posted by A Ali Taha

Leave a Reply


*