我想做一个java程序,把用网络摄像头拍摄的照片从一台计算机发送到另一台没有连接到局域网的计算机上。
我可以让程序拍摄照片并将其转换为文件,但我无法成功地将它们发送到另一台计算机。
我不一定要发送照片,但要发送任何形式的数据。我对这种方法是新手,所以我可能会犯很多错误,因为我不太确定它是如何工作的。
到目前为止,我尝试的代码如下:
客户端(我想要从其发送数据的计算机,它应该截屏并将其发送到服务器):
public class GreetingClient
{
Image newimg;
static BufferedImage bimg;
byte[] bytes;
public static void main(String [] args)
{
String serverName = "127.0.0.1";//tried "localhost" and my actual ip adress as well.
int port = 6066;
try
{
Socket client = new Socket(serverName, port);
Robot bot;
bot = new Robot();
bimg = bot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(bimg,"JPG",client.getOutputStream());
client.close();
} catch(IOException | AWTException e) {
e.printStackTrace();
}
}
}
服务器代码(应接收数据的计算机):
public class Main extends Thread
{
private ServerSocket serverSocket;
Socket server;
public Main(int port) throws IOException, SQLException, ClassNotFoundException, Exception
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(180000);
}
public void run()
{
while(true)
{
try
{
server = serverSocket.accept();
JFrame frame = new JFrame();
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(server.getInputStream()));
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.setVisible(true);
frame.pack();
}
catch(SocketTimeoutException st)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception
{
Thread t = new Main(6066);
t.start();
}
}
当我尝试将数据从我的pc发送到我的pc时,它可以工作(如果这有意义的话),但当我尝试从另一台未与我的本地连接的计算机发送数据时,它不起作用。
如果可能的话,我想在不激活端口转发的情况下实现这一点。
任何帮助都是非常感谢的。
发布于 2021-10-14 12:04:41
你也可以用java编写一个简单的客户端-服务器聊天应用,然后将转换后的JPG消息发送到例如Base64。然后,当接收到它们时,将其解码。因此,首先要编写聊天应用程序,然后编写新的功能来自动将JPG转换为Base64,并在窗口中显示图像:
//服务器:
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(3456);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
//客户端:
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",3456);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
//图片显示:
import java.awt.*;
import javax.swing.JFrame;
public class MyCanvas extends Canvas{
public void paint(Graphics g) {
Toolkit t=Toolkit.getDefaultToolkit();
Image i=t.getImage("p3.gif");
g.drawImage(i, 120,100,this);
}
public static void main(String[] args) {
MyCanvas m=new MyCanvas();
JFrame f=new JFrame();
f.add(m);
f.setSize(400,400);
f.setVisible(true);
}
}
//转换:
public static void main(String[] args) throws Exception{
File f = new File("/opt/myImage.jpg");
String encodstring = encodeFileToBase64Binary(f);
System.out.println(encodstring);
}
private static String encodeFileToBase64Binary(File file) throws Exception{
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
return new String(Base64.encodeBase64(bytes), "UTF-8");
}
https://stackoverflow.com/questions/69568944
复制相似问题