简易双端视频通信实现

客户端首先建立用户界面,使用Webcam-capture获取摄像头,使用线程将每一帧图片显示在用户界面上实现视频效果,同时使用BufferedImage获取图片数据,使用Socket与服务端连接,并使用DataOutputStream像服务端发送图片数据。

服务端首先建立用户界面,使用ServerSocket、Socket与客户端连接,在线程中使用InputStream和DataInputStream不断读取客户端发来的图片数据,并用画笔Graphics画在用户界面上。

代码实现:

1、客户端

复制代码
package video;

import com.github.sarxos.webcam.Webcam;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class VideoClient {
    public static void main(String[] args) {
        new VideoClient().videoUI();
    }
    public void videoUI(){
        JFrame jf=new JFrame("视频通信_client");
        jf.setSize(900,900);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);
        jf.setLayout(new FlowLayout());

        JButton start=new JButton("视频");
        jf.add(start);

        jf.setVisible(true);

        Graphics g=jf.getGraphics();

        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //启动摄像头,获取视频数据
                openVideo(g);
            }
        });
    }
    //开启摄像头
    public void openVideo(Graphics g){
        Webcam cam=Webcam.getDefault();
        cam.open();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Socket socket=null;
                DataOutputStream dos=null;
                try {
                    socket=new Socket("127.0.0.1",8899);
                    OutputStream os=socket.getOutputStream();
                    dos=new DataOutputStream(os);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                while (true){
                    BufferedImage bufferedImage=cam.getImage();
                    g.drawImage(bufferedImage,30,100,null);

                    try {
                        Thread.sleep(10);
                        startVideo(bufferedImage,dos);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

    }
    //与服务器建立连接,发送视频数据(像素点)
    public void startVideo(BufferedImage image,DataOutputStream dos)throws Exception{

        //获取图片的像素点
        int w=image.getWidth();
        int h=image.getHeight();

        //发送图片大小
        dos.writeInt(w);
        dos.writeInt(h);
        dos.flush();

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                int pixel=image.getRGB(j,i);
                dos.writeInt(pixel);
                dos.flush();
            }
        }
    }
}

2、服务端

复制代码
package video;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class VideoServer {
    public static void main(String[] args) {
        new VideoServer().videoUI();
    }
    private Graphics g;
    public void videoUI(){
        JFrame jf=new JFrame("视频通信_server");
        jf.setSize(900,900);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);
        jf.setLayout(new FlowLayout());

        jf.setVisible(true);

        g=jf.getGraphics();

        try {
            readImage();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //读取发来的视频数据
    public void readImage() throws Exception{
        ServerSocket server=new ServerSocket(8899);
        System.out.println("启动服务器...");

        Socket socket=server.accept();
        InputStream is=socket.getInputStream();
        DataInputStream dis=new DataInputStream(is);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(10);
                        int w=dis.readInt();
                        int h=dis.readInt();

                        BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
                        for(int i=0;i<h;i++){
                            for(int j=0;j<w;j++){
                                int pixel=dis.readInt();
                                image.setRGB(j,i,pixel);
                            }
                        }
                        g.drawImage(image,30,80,null);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

    }
}
相关推荐
能不能别报错4 小时前
openclaw-linux部署教程+mimo-v2-pro
linux·运维·服务器
小虎卫远程打卡app5 小时前
光通信与视频编码前沿技术综述:从超大容量传输到实时神经网络编码
运维·网络·信息与通信·视频编解码
钛态5 小时前
Flutter for OpenHarmony:mockito 单元测试的替身演员,轻松模拟复杂依赖(测试驱动开发必备) 深度解析与鸿蒙适配指南
服务器·驱动开发·安全·flutter·华为·单元测试·harmonyos
%小农6 小时前
在cursor中使用server
网络·网络协议·http
硅基导游7 小时前
Linux内核观测与跟踪的利器BPF环境测试
linux·服务器·性能监控·bpf
ivy159868377157 小时前
芯锦科技 HP9117 多协议USB Type-A快充识别芯片
网络·科技·网络协议·5g·信号处理·p2p
我是谁??8 小时前
在 Rocky Linux 9 无桌面环境中通过 SSH 安装 KVM 虚拟机(Rocky9含 XFCE 桌面/xubuntu20)完整指南
linux·服务器·ssh
西红市杰出青年8 小时前
MCP 的三种数据传输模式教程(stdio / SSE / Streamable HTTP)
网络·网络协议·http·ai
Filotimo_8 小时前
内网穿透概念
网络