简易双端视频通信实现

客户端首先建立用户界面,使用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();

    }
}
相关推荐
是Dream呀20 分钟前
Python从0到100(八十六):神经网络-ShuffleNet通道混合轻量级网络的深入介绍
网络·python·神经网络
明 庭39 分钟前
通过 Docker 部署 pSQL 服务器的教程
服务器·docker·容器
山楂树の2 小时前
计算机网络 性能指标相关
网络·计算机网络
泪不是Web妳而流2 小时前
【HTML入门】Sublime Text 4与 Phpstorm
网络·经验分享·编辑器·html·学习方法·sublime text·phpstorm
star010-2 小时前
一文学会HTML编程之视频+图文详解详析
前端·网络·网络安全·html·html5
身在江湖的郭大侠2 小时前
Linux内核
linux·服务器
破-风4 小时前
linux的用法
linux·运维·服务器
__雨夜星辰__8 小时前
Linux 学习笔记__Day2
linux·服务器·笔记·学习·centos 7
学问小小谢8 小时前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
一ge科研小菜鸡8 小时前
网络安全实战指南:攻防技术与防御策略
网络