如何加快Remote Viewer的帧速率?

时间:2018-11-08 15:00:36

标签: java performance remote-access frame-rate cpu-speed

因此,我构建了一个包含两部分的应用程序,即客户端和服务器。客户端获取屏幕截图,将其转换为字节数组,将其发送到服务器,然后等待响应以重复所有操作。服务器获取字节数组,将其转换为屏幕截图,显示屏幕截图,然后告诉客户端已完成。

一切正常,但是问题是整个过程花费大约200-350ms,这意味着我最多只能获得5 fps。这显然不能令人满意,因此我想知道如何才能加快此过程。有没有更快的截图方法?使用1080x1920显示器而不是较小的分辨率是否会带来很大的不同?是否由于图像必须调整为盒子大小而导致时间太长?等。

这是客户:

// Capture the image
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = robot.createScreenCapture(screenRect);
// Convert the image to a byte array

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(capture, "jpg", baos);
byte[] bytes = baos.toByteArray();
// Send byte array over socket connection
Socket socket = new Socket("machine", 2282);
DataOutputStream outputData = new DataOutputStream(socket.getOutputStream());
DataInputStream inputData = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
outputData.write(bytes);
outputData.close();
// Wait for response
String incomingData = inputData.readUTF();
inputData.close();

if (incomingData.equals("1"))
    break;

和服务器:

long startTime = System.nanoTime();
// Get client and receive byte array
ServerSocket server = new ServerSocket(2282);
Socket socket = server.accept();
DataInputStream inputData = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream outputData = new DataOutputStream(socket.getOutputStream());
// Read bytes to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
for (int s; (s = inputData.read(buffer)) != -1;) {
  baos.write(buffer, 0, s);
}
byte result[] = baos.toByteArray();
// Create bufferedImage from byte array
BufferedImage bufferedImage = null;
ByteArrayInputStream bais = new ByteArrayInputStream(result);
bufferedImage = ImageIO.read(bais);
// Update image box
imageBox.setBounds(10, 11, (frame.getWidth() - 40), (frame.getHeight()) - 50);
BufferedImage resizedImage = resize(bufferedImage, imageBox.getHeight(), imageBox.getWidth());
imageBox.setIcon(new ImageIcon(resizedImage));

outputData.writeUTF("1");
// Close everything
outputData.close();
server.close();
socket.close();
inputData.close();
baos.close();
bais.close();

long endTime = System.nanoTime();
long totalTime = (endTime - startTime) / 1000000;
System.out.println(totalTime);

然后调整大小方法:

public static BufferedImage resize(BufferedImage img, int height, int width) {
    Image image = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2d = resized.createGraphics();
    graphics2d.drawImage(image, 0, 0, null);
    graphics2d.dispose();
    return resized;
}

运行此服务器时,服务器占用大约20%的CPU(3.20 GHz)和40%的RAM(8GB)。不仅限于此,但通常不会超过该值。

0 个答案:

没有答案