论坛首页 Java企业应用论坛

带鼠标截屏

浏览 6427 次
锁定老帖子 主题:带鼠标截屏
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-12  
Java自带的awt包带了一个截屏的方法,
public class ScreenSnapshot {

    public static void main(String[] args) {
        try {
            int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
            int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
            Robot robot = new Robot();
            BufferedImage image = robot.createScreenCapture(new Rectangle(width,height));
            ImageIO.write (image, "png" , new File("c:/cross/test/1.png")); 
            
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我现在想用apache mina做一个多人桌面共享演示的软件,服务端每隔一定时间截屏发送给客户端。但是这个方法的缺点是没有把鼠标包含进来。既然是演示就需要鼠标的位置,有什么解决方案吗
   发表时间:2012-04-13  
各位都没遇到过这样的问题吗?????
0 请登录后投票
   发表时间:2012-04-13   最后修改:2012-04-13
可以用MouseInfo.getPointerInfo()获取鼠标当前位置的信息。

int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;

获取截屏的BufferedImage对象后,可以把你自定义的鼠标图片放到截图上当前鼠标所在的位置,这得用到Java 2D API.

BufferedImage image = robot.createScreenCapture(new Rectangle(width,height)); 

Image cursor = ImageIO.read(new File("c:/cursor.gif"));
int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;

Graphics2D graphics2D = image.createGraphics();
graphics2D.drawImage(cursor, x, y, 16, 16, null);//图片16*16大小
ImageIO.write (image, "png" , new File("c:/cross/test/1.png"));   


希望这个对你有所帮助。
0 请登录后投票
   发表时间:2012-04-13  
to jobar:

谢谢!只能自己在原始图片上绘制了。
0 请登录后投票
   发表时间:2012-04-13  
public class ScreenSnapshot {

    public static void main(String[] args) {
        
        try {
            Robot robot = new Robot();
            Toolkit toolkit= Toolkit.getDefaultToolkit();
            Rectangle screen_area= new Rectangle(toolkit.getScreenSize());

            Point p= MouseInfo.getPointerInfo().getLocation();
            BufferedImage screenshot= robot.createScreenCapture(screen_area);

            String imagepath = ScreenSnapshot.class.getResource("/").toString();
            String imagepath2 = ScreenSnapshot.class.getResource("/").getPath().toString();
            
            System.out.println(imagepath+"cursor.gif");
            System.out.println(imagepath2+"cursor.gif");
            
            BufferedImage cursor= ImageIO.read(new File(imagepath2+"cursor.gif"));
            screenshot.createGraphics().drawImage(cursor, p.x, p.y, null);

            ImageIO.write(screenshot, "jpg", new File("c:/cross/output.jpg"));
            ImageIO.write(screenshot, "png", new File("c:/cross/output.png"));
        } catch (HeadlessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
0 请登录后投票
   发表时间:2012-04-13  
嗯,效果怎么样,好使吧
0 请登录后投票
   发表时间:2012-04-13  
还要考虑两个屏幕的大小不同
0 请登录后投票
   发表时间:2012-05-11  
得到鼠标坐标,然后是拖拽事件,得到拖拽后的坐标。然后截图
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics