当前位置: 首页 > news >正文

简易双端视频通信实现

客户端首先建立用户界面,使用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() {@Overridepublic void actionPerformed(ActionEvent e) {//启动摄像头,获取视频数据openVideo(g);}});}//开启摄像头public void openVideo(Graphics g){Webcam cam=Webcam.getDefault();cam.open();new Thread(new Runnable() {@Overridepublic 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() {@Overridepublic 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();}
}

http://www.mrgr.cn/news/958.html

相关文章:

  • 16.FreeRTOS内存管理
  • 基于SpringBoot的论坛系统设计与实现(源码+lw+部署文档+讲解等)
  • 正则表达式入门:Python ‘ re ‘ 模块详解
  • TCP/IP 协议及其协议号
  • Java-HttpURLConnection 从服务器获取输入流和使用RestTemplate来获取输入流。
  • 一个简单的WEB开发案例
  • 7.实时时钟(RTC)
  • 深入解析CDN(内容分发网络):架构、优势与实现
  • 微分方程(Blanchard Differential Equations 4th)中文版Section3.2
  • ansible
  • SQL - 汇总与分组
  • 机器学习(3)-- 一元线性回归
  • Linux内核(5)——从udev/mdev工作原理到亲手实现SD卡热插拔
  • CentOS7下制作openssl1.1.1i RPM包并升级
  • BUUCTF PWN wp--warmup_csaw_2016
  • MySQL面试相关精选
  • Github 2024-08-19 开源项目周报Top15
  • jenkins workpase优化清理
  • AutoTiny电脑自动化操作
  • FunClip,音视频识别,自动化剪辑,文本校对,智能纠错,导出SRT