[英]Writing imageicon in the output stream
我無法在輸出流中寫一個圖像圖標。這是我的代碼。請任何人幫助我。
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之后捕獲可拋出的異常,以查看是否還有另一個異常。
生成圖像后,立即將其替換為已知圖像,看是否被寫入; 這將有助於弄清楚此特定的writeObject()調用是否有問題或程序中其他地方是否有問題。
嘗試使用屏幕上的一個小矩形,而不是全部。 也許getScreenSize()返回一些不可用的東西,例如比屏幕大一個像素的東西。 如果一個小的矩形有效,請嘗試在兩個維度上將矩形縮小幾個像素。
看來您實際上是在嘗試將屏幕快照圖像保存到OutputStream
或磁盤中。
在這種情況下,您不必使用ImageIcon
。 您可以保存從createScreenCapture
調用返回的圖像。 您可以使用ImageIO
保存圖像:
ImageIO.write(BufferedImage image, String formatName, File output);
要么
ImageIO.write(BufferedImage image, String formatName, ImageOutputStream output);
要么
ImageIO.write(BufferedImage image, String formatName, OutputStream output);
formatName
可以是jpg,png或gif。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.