我无法在输出流中写入图像图标。这是我的代码。请任何人帮助我。
public ScreenSpyer(Socket socket, Robot robot, Rectangle rect) {
this.socket = socket;
this.robot = robot;
this.rectangle = rect;
start();
}
public void run(){
oos = null;
try{
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(rectangle);
// oos.flush();
// oos.reset();
}catch(IOException ex){
ex.printStackTrace();
}
while(continueLoop){
//Capture screen
image = robot.createScreenCapture(rectangle);
imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
System.out.println("intermidiate");
// oos.reset();
oos.writeObject(imageIcon);
System.out.println("New screenshot sent");
//oos.reset();
//oos.flush();
oos.reset();
} catch (IOException ex) {
ex.printStackTrace();
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
最佳答案
你说“它被卡住了”;你怎么知道?这显然是一个线程,被其他代码终止。我假设跟踪输出“新屏幕截图已发送”没有被执行;这可能是因为它被卡住了,或者 writeObject() 可能会抛出一个您没有捕获的异常。
在 IOException 之后捕获 throwable 以查看是否存在另一个异常。
生成镜像后,立即将其替换为已知镜像并查看是否写入;这将有助于确定这个特定的 writeObject() 调用是否存在问题,或者程序中其他地方是否有问题。
尝试使用屏幕上的一个小矩形,而不是全部。也许 getScreenSize() 返回一些不可用的东西,比如比屏幕大一个像素大小的东西。如果小矩形有效,请尝试在两个维度上将矩形缩小几个像素。
关于java - 在输出流中写入 imageicon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713633/