我尝试使用Robot类从服务器截取屏幕截图然后将其发送到android,然后在图像视图小部件上显示屏幕截图。
这是我的服务器代码:
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(400);
System.out.println("Running.....");
while (true) {
Socket serverSockt = server.accept();
System.out.println("Accept.....");
Toolkit toolKit = Toolkit.getDefaultToolkit();
Dimension dimension = toolKit.getScreenSize();
Robot robot = new Robot();
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimension));
ImageIcon icon = new ImageIcon(screenshot);
ObjectOutputStream oos = new ObjectOutputStream(serverSockt.getOutputStream());
oos.writeObject(icon);
oos.flush();
oos.close();
serverSockt.close();
}
} catch (IOException ex) {
Logger.getLogger(VNCserver.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException ex) {
Logger.getLogger(VNCserver.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t.start();
}
和这个客户端代码(android):
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Thread th = new Thread(new Runnable() {
@Override
public void run() {
try {
Socket clinetSocket = new Socket(txtIP.getText().toString(), Integer.parseInt(txtPort.getText().toString()))
ObjectInputStream ois = new ObjectInputStream(clinetSocket.getInputStream());
byte[] buffer = (byte[]) ois.readObject();
img = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
viwe.setImageBitmap(img);
clinetSocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
th.start();
}
});
我不知道哪里出错了?我没有收到IOException错误,我的第一个想法可能是我的客户端正在接收空输入流。有帮助吗?我做错了什么?如果我的整个代码是错误的..那么我如何使服务器截取然后将其发送到Android客户端并显示截图,无论是在图像视图还是Canvas。