preface

In this chapter, native Java is used for screen capture, and CanvasFrame of javacv1.3 is used for screen image display

One, the realization of the function

1. Screen device traversal

2. Local screen image capture (also known as screen image capture)

3. Play local images (using JavacV)

4. Close the playing window to stop image collection

Two, implementation code

Simple screen capture


	public static void captureScreen(a){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// Get the current screen size
		Rectangle rectangle = new Rectangle(screenSize);// Specify the size of the capture screen area, where full screen capture is used
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();// Local environment
		GraphicsDevice[] gs = ge.getScreenDevices();// Get the list of local screen devices
		System.err.println("Eguid tips found."+gs.length+"A screen device.");
		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("Open"+index+"One screen device failed, try opening the first."+(index+1) +"A screen device.");
			}
		}
		System.err.println("Open screen serial number:"+ret);
		CanvasFrame frame = new CanvasFrame("Eguid screen recording");// Javacv provides an image display window
		int width = 800;
		int height = 600;
		frame.setBounds((int) (screenSize.getWidth() - width) / 2, (int) (screenSize.getHeight() - height) / 2, width,
				height);// The window is centered
		frame.setCanvasSize(width, height);// Set the CanvasFrame window size
		while (frame.isShowing()) {
			BufferedImage image = robot.createScreenCapture(rectangle);// A pixel image read from the current screen, excluding the mouse cursor
			frame.showImage(image);
			
			try {
				Thread.sleep(45);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		frame.dispose();
	}
Copy the code

3. Test results

Find 1 screen device open screen: 0