zoukankan      html  css  js  c++  java
  • java原生实现屏幕设备遍历和屏幕采集(捕获)等功能

    前言:本章中屏幕捕获使用原生java实现,屏幕图像显示采用javacv1.3的CanvasFrame

    一、实现的功能

    1、屏幕设备遍历

    2、本地屏幕图像采集(也叫屏幕图像捕获)

    3、播放本地图像(采用javacv)

    4、关闭播放窗口即停止图像采集

    二、实现代码

    	public static void captureScreen(){
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// 获取当前屏幕大小
    		Rectangle rectangle = new Rectangle(screenSize);// 指定捕获屏幕区域大小,这里使用全屏捕获
    		//做好自己!--eguid,eguid的博客是:blog.csdn.net/eguid_1
    		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//本地环境
    		GraphicsDevice[] gs = ge.getScreenDevices();//获取本地屏幕设备列表
    		System.err.println("eguid温馨提示,找到"+gs.length+"个屏幕设备");
    		Robot robot=null;
    		int ret=-1;
    		for(int index=0;index<10;index++){
    			GraphicsDevice g=gs[index];
    			try {
    				robot= new Robot(g);
    				BufferedImage img=robot.createScreenCapture(rectangle);
    				if(img!=null&&img.getWidth()>1){
    					ret=index;
    					break;
    				}
    			} catch (AWTException e) {
    				System.err.println("打开第"+index+"个屏幕设备失败,尝试打开第"+(index+1)+"个屏幕设备");
    			}
    		}
    		System.err.println("打开的屏幕序号:"+ret);
    		CanvasFrame frame = new CanvasFrame("eguid屏幕录制");// javacv提供的图像展现窗口
    		int width = 800;
    		int height = 600;
    		frame.setBounds((int) (screenSize.getWidth() - width) / 2, (int) (screenSize.getHeight() - height) / 2, width,
    				height);// 窗口居中
    		frame.setCanvasSize(width, height);// 设置CanvasFrame窗口大小
    		while (frame.isShowing()) {
    			BufferedImage image = robot.createScreenCapture(rectangle);// 从当前屏幕中读取的像素图像,该图像不包括鼠标光标
    			frame.showImage(image);
    			
    			try {
    				Thread.sleep(45);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		frame.dispose();
    	}


    三、测试结果

    找到1个屏幕设备
    打开的屏幕:0



  • 相关阅读:
    使用iOS网络请求
    Invalid RNPermission 'ios.permission.xxx'. should be one of: ( )
    React Native 报错 Error: spawn EACCES 权限
    React Native 适配Android物理返回键,实现连续两次点击退出
    图解:平衡二叉树,AVL树
    NOIP 骗分技巧
    P1004 方格取数
    5. 最长回文子串
    全链路压测自动化实践
    深度学习在美团配送ETA预估中的探索与实践
  • 原文地址:https://www.cnblogs.com/eguid/p/6821562.html
Copyright © 2011-2022 走看看