我正在尝试将连续屏幕截图发送到同一网络上的另一台PC。
我也搜索了它,但我没有找到与我相关的东西。 以下是我的发件人和收件人代码。问题是只有第一张图像被接收然后被击中。 如果我打开JFrame,则不会在JPanel中更新单个图像。请帮帮我: - (imagesender.java
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
class imagesender
{
imagesender()throws Exception
{
Socket soc=new Socket("127.0.0.1",5555);
OutputStream os=soc.getOutputStream();
while(true)
{
try
{
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rec=new Rectangle(dim);
Robot bot=new Robot();
BufferedImage image=bot.createScreenCapture(rec);
ImageIO.write(image,"jpeg",os);
System.out.println("Image Sent");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String aerg[])throws Exception
{
new imagesender();
}
}
imagereciever.java
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;
class imagereciever extends JPanel
{
static BufferedImage image;
imagereciever()throws Exception
{
ServerSocket ss=new ServerSocket(5555);
Socket soc=ss.accept();
JFrame frame=new JFrame();
frame.setSize(500,500);
frame.setContentPane(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setVisible(true);
InputStream is=soc.getInputStream();
while(true)
{
try
{
image=ImageIO.read(is);
//this.repaint();
ImageIO.write(image,"jpeg",new File("C:\\Users\\Arpit Jindal\\Desktop\\screenshot.jpeg"));
System.out.println("Image Recieved");
}
catch(Exception e)
{e.printStackTrace()}
}
}
public static void main(String aerg[])throws Exception
{
new imagereciever();
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,0,0,null);
}
}
imagereciever.java 收到一张图片,然后无限次地发出此错误, imagesender.java 继续发送图片: -
java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
at javax.imageio.ImageIO.write(ImageIO.java:1520)
at imagereciever.<init>(imagereciever.java:26)
at imagereciever.main(imagereciever.java:37)
答案 0 :(得分:4)
你的GUI完全忽略了Swing线程规则,所以我不会感到惊讶它可能会卡住。建议:
SwingWorker<Void, BufferedImage>
,以便您可以通过发布/处理方法对将BufferedImage传递给GUI。 Concurrency in Swing教程将告诉您有关如何使用此工具的详细信息。答案 1 :(得分:0)
示例@MadProgrammer给了我解决了我的问题。这是我的新代码: -
<强> imagesender.java 强>
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
class imagesender
{
imagesender()throws Exception
{
Socket soc=new Socket("127.0.0.1",5555);
OutputStream os=soc.getOutputStream();
while(true)
{
try
{
BufferedImage image=new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(image,"png",baos);
baos.close();
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(baos.size()+"");
os.write(baos.toByteArray());
//System.out.println("Image Sent");
}
catch(Exception e)
{
System.exit(1);
}
}
}
public static void main(String aerg[])throws Exception
{
new imagesender();
}
}
<强> imagereciever.java 强>
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;
class imagereciever extends JPanel
{
static BufferedImage image;
static Socket soc;
static InputStream is;
imagereciever()throws Exception
{
ServerSocket ss=new ServerSocket(5555);
soc=ss.accept();
is=soc.getInputStream();
JFrame frame=new JFrame();
frame.setSize(500,500);
frame.setContentPane(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
while(true)
{
try
{
ObjectInputStream ois=new ObjectInputStream(is);
int size=Integer.parseInt(ois.readObject().toString());
ByteArrayOutputStream baos=new ByteArrayOutputStream(size);
int sizeread=0,bytesin=0;
byte[] buffer=new byte[1024];
while(sizeread<size)
{
bytesin=is.read(buffer);
sizeread+=bytesin;
baos.write(buffer,0,bytesin);
}
baos.close();
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
image=ImageIO.read(bais);
this.repaint();
}
catch(Exception e)
{
System.exit(1);
}
}
}
public static void main(String aerg[])throws Exception
{
new imagereciever();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,0,0,null);
}
}