gpt4 book ai didi

java - 如何通过java中的套接字发送图像?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:03:51 26 4
gpt4 key购买 nike

我正在编写一个客户端-服务器程序,我希望它能发送图像。代码如下:

//RECEIVER
while(true){
try{
socket = server.accept();

out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
in = new ObjectInputStream(socket.getInputStream());

System.out.println("Connected to "+PORTA+".");

while(!socket.isClosed()){
System.out.println("\nPrint the action");
azione = reader.readLine();

if(azione.equals("screenshot")){

out.writeObject("screenshot");
out.flush();
BufferedImage screenshot = ImageIO.read(in);

ImageIO.write(screenshot, "jpg", new File("screenshot.jpg"));
}
}catch(Exception ex){
System.out.println("Not connected.\n");
}
}

和服务器:

while(true){ 
try{
socket = new Socket(INDIRIZZO, PORT);

out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
in = new ObjectInputStream(socket.getInputStream());

while(!socket.isClosed()){
try {
action = (String)in.readObject();
if(azione.equals("screenshot")){
BufferedImage screenshot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenshot, "jpg", out);
}catch(Exception e){

}
}
}catch(Exception ex){
//
}
}

我的问题是客户端只有在我关闭套接字或输出流时才会收到图像,但我不希望这种情况发生。我怎样才能绕过它?如何以字节形式发送图像?谢谢!

最佳答案

问题是 ImageIO.read 等待流结束。套接字仅在您关闭它时发送它。 (这是有道理的)

您要做的是首先发送图像的大小,然后在接收端以字节数组形式读取图像。

public class Send {

public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 13085);
OutputStream outputStream = socket.getOutputStream();

BufferedImage image = ImageIO.read(new File("C:\\Users\\Jakub\\Pictures\\test.jpg"));

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);

byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());

Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
}
}


public class Receive {

public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();

System.out.println("Reading: " + System.currentTimeMillis());

byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();

byte[] imageAr = new byte[size];
inputStream.read(imageAr);

BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));

System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("C:\\Users\\Jakub\\Pictures\\test2.jpg"));

serverSocket.close();
}

}

关于java - 如何通过java中的套接字发送图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25086868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com